-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindexing.js
166 lines (138 loc) · 5.4 KB
/
indexing.js
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
157
158
159
160
161
162
163
164
165
166
const algoliasearch = require("algoliasearch");
const dotenv = require("dotenv");
dotenv.config();
(async () => {
try {
// Algolia client credentials
const ALGOLIA_APP_ID = process.env.ALGOLIA_APP_ID;
const ALGOLIA_API_KEY = process.env.ALGOLIA_API_KEY;
const ALGOLIA_INDEX_NAME = process.env.ALGOLIA_INDEX_NAME;
// Initialize the client
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
const client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_API_KEY);
// Initialize an index
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
const contacts = [
{
name: "Foo",
objectID: "1",
},
{
name: "Bar",
objectID: "2",
},
];
// We don't have any objects (yet) in our index
let res = await index.search("");
console.log("Current objects: ", res.hits);
// Save objects: Add multiple objects to an index
// https://www.algolia.com/doc/api-reference/api-methods/add-objects/?client=javascript
console.log("Save objects - Adding multiple objects: ", contacts);
await index.saveObjects(contacts).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Save objects: replace an existing object with an updated set of attributes
// https://www.algolia.com/doc/api-reference/api-methods/save-objects/?client=javascript
console.log(
"Save objects - Replacing objects' attributes on ",
contacts[0]
);
let newContact = { name: "FooBar", objectID: "1" };
await index.saveObject(newContact).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Partially update objects: update one or more attribute of an existing object
console.log("Save objects - Updating objects attributes on ", contacts[0]);
newContact = { email: "[email protected]", objectID: "1" };
await index.partialUpdateObject(newContact).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Delete objects: remove objects from an index using their objectID
// https://www.algolia.com/doc/api-reference/api-methods/delete-objects/?client=javascript
objectIDToDelete = contacts[0].objectID;
console.log(
`Delete objects - Deleting object with objectID ${objectIDToDelete}`
);
await index.deleteObject(objectIDToDelete).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// 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=javascript
const newContacts = [
{
name: "NewFoo",
objectID: "3",
},
{
name: "NewBar",
objectID: "4",
},
];
console.log(
"Replace all objects - clears all objects and replaces them with ",
newContacts
);
await index.replaceAllObjects(newContacts).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Delete by: remove all objects matching a filter (including geo filters)
// https://www.algolia.com/doc/api-reference/api-methods/delete-by/?client=javascript
console.log("Delete by - Remove all objects matching 'name:NewBar'");
// First, define an attribute to filter
// https://www.algolia.com/doc/api-client/methods/settings/?client=javascript
await index
.setSettings({
attributesForFaceting: ["name"],
})
.wait();
// https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/
await index
.deleteBy({
facetFilters: ["name:newBar"],
})
.wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Get objects: get one or more objects by their objectIDs
// https://www.algolia.com/doc/api-reference/api-methods/get-objects/?client=javascript
const objectID = newContacts[0].objectID;
console.log(`Get Objects - Getting object with objectID "${objectID}"`);
res = await index.getObject(objectID);
console.log("Results: ", res);
// Custom batch: perform several indexing operations in one API call
// https://www.algolia.com/doc/api-reference/api-methods/batch/?client=javascript
const operations = [
{
action: "addObject",
indexName: ALGOLIA_INDEX_NAME,
body: {
name: "BatchedBar",
},
},
{
action: "updateObject",
indexName: ALGOLIA_INDEX_NAME,
body: {
objectID: objectID,
name: "NewBatchedBar",
},
},
];
console.log(`Custom batch - batching ${operations.length} operations`);
await client.multipleBatch(operations).wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
// Clear objects: clear the records of an index without affecting its settings
// https://www.algolia.com/doc/api-reference/api-methods/clear-objects/?client=javascript
console.log(
"Clear objects - clear the records of an index without affecting its settings"
);
await index.clearObjects().wait();
res = await index.search("");
console.log("Current objects: ", res.hits);
} catch (error) {
console.error(error);
}
})();