-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (170 loc) · 3.73 KB
/
main.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
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
// Connect creates an index with a mapping with different data types.
//
// Example
//
//
// ./completion -url=http://127.0.0.1:9200 -index=cities
//
// For more details and experimentation, take a look at the official
// documentation at https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-suggesters-completion.html.
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/olivere/elastic"
)
const (
mapping = `
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"_doc":{
"properties":{
"name":{
"type":"keyword"
},
"name_suggest":{
"type":"completion"
}
}
}
}
}
`
)
// City is used in this example as an index document for suggestions.
type City struct {
Name string `json:"name,omitempty"`
NameSuggest *elastic.SuggestField `json:"name_suggest,omitempty"`
}
var (
cities = []string{
"Amsterdam",
"Athens",
"Berlin",
"Barcelona",
"Brussels",
"Dublin",
"Helsinki",
"Madrid",
"Lisbon",
"London",
"Oslo",
"Paris",
"Stockholm",
"Rome",
"Valetta",
"Vienna",
}
)
func main() {
var (
url = flag.String("url", "http://localhost:9200", "Elasticsearch URL")
sniff = flag.Bool("sniff", true, "Enable or disable sniffing")
index = flag.String("index", "", "Index name")
)
flag.Parse()
log.SetFlags(0)
if *url == "" {
*url = "http://127.0.0.1:9200"
}
if *index == "" {
log.Fatal("please specify an index name -index")
}
// Create an Elasticsearch client
client, err := elastic.NewClient(
elastic.SetURL(*url),
elastic.SetSniff(*sniff),
// elastic.SetTraceLog(log.New(os.Stdout, "", 0)), // uncomment to see the wire protocol
)
if err != nil {
log.Fatal(err)
}
_ = client
// Check if index already exists. We'll drop it then.
// Next, we create a fresh index/mapping.
ctx := context.Background()
exists, err := client.IndexExists(*index).Do(ctx)
if err != nil {
log.Fatal(err)
}
if exists {
_, err := client.DeleteIndex(*index).Do(ctx)
if err != nil {
log.Fatal(err)
}
}
_, err = client.CreateIndex(*index).Body(mapping).Do(ctx)
if err != nil {
log.Fatal(err)
}
// Add some cities
for _, name := range cities {
fmt.Printf("Add %s...\n", name)
_, err := client.Index().
Index(*index).
Type("_doc").
BodyJson(&City{
Name: name,
NameSuggest: elastic.NewSuggestField(name),
}).
Refresh("true").
Do(context.Background())
if err != nil {
log.Fatal(err)
}
}
// Allow suggestions
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter a city name: ")
name, _ := reader.ReadString('\n')
name = strings.TrimRight(name, "\n")
if name == "" {
break
}
res, err := client.Search().
Index(*index).
Type("_doc").
Suggester(
elastic.NewCompletionSuggester("name_suggestion").
Field("name_suggest").
Text(name),
).
Size(0). // we dont' want the hits, just the suggestions
Pretty(true).
Do(context.Background())
if err != nil {
log.Fatal(err)
}
suggestions, found := res.Suggest["name_suggestion"]
if !found {
fmt.Printf("%d matches found:\n", 0)
continue
}
for _, suggestion := range suggestions {
fmt.Printf("%d suggestions found for text %q:\n", len(suggestion.Options), suggestion.Text)
for _, opt := range suggestion.Options {
fmt.Printf("* %s\n", opt.Text)
// The document's source is in opt.Source
var city City
if err = json.Unmarshal(*opt.Source, &city); err != nil {
log.Fatal(err)
}
_ = city
}
}
}
}