-
Notifications
You must be signed in to change notification settings - Fork 51
/
indexing.go
233 lines (210 loc) · 6.43 KB
/
indexing.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"fmt"
"log"
"os"
"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/algolia/algoliasearch-client-go/v3/algolia/search"
"github.com/joho/godotenv"
)
type Contact struct {
ObjectID string `json:"objectID,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
func PrintErrAndExit(err error) {
fmt.Println(err)
os.Exit(1)
}
func PrintCurrentObjects(i search.Index) {
resSearch, err := i.Search("")
if err != nil {
PrintErrAndExit(err)
}
fmt.Println("Current objects: ", resSearch.Hits, "\n")
}
func main() {
if err := godotenv.Load(); err != nil {
log.Fatalf("godotenv.Load: %v", err)
}
// Algolia client credentials
appID, apiKey, indexName := os.Getenv("ALGOLIA_APP_ID"), os.Getenv("ALGOLIA_API_KEY"), os.Getenv("ALGOLIA_INDEX_NAME")
// Initialize the client
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/
client := search.NewClient(appID, apiKey)
// Initialize an index
// https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/#initialize-an-index
index := client.InitIndex(indexName)
// Define some objects to add to our index
// https://www.algolia.com/doc/api-client/methods/indexing/#object-and-record
contacts := []Contact{
{
Name: "Foo",
ObjectID: "1",
},
{
Name: "Bar",
ObjectID: "2",
},
}
// We don't have any objects (yet) in our index
PrintCurrentObjects(*index)
// Save Objects: Add mutliple new objects to an index.
// https://www.algolia.com/doc/api-reference/api-methods/add-objects/?client=go
fmt.Println("Save Objects - Adding multiple objects: ", contacts)
resSaveMultiple, err := index.SaveObjects(contacts)
if err != nil {
PrintErrAndExit(err)
}
err = resSaveMultiple.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// Save Objects: Replace an existing object with an updated set of attributes.
// https://www.algolia.com/doc/api-reference/api-methods/save-objects/?client=go
fmt.Println("Save Objects - Replacing objects’s attributes on:", contacts[0])
newContact := Contact{
Name: "FooBar",
ObjectID: "1",
}
resSaveSingle, err := index.SaveObject(newContact)
if err != nil {
PrintErrAndExit(err)
}
err = resSaveSingle.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// 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=go
fmt.Println("Partial Update Objects - Updating object’s attributes on:", contacts[0])
newContact = Contact{
Email: "[email protected]", // New attribute
ObjectID: "1",
}
resPartialUpdate, err := index.PartialUpdateObject(newContact)
if err != nil {
PrintErrAndExit(err)
}
err = resPartialUpdate.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// Delete Objects: Remove objects from an index using their objectID.
// https://www.algolia.com/doc/api-reference/api-methods/delete-objects/?client=go
objectIDToDelete := contacts[0].ObjectID
fmt.Println("Delete Objects - Deleting object with objectID:", objectIDToDelete)
resDelete, err := index.DeleteObject(objectIDToDelete)
if err != nil {
PrintErrAndExit(err)
}
err = resDelete.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// 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=go
newContacts := []Contact{
{
Name: "NewFoo",
ObjectID: "3",
},
{
Name: "NewBar",
ObjectID: "4",
},
}
fmt.Println("Replace All Objects - Clears all objects and replaces them with:", newContacts)
resReplaceAll, err := index.ReplaceAllObjects(newContacts)
if err != nil {
PrintErrAndExit(err)
}
err = resReplaceAll.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// Delete By: Remove all objects matching a filter (including geo filters).
// https://www.algolia.com/doc/api-reference/api-methods/delete-by/?client=go
fmt.Println("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=go
resSetSettings, err := index.SetSettings(search.Settings{
AttributesForFaceting: opt.AttributesForFaceting("name"),
})
if err != nil {
PrintErrAndExit(err)
}
err = resSetSettings.Wait()
if err != nil {
PrintErrAndExit(err)
}
resDeleteBy, err := index.DeleteBy(opt.Filters("name:NewBar")) // https://www.algolia.com/doc/api-reference/api-parameters/filters/
if err != nil {
PrintErrAndExit(err)
}
err = resDeleteBy.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// Get Objects: Get one or more objects using their objectIDs.
// https://www.algolia.com/doc/api-reference/api-methods/get-objects/?client=go
objectID := newContacts[0].ObjectID
fmt.Println("Get Objects - Getting object with objectID:", objectID)
var retrievedContact Contact
err = index.GetObject(objectID, &retrievedContact)
if err != nil {
PrintErrAndExit(err)
}
fmt.Println("Results: ", retrievedContact)
// Custom Batch: Perform several indexing operations in one API call.
// https://www.algolia.com/doc/api-reference/api-methods/batch/?client=go
operations := []search.BatchOperationIndexed{
{
IndexName: indexName,
BatchOperation: search.BatchOperation{
Action: search.AddObject,
Body: Contact{
Name: "BatchedBar",
},
},
},
{
IndexName: indexName,
BatchOperation: search.BatchOperation{
Action: search.UpdateObject,
Body: Contact{
ObjectID: objectID,
Name: "NewBatchedBar",
},
},
},
}
fmt.Println("Custom Batch - Batching operations:", operations)
resBatch, err := client.MultipleBatch(operations)
if err != nil {
PrintErrAndExit(err)
}
err = resBatch.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
// Clear Objects: Clear the records of an index without affecting its settings.
// https://www.algolia.com/doc/api-reference/api-methods/clear-objects/?client=go
fmt.Println("Clear Objects: Clear the records of an index without affecting its settings.")
resClear, err := index.ClearObjects()
if err != nil {
PrintErrAndExit(err)
}
err = resClear.Wait()
if err != nil {
PrintErrAndExit(err)
}
PrintCurrentObjects(*index)
}