forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggester_completion_fuzzy.go
171 lines (146 loc) · 4.55 KB
/
suggester_completion_fuzzy.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
// Copyright 2012-2015 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.
package elastic
// FuzzyFuzzyCompletionSuggester is a FuzzyCompletionSuggester that allows fuzzy
// completion.
// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
// for details, and
// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#fuzzy
// for details about the fuzzy completion suggester.
type FuzzyCompletionSuggester struct {
Suggester
name string
text string
field string
analyzer string
size *int
shardSize *int
contextQueries []SuggesterContextQuery
fuzziness interface{}
fuzzyTranspositions *bool
fuzzyMinLength *int
fuzzyPrefixLength *int
unicodeAware *bool
}
// Fuzziness defines the fuzziness which is used in FuzzyCompletionSuggester.
type Fuzziness struct {
}
// Creates a new completion suggester.
func NewFuzzyCompletionSuggester(name string) FuzzyCompletionSuggester {
return FuzzyCompletionSuggester{
name: name,
contextQueries: make([]SuggesterContextQuery, 0),
}
}
func (q FuzzyCompletionSuggester) Name() string {
return q.name
}
func (q FuzzyCompletionSuggester) Text(text string) FuzzyCompletionSuggester {
q.text = text
return q
}
func (q FuzzyCompletionSuggester) Field(field string) FuzzyCompletionSuggester {
q.field = field
return q
}
func (q FuzzyCompletionSuggester) Analyzer(analyzer string) FuzzyCompletionSuggester {
q.analyzer = analyzer
return q
}
func (q FuzzyCompletionSuggester) Size(size int) FuzzyCompletionSuggester {
q.size = &size
return q
}
func (q FuzzyCompletionSuggester) ShardSize(shardSize int) FuzzyCompletionSuggester {
q.shardSize = &shardSize
return q
}
func (q FuzzyCompletionSuggester) ContextQuery(query SuggesterContextQuery) FuzzyCompletionSuggester {
q.contextQueries = append(q.contextQueries, query)
return q
}
func (q FuzzyCompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) FuzzyCompletionSuggester {
q.contextQueries = append(q.contextQueries, queries...)
return q
}
// Fuzziness defines the strategy used to describe what "fuzzy" actually
// means for the suggester, e.g. 1, 2, "0", "1..2", ">4", or "AUTO".
// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
// for a detailed description.
func (q FuzzyCompletionSuggester) Fuzziness(fuzziness interface{}) FuzzyCompletionSuggester {
q.fuzziness = fuzziness
return q
}
func (q FuzzyCompletionSuggester) FuzzyTranspositions(fuzzyTranspositions bool) FuzzyCompletionSuggester {
q.fuzzyTranspositions = &fuzzyTranspositions
return q
}
func (q FuzzyCompletionSuggester) FuzzyMinLength(minLength int) FuzzyCompletionSuggester {
q.fuzzyMinLength = &minLength
return q
}
func (q FuzzyCompletionSuggester) FuzzyPrefixLength(prefixLength int) FuzzyCompletionSuggester {
q.fuzzyPrefixLength = &prefixLength
return q
}
func (q FuzzyCompletionSuggester) UnicodeAware(unicodeAware bool) FuzzyCompletionSuggester {
q.unicodeAware = &unicodeAware
return q
}
// Creates the source for the completion suggester.
func (q FuzzyCompletionSuggester) Source(includeName bool) interface{} {
cs := &completionSuggesterRequest{}
if q.text != "" {
cs.Text = q.text
}
suggester := make(map[string]interface{})
cs.Completion = suggester
if q.analyzer != "" {
suggester["analyzer"] = q.analyzer
}
if q.field != "" {
suggester["field"] = q.field
}
if q.size != nil {
suggester["size"] = *q.size
}
if q.shardSize != nil {
suggester["shard_size"] = *q.shardSize
}
switch len(q.contextQueries) {
case 0:
case 1:
suggester["context"] = q.contextQueries[0].Source()
default:
ctxq := make([]interface{}, 0)
for _, query := range q.contextQueries {
ctxq = append(ctxq, query.Source())
}
suggester["context"] = ctxq
}
// Fuzzy Completion Suggester fields
fuzzy := make(map[string]interface{})
suggester["fuzzy"] = fuzzy
if q.fuzziness != nil {
fuzzy["fuzziness"] = q.fuzziness
}
if q.fuzzyTranspositions != nil {
fuzzy["transpositions"] = *q.fuzzyTranspositions
}
if q.fuzzyMinLength != nil {
fuzzy["min_length"] = *q.fuzzyMinLength
}
if q.fuzzyPrefixLength != nil {
fuzzy["prefix_length"] = *q.fuzzyPrefixLength
}
if q.unicodeAware != nil {
fuzzy["unicode_aware"] = *q.unicodeAware
}
if !includeName {
return cs
}
source := make(map[string]interface{})
source[q.name] = cs
return source
}