summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/olivere/elastic/suggester_completion.go
blob: d2b4a326cd23421e5af0df4ea67dcb9096e7d1ef (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// 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.

package elastic

import "errors"

// CompletionSuggester is a fast suggester for e.g. type-ahead completion.
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html
// for more details.
type CompletionSuggester struct {
	Suggester
	name           string
	text           string
	prefix         string
	regex          string
	field          string
	analyzer       string
	size           *int
	shardSize      *int
	contextQueries []SuggesterContextQuery
	payload        interface{}

	fuzzyOptions   *FuzzyCompletionSuggesterOptions
	regexOptions   *RegexCompletionSuggesterOptions
	skipDuplicates *bool
}

// Creates a new completion suggester.
func NewCompletionSuggester(name string) *CompletionSuggester {
	return &CompletionSuggester{
		name: name,
	}
}

func (q *CompletionSuggester) Name() string {
	return q.name
}

func (q *CompletionSuggester) Text(text string) *CompletionSuggester {
	q.text = text
	return q
}

func (q *CompletionSuggester) Prefix(prefix string) *CompletionSuggester {
	q.prefix = prefix
	return q
}

func (q *CompletionSuggester) PrefixWithEditDistance(prefix string, editDistance interface{}) *CompletionSuggester {
	q.prefix = prefix
	q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions().EditDistance(editDistance)
	return q
}

func (q *CompletionSuggester) PrefixWithOptions(prefix string, options *FuzzyCompletionSuggesterOptions) *CompletionSuggester {
	q.prefix = prefix
	q.fuzzyOptions = options
	return q
}

func (q *CompletionSuggester) FuzzyOptions(options *FuzzyCompletionSuggesterOptions) *CompletionSuggester {
	q.fuzzyOptions = options
	return q
}

func (q *CompletionSuggester) Fuzziness(fuzziness interface{}) *CompletionSuggester {
	if q.fuzzyOptions == nil {
		q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions()
	}
	q.fuzzyOptions = q.fuzzyOptions.EditDistance(fuzziness)
	return q
}

func (q *CompletionSuggester) Regex(regex string) *CompletionSuggester {
	q.regex = regex
	return q
}

func (q *CompletionSuggester) RegexWithOptions(regex string, options *RegexCompletionSuggesterOptions) *CompletionSuggester {
	q.regex = regex
	q.regexOptions = options
	return q
}

func (q *CompletionSuggester) RegexOptions(options *RegexCompletionSuggesterOptions) *CompletionSuggester {
	q.regexOptions = options
	return q
}

func (q *CompletionSuggester) SkipDuplicates(skipDuplicates bool) *CompletionSuggester {
	q.skipDuplicates = &skipDuplicates
	return q
}

func (q *CompletionSuggester) Field(field string) *CompletionSuggester {
	q.field = field
	return q
}

func (q *CompletionSuggester) Analyzer(analyzer string) *CompletionSuggester {
	q.analyzer = analyzer
	return q
}

func (q *CompletionSuggester) Size(size int) *CompletionSuggester {
	q.size = &size
	return q
}

func (q *CompletionSuggester) ShardSize(shardSize int) *CompletionSuggester {
	q.shardSize = &shardSize
	return q
}

func (q *CompletionSuggester) ContextQuery(query SuggesterContextQuery) *CompletionSuggester {
	q.contextQueries = append(q.contextQueries, query)
	return q
}

func (q *CompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *CompletionSuggester {
	q.contextQueries = append(q.contextQueries, queries...)
	return q
}

// completionSuggesterRequest is necessary because the order in which
// the JSON elements are routed to Elasticsearch is relevant.
// We got into trouble when using plain maps because the text element
// needs to go before the completion element.
type completionSuggesterRequest struct {
	Text       string      `json:"text,omitempty"`
	Prefix     string      `json:"prefix,omitempty"`
	Regex      string      `json:"regex,omitempty"`
	Completion interface{} `json:"completion,omitempty"`
}

// Source creates the JSON data for the completion suggester.
func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) {
	cs := &completionSuggesterRequest{}

	if q.text != "" {
		cs.Text = q.text
	}
	if q.prefix != "" {
		cs.Prefix = q.prefix
	}
	if q.regex != "" {
		cs.Regex = q.regex
	}

	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:
		src, err := q.contextQueries[0].Source()
		if err != nil {
			return nil, err
		}
		suggester["contexts"] = src
	default:
		ctxq := make(map[string]interface{})
		for _, query := range q.contextQueries {
			src, err := query.Source()
			if err != nil {
				return nil, err
			}
			// Merge the dictionary into ctxq
			m, ok := src.(map[string]interface{})
			if !ok {
				return nil, errors.New("elastic: context query is not a map")
			}
			for k, v := range m {
				ctxq[k] = v
			}
		}
		suggester["contexts"] = ctxq
	}

	// Fuzzy options
	if q.fuzzyOptions != nil {
		src, err := q.fuzzyOptions.Source()
		if err != nil {
			return nil, err
		}
		suggester["fuzzy"] = src
	}

	// Regex options
	if q.regexOptions != nil {
		src, err := q.regexOptions.Source()
		if err != nil {
			return nil, err
		}
		suggester["regex"] = src
	}

	if q.skipDuplicates != nil {
		suggester["skip_duplicates"] = *q.skipDuplicates
	}

	// TODO(oe) Add completion-suggester specific parameters here

	if !includeName {
		return cs, nil
	}

	source := make(map[string]interface{})
	source[q.name] = cs
	return source, nil
}

// -- Fuzzy options --

// FuzzyCompletionSuggesterOptions represents the options for fuzzy completion suggester.
type FuzzyCompletionSuggesterOptions struct {
	editDistance          interface{}
	transpositions        *bool
	minLength             *int
	prefixLength          *int
	unicodeAware          *bool
	maxDeterminizedStates *int
}

// NewFuzzyCompletionSuggesterOptions initializes a new FuzzyCompletionSuggesterOptions instance.
func NewFuzzyCompletionSuggesterOptions() *FuzzyCompletionSuggesterOptions {
	return &FuzzyCompletionSuggesterOptions{}
}

// EditDistance specifies the maximum number of edits, e.g. a number like "1" or "2"
// or a string like "0..2" or ">5". See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/common-options.html#fuzziness
// for details.
func (o *FuzzyCompletionSuggesterOptions) EditDistance(editDistance interface{}) *FuzzyCompletionSuggesterOptions {
	o.editDistance = editDistance
	return o
}

// Transpositions, if set to true, are counted as one change instead of two (defaults to true).
func (o *FuzzyCompletionSuggesterOptions) Transpositions(transpositions bool) *FuzzyCompletionSuggesterOptions {
	o.transpositions = &transpositions
	return o
}

// MinLength represents the minimum length of the input before fuzzy suggestions are returned (defaults to 3).
func (o *FuzzyCompletionSuggesterOptions) MinLength(minLength int) *FuzzyCompletionSuggesterOptions {
	o.minLength = &minLength
	return o
}

// PrefixLength represents the minimum length of the input, which is not checked for
// fuzzy alternatives (defaults to 1).
func (o *FuzzyCompletionSuggesterOptions) PrefixLength(prefixLength int) *FuzzyCompletionSuggesterOptions {
	o.prefixLength = &prefixLength
	return o
}

// UnicodeAware, if true, all measurements (like fuzzy edit distance, transpositions, and lengths)
// are measured in Unicode code points instead of in bytes. This is slightly slower than
// raw bytes, so it is set to false by default.
func (o *FuzzyCompletionSuggesterOptions) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggesterOptions {
	o.unicodeAware = &unicodeAware
	return o
}

// MaxDeterminizedStates is currently undocumented in Elasticsearch. It represents
// the maximum automaton states allowed for fuzzy expansion.
func (o *FuzzyCompletionSuggesterOptions) MaxDeterminizedStates(max int) *FuzzyCompletionSuggesterOptions {
	o.maxDeterminizedStates = &max
	return o
}

// Source creates the JSON data.
func (o *FuzzyCompletionSuggesterOptions) Source() (interface{}, error) {
	out := make(map[string]interface{})

	if o.editDistance != nil {
		out["fuzziness"] = o.editDistance
	}
	if o.transpositions != nil {
		out["transpositions"] = *o.transpositions
	}
	if o.minLength != nil {
		out["min_length"] = *o.minLength
	}
	if o.prefixLength != nil {
		out["prefix_length"] = *o.prefixLength
	}
	if o.unicodeAware != nil {
		out["unicode_aware"] = *o.unicodeAware
	}
	if o.maxDeterminizedStates != nil {
		out["max_determinized_states"] = *o.maxDeterminizedStates
	}

	return out, nil
}

// -- Regex options --

// RegexCompletionSuggesterOptions represents the options for regex completion suggester.
type RegexCompletionSuggesterOptions struct {
	flags                 interface{} // string or int
	maxDeterminizedStates *int
}

// NewRegexCompletionSuggesterOptions initializes a new RegexCompletionSuggesterOptions instance.
func NewRegexCompletionSuggesterOptions() *RegexCompletionSuggesterOptions {
	return &RegexCompletionSuggesterOptions{}
}

// Flags represents internal regex flags. See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex
// for details.
func (o *RegexCompletionSuggesterOptions) Flags(flags interface{}) *RegexCompletionSuggesterOptions {
	o.flags = flags
	return o
}

// MaxDeterminizedStates represents the maximum automaton states allowed for regex expansion.
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex
// for details.
func (o *RegexCompletionSuggesterOptions) MaxDeterminizedStates(max int) *RegexCompletionSuggesterOptions {
	o.maxDeterminizedStates = &max
	return o
}

// Source creates the JSON data.
func (o *RegexCompletionSuggesterOptions) Source() (interface{}, error) {
	out := make(map[string]interface{})

	if o.flags != nil {
		out["flags"] = o.flags
	}
	if o.maxDeterminizedStates != nil {
		out["max_determinized_states"] = *o.maxDeterminizedStates
	}

	return out, nil
}