-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindexing.py
156 lines (127 loc) · 5.01 KB
/
indexing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from os import getenv
from algoliasearch.search_client import SearchClient
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
# Algolia client credentials
ALGOLIA_APP_ID = getenv('ALGOLIA_APP_ID')
ALGOLIA_API_KEY = getenv('ALGOLIA_API_KEY')
ALGOLIA_INDEX_NAME = getenv('ALGOLIA_INDEX_NAME')
# Initialize the client
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/?client=python
client = SearchClient.create(ALGOLIA_APP_ID, ALGOLIA_API_KEY)
# Initialize an index
# https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
index = client.init_index(ALGOLIA_INDEX_NAME)
# Define some objects to add to our index
# https://www.algolia.com/doc/api-client/methods/indexing/#object-and-record
contacts = [
{
'name': 'Foo',
'objectID': '1'
},
{
'name': 'Bar',
'objectID': '2'
}
]
# We don't have any objects (yet) in our index
res = index.search('')
print('Current objects: ', res['hits'])
# Save Objects: Add mutliple new objects to an index.
# https://www.algolia.com/doc/api-reference/api-methods/add-objects/?client=python
print('Save Objects - Adding multiple objects: ', contacts)
index.save_objects(contacts).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Save Objects: Replace an existing object with an updated set of attributes.
# https://www.algolia.com/doc/api-reference/api-methods/save-objects/?client=python
print(f'Save Objects - Replacing objects’s attributes on {contacts[0]}')
new_contact = {
'name': 'FooBar',
'objectID': '1'
}
index.save_object(new_contact).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Partial Update Objects: Update one or more attributes of an existing object.
# https://www.algolia.com/doc/api-reference/api-methods/partial-update-objects/?client=python
print(f'Save Objects - Updating object’s attributes on {contacts[0]}')
new_contact = {
'email': '[email protected]', # New attribute
'objectID': '1'
}
index.partial_update_object(new_contact).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Delete Objects: Remove objects from an index using their objectID.
# https://www.algolia.com/doc/api-reference/api-methods/delete-objects/?client=python
objectID_to_delete = contacts[0]["objectID"]
print(f'Delete Objects - Deleting object with objectID "{objectID_to_delete}"')
index.delete_object(objectID_to_delete).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Replace All Objects: Clears all objects from your index and replaces them with a new set of objects.
# https://www.algolia.com/doc/api-reference/api-methods/replace-all-objects/?client=python
new_contacts = [
{
'name': 'NewFoo',
'objectID': '3'
},
{
'name': 'NewBar',
'objectID': '4'
}
]
print(f'Replace All Objects - Clears all objects and replaces them with "{new_contacts}"')
index.replace_all_objects(new_contacts).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Delete By: Remove all objects matching a filter (including geo filters).
# https://www.algolia.com/doc/api-reference/api-methods/delete-by/?client=python
print(f'Delete By - Remove all objects matching "name:NewBar"')
# Firstly, have an attribute to filter on
# https://www.algolia.com/doc/api-client/methods/settings/?client=python
index.set_settings({
'attributesForFaceting': ['name']
}).wait()
index.delete_by({
'facetFilters': [f'name:NewBar'] # https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/
}).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Get Objects: Get one or more objects using their objectIDs.
# https://www.algolia.com/doc/api-reference/api-methods/get-objects/?client=python
object_id = new_contacts[0]['objectID']
print(f'Get Objects - Getting object with objectID "{object_id}"')
res = index.get_object(object_id)
print('Results: ', res, '\n')
# Custom Batch: Perform several indexing operations in one API call.
# https://www.algolia.com/doc/api-reference/api-methods/batch/?client=python
operations = [
{
'action': 'addObject',
'indexName': ALGOLIA_INDEX_NAME,
'body': {
'name': 'BatchedBar',
}
},
{
'action': 'updateObject',
'indexName': ALGOLIA_INDEX_NAME,
'body': {
'objectID': object_id,
'name': 'NewBatchedBar',
}
}
]
print(f'Custom Batch - Batching {len(operations)} operations')
res = client.multiple_batch(operations).wait()
res = index.search('')
print('Current objects: ', res['hits'], '\n')
# Clear Objects: Clear the records of an index without affecting its settings.
# https://www.algolia.com/doc/api-reference/api-methods/clear-objects/?client=python
print('Clear Objects: Clear the records of an index without affecting its settings.')
index.clear_objects().wait()
# We don't have any objects in our index
res = index.search('')
print('Current objects: ', res['hits'])