-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsearch.go
79 lines (73 loc) · 1.75 KB
/
search.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
package logic
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os4gophers/domain"
"strings"
"github.com/opensearch-project/opensearch-go"
)
func SearchBestMatrixMovies(ctx context.Context, opensearchClient *opensearch.Client) {
var searchBuffer bytes.Buffer
search := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": map[string]interface{}{
"match": map[string]string{
"actors.en": "keanu reeves",
},
},
"filter": []map[string]interface{}{
{
"term": map[string]string{
"genres.keyword": "Action",
},
},
{
"range": map[string]interface{}{
"rating": map[string]float64{
"gte": 7.0,
},
},
},
{
"range": map[string]interface{}{
"year": map[string]int{
"gte": 1995,
"lte": 2005,
},
},
},
},
},
},
}
err := json.NewEncoder(&searchBuffer).Encode(search)
if err != nil {
panic(err)
}
response, err := opensearchClient.Search(
opensearchClient.Search.WithContext(ctx),
opensearchClient.Search.WithIndex("movies"),
opensearchClient.Search.WithBody(&searchBuffer),
opensearchClient.Search.WithTrackTotalHits(true),
opensearchClient.Search.WithPretty(),
)
if err != nil {
panic(err)
}
defer response.Body.Close()
var searchResponse = domain.SearchResponse{}
err = json.NewDecoder(response.Body).Decode(&searchResponse)
if err != nil {
panic(err)
}
if searchResponse.Hits.Total.Value > 0 {
var movieTitles []string
for _, movieTitle := range searchResponse.Hits.Hits {
movieTitles = append(movieTitles, movieTitle.Source.Title)
}
fmt.Printf("🟦 Best Matrix movies with Keanu Reeves: [%s] \n", strings.Join(movieTitles, ", "))
}
}