Skip to content

Commit c886255

Browse files
committed
feat(go): guides
1 parent b8862f1 commit c886255

14 files changed

+509
-3
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func deleteMultipleIndices() {
12+
// You need an API key with `deleteIndex`
13+
{{> snippets/init}}
14+
15+
// List all indices
16+
indices, err := {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}}
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
// Primary indices don't have a `primary` key
22+
primaryIndices := make([]search.FetchedIndex, len(indices.Items))
23+
replicaIndices := make([]search.FetchedIndex, len(indices.Items))
24+
25+
for _, index := range indices.Items {
26+
if index.Primary == nil {
27+
primaryIndices = append(primaryIndices, index)
28+
} else {
29+
replicaIndices = append(replicaIndices, index)
30+
}
31+
}
32+
33+
// Delete primary indices first
34+
if len(primaryIndices) > 0 {
35+
requests := make([]search.MultipleBatchRequest, len(primaryIndices))
36+
37+
for _, index := range primaryIndices {
38+
requests = append(requests, search.MultipleBatchRequest{
39+
Action: "delete",
40+
IndexName: index.Name,
41+
})
42+
}
43+
44+
_, err = {{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}}
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
fmt.Println("Deleted primary indices.")
50+
}
51+
52+
// Now, delete replica indices
53+
if len(replicaIndices) > 0 {
54+
requests := make([]search.MultipleBatchRequest, len(primaryIndices))
55+
56+
for _, index := range primaryIndices {
57+
requests = append(requests, search.MultipleBatchRequest{
58+
Action: "delete",
59+
IndexName: index.Name,
60+
})
61+
}
62+
63+
_, err = {{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}}
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
fmt.Println("Deleted replica indices.")
69+
}
70+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
{{> snippets/import}}
11+
12+
func saveObjectsChunks() {
13+
{{> snippets/init}}
14+
15+
content, err := os.ReadFile("actors.json")
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
var records []map[string]any
21+
22+
err = json.Unmarshal(content, &records)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
chunkSize := 10000
28+
29+
for beginIndex := 0; beginIndex < len(records); beginIndex += chunkSize {
30+
chunk := records[beginIndex:min(beginIndex+chunkSize, len(records))]
31+
32+
_, err = {{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}}
33+
if err != nil {
34+
panic(err)
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func getAllAppIDConfigurations() ([]struct{ appID, apiKey string }, error) {
12+
return []struct{ appID, apiKey string }{ /* A list of your MCM AppID/ApiKey pairs */ }, nil
13+
}
14+
15+
func saveObjectsMCM() {
16+
playlists := []map[string]any{ { /* Your records */ } }
17+
18+
// Fetch from your own data storage and with your own code
19+
// the list of application IDs and API keys to target each cluster
20+
configurations, err := getAllAppIDConfigurations()
21+
if err != nil {
22+
fmt.Println(err)
23+
return
24+
}
25+
26+
// Send the records to each cluster
27+
for _, configuration := range configurations {
28+
client, err := search.NewClient(configuration.appID, configuration.apiKey)
29+
if err != nil {
30+
fmt.Println(err)
31+
return
32+
}
33+
34+
_, err = {{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}}
35+
if err != nil {
36+
panic(err)
37+
}
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
{{> snippets/import}}
11+
12+
func saveObjectsModified() {
13+
{{> snippets/init}}
14+
15+
content, err := os.ReadFile("products.json")
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
var products []map[string]any
21+
22+
err = json.Unmarshal(content, &products)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
records := make([]map[string]any, len(products))
28+
29+
for _, product := range products {
30+
reference := product["product_reference"].(string)
31+
suffixes := make([]string, len(reference)-1)
32+
33+
for i := len(reference); i > 1; i-- {
34+
suffixes = append(suffixes, reference[i:])
35+
}
36+
37+
record := product
38+
record["product_reference_suffixes"] = suffixes
39+
40+
records = append(records, record)
41+
}
42+
43+
_, err = {{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
44+
if err != nil {
45+
panic(err)
46+
}
47+
}

templates/go/guides/search/saveObjectsMovies.mustache

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ func saveObjectsMovies() {
3535
// push data to algolia
3636
result, err := {{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
3737
if err != nil {
38-
fmt.Println(err)
39-
return
38+
panic(err)
4039
}
4140

42-
fmt.Printf("Done! Uploaded records in %d batches.", len(result))
41+
fmt.Printf("Done! Uploaded records in %d batches.", len(result))
4342
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func saveObjectsPublicUser() {
12+
playlists := []map[string]any{ { /* Your records */ } }
13+
14+
{{> snippets/init}}
15+
16+
_, err = {{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}}
17+
if err != nil {
18+
panic(err)
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func savePopularRecords() {
12+
{{> snippets/init}}
13+
14+
records := []map[string]any{ { /* Your records */ } }
15+
16+
err = client.BrowseObjects("<YOUR_INDEX_NAME>", search.BrowseParamsObject{}, search.WithAggregator(func(res any, err error) {
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
browseRes, ok := res.(search.BrowseResponse)
22+
if !ok {
23+
panic("Invalid response")
24+
}
25+
26+
for _, hit := range browseRes.Hits {
27+
record := hit.AdditionalProperties
28+
record["isPopular"] = record["nbFollowers"].(int) >= 1_000_000
29+
30+
records = append(records, record)
31+
}
32+
}))
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
_, err = {{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
38+
if err != nil {
39+
panic(err)
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
)
9+
10+
{{> snippets/import}}
11+
12+
func searchRecentlyPublishedBooks() {
13+
{{> snippets/init}}
14+
15+
dateTimestamp := time.Now().UnixMilli()
16+
searchParams := search.SearchParamsObjectAsSearchParams(
17+
search.NewSearchParamsObject().
18+
SetQuery("<YOUR_SEARCH_QUERY>").
19+
SetFilters(fmt.Sprintf("date_timestamp > %d", dateTimestamp)),
20+
)
21+
22+
_, err = {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
23+
if err != nil {
24+
panic(err)
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func getGoogleAnalyticsUserIdFromBrowserCookie(_ string) (string, error) {
12+
return "", nil // Implement your logic here
13+
}
14+
15+
func searchWithGAToken() {
16+
{{> snippets/init}}
17+
18+
userToken, err := getGoogleAnalyticsUserIdFromBrowserCookie("_ga")
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
searchParams := search.SearchParamsObjectAsSearchParams(
24+
search.NewSearchParamsObject().
25+
SetQuery("<YOUR_SEARCH_QUERY>").
26+
SetUserToken(userToken),
27+
)
28+
29+
_, err = {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
var loggedInUser *string
35+
36+
if loggedInUser != nil {
37+
searchParams = search.SearchParamsObjectAsSearchParams(searchParams.SearchParamsObject.SetUserToken(*loggedInUser))
38+
}
39+
40+
_, err = {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
41+
if err != nil {
42+
panic(err)
43+
}
44+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
{{> snippets/import}}
10+
11+
func reduceLabelsToFilters(_ []string) (search.OptionalFilters, error) {
12+
return search.OptionalFilters{}, nil // Implement your logic here
13+
}
14+
15+
func searchWithOptionalFilters() {
16+
labels := []string{ /* A list of labels */ }
17+
18+
{{> snippets/init}}
19+
20+
optionalFilters, err := reduceLabelsToFilters(labels)
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
searchParams := search.SearchParamsObjectAsSearchParams(
26+
search.NewSearchParamsObject().
27+
SetQuery("<YOUR_SEARCH_QUERY>").
28+
SetOptionalFilters(&optionalFilters),
29+
)
30+
31+
_, err = {{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
32+
if err != nil {
33+
panic(err)
34+
}
35+
}

0 commit comments

Comments
 (0)