|
| 1 | +import json |
1 | 2 | import os |
2 | 3 | import sys |
3 | 4 |
|
|
7 | 8 | import typesense |
8 | 9 |
|
9 | 10 | client = typesense.Client({ |
10 | | - 'api_key': 'abcd', |
11 | | - 'nodes': [{ |
12 | | - 'host': 'localhost', |
13 | | - 'port': '8108', |
14 | | - 'protocol': 'http' |
15 | | - }], |
16 | | - 'timeout_seconds': 2 |
| 11 | + 'api_key': 'abcd', |
| 12 | + 'nodes': [{ |
| 13 | + 'host': 'localhost', |
| 14 | + 'port': '8108', |
| 15 | + 'protocol': 'http' |
| 16 | + }], |
| 17 | + 'timeout_seconds': 2 |
17 | 18 | }) |
18 | 19 |
|
19 | | - |
20 | 20 | # Create a collection |
21 | 21 |
|
22 | 22 | create_response = client.collections.create({ |
23 | | - "name": "books", |
24 | | - "fields": [ |
25 | | - {"name": "title", "type": "string" }, |
26 | | - {"name": "authors", "type": "string[]" }, |
27 | | - {"name": "authors_facet", "type": "string[]", "facet": True }, |
28 | | - {"name": "publication_year", "type": "int32" }, |
29 | | - {"name": "publication_year_facet", "type": "string", "facet": True }, |
30 | | - {"name": "ratings_count", "type": "int32" }, |
31 | | - {"name": "average_rating", "type": "float" }, |
32 | | - {"name": "image_url", "type": "string" } |
33 | | - ], |
34 | | - "default_sorting_field": "ratings_count" |
| 23 | + "name": "books", |
| 24 | + "fields": [ |
| 25 | + {"name": "title", "type": "string"}, |
| 26 | + {"name": "authors", "type": "string[]"}, |
| 27 | + {"name": "authors_facet", "type": "string[]", "facet": True}, |
| 28 | + {"name": "publication_year", "type": "int32"}, |
| 29 | + {"name": "publication_year_facet", "type": "string", "facet": True}, |
| 30 | + {"name": "ratings_count", "type": "int32"}, |
| 31 | + {"name": "average_rating", "type": "float"}, |
| 32 | + {"name": "image_url", "type": "string"} |
| 33 | + ], |
| 34 | + "default_sorting_field": "ratings_count" |
35 | 35 | }) |
36 | 36 |
|
37 | 37 | print(create_response) |
|
48 | 48 | # Add a book |
49 | 49 |
|
50 | 50 | hunger_games_book = { |
51 | | - 'id': '1', 'original_publication_year': 2008, 'authors': ['Suzanne Collins'], 'average_rating': 4.34, |
52 | | - 'publication_year': 2008, 'publication_year_facet': '2008', 'authors_facet': ['Suzanne Collins'], |
53 | | - 'title': 'The Hunger Games', |
54 | | - 'image_url': 'https://images.gr-assets.com/books/1447303603m/2767052.jpg', |
55 | | - 'ratings_count': 4780653 |
| 51 | + 'id': '1', 'original_publication_year': 2008, 'authors': ['Suzanne Collins'], 'average_rating': 4.34, |
| 52 | + 'publication_year': 2008, 'publication_year_facet': '2008', 'authors_facet': ['Suzanne Collins'], |
| 53 | + 'title': 'The Hunger Games', |
| 54 | + 'image_url': 'https://images.gr-assets.com/books/1447303603m/2767052.jpg', |
| 55 | + 'ratings_count': 4780653 |
56 | 56 | } |
57 | 57 |
|
58 | 58 | client.collections['books'].documents.create(hunger_games_book) |
59 | 59 |
|
60 | 60 | # Export the documents from a collection |
61 | 61 |
|
62 | | -print(client.collections['books'].documents.export()) |
| 62 | +exported_doc_strs = client.collections['books'].documents.export() |
| 63 | +print(exported_doc_strs) |
63 | 64 |
|
64 | 65 | # Fetch a document in a collection |
65 | 66 |
|
|
77 | 78 |
|
78 | 79 | print(client.collections['books'].documents['1'].delete()) |
79 | 80 |
|
| 81 | +# Import documents into a collection |
| 82 | +docs_to_import = [] |
| 83 | +for exported_doc_str in exported_doc_strs: |
| 84 | + docs_to_import.append(json.loads(exported_doc_str)) |
| 85 | + |
| 86 | +import_res = client.collections['books'].documents.create_many(docs_to_import) |
| 87 | +print(import_res["success"]) |
| 88 | + |
80 | 89 | # Drop the collection |
81 | 90 |
|
82 | 91 | drop_response = client.collections['books'].delete() |
|
0 commit comments