| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import "errors" |
| 8 |
|
| 9 |
// CompletionSuggester is a fast suggester for e.g. type-ahead completion. |
| 10 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html |
| 11 |
// for more details. |
| 12 |
type CompletionSuggester struct { |
| 13 |
Suggester |
| 14 |
name string |
| 15 |
text string |
| 16 |
prefix string |
| 17 |
regex string |
| 18 |
field string |
| 19 |
analyzer string |
| 20 |
size *int |
| 21 |
shardSize *int |
| 22 |
contextQueries []SuggesterContextQuery |
| 23 |
payload interface{} |
| 24 |
|
| 25 |
fuzzyOptions *FuzzyCompletionSuggesterOptions |
| 26 |
regexOptions *RegexCompletionSuggesterOptions |
| 27 |
} |
| 28 |
|
| 29 |
// Creates a new completion suggester. |
| 30 |
func NewCompletionSuggester(name string) *CompletionSuggester { |
| 31 |
return &CompletionSuggester{ |
| 32 |
name: name, |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func (q *CompletionSuggester) Name() string { |
| 37 |
return q.name |
| 38 |
} |
| 39 |
|
| 40 |
func (q *CompletionSuggester) Text(text string) *CompletionSuggester { |
| 41 |
q.text = text |
| 42 |
return q |
| 43 |
} |
| 44 |
|
| 45 |
func (q *CompletionSuggester) Prefix(prefix string) *CompletionSuggester { |
| 46 |
q.prefix = prefix |
| 47 |
return q |
| 48 |
} |
| 49 |
|
| 50 |
func (q *CompletionSuggester) PrefixWithEditDistance(prefix string, editDistance interface{}) *CompletionSuggester { |
| 51 |
q.prefix = prefix |
| 52 |
q.fuzzyOptions = NewFuzzyCompletionSuggesterOptions().EditDistance(editDistance) |
| 53 |
return q |
| 54 |
} |
| 55 |
|
| 56 |
func (q *CompletionSuggester) PrefixWithOptions(prefix string, options *FuzzyCompletionSuggesterOptions) *CompletionSuggester { |
| 57 |
q.prefix = prefix |
| 58 |
q.fuzzyOptions = options |
| 59 |
return q |
| 60 |
} |
| 61 |
|
| 62 |
func (q *CompletionSuggester) FuzzyOptions(options *FuzzyCompletionSuggesterOptions) *CompletionSuggester { |
| 63 |
q.fuzzyOptions = options |
| 64 |
return q |
| 65 |
} |
| 66 |
|
| 67 |
func (q *CompletionSuggester) Regex(regex string) *CompletionSuggester { |
| 68 |
q.regex = regex |
| 69 |
return q |
| 70 |
} |
| 71 |
|
| 72 |
func (q *CompletionSuggester) RegexWithOptions(regex string, options *RegexCompletionSuggesterOptions) *CompletionSuggester { |
| 73 |
q.regex = regex |
| 74 |
q.regexOptions = options |
| 75 |
return q |
| 76 |
} |
| 77 |
|
| 78 |
func (q *CompletionSuggester) RegexOptions(options *RegexCompletionSuggesterOptions) *CompletionSuggester { |
| 79 |
q.regexOptions = options |
| 80 |
return q |
| 81 |
} |
| 82 |
|
| 83 |
func (q *CompletionSuggester) Field(field string) *CompletionSuggester { |
| 84 |
q.field = field |
| 85 |
return q |
| 86 |
} |
| 87 |
|
| 88 |
func (q *CompletionSuggester) Analyzer(analyzer string) *CompletionSuggester { |
| 89 |
q.analyzer = analyzer |
| 90 |
return q |
| 91 |
} |
| 92 |
|
| 93 |
func (q *CompletionSuggester) Size(size int) *CompletionSuggester { |
| 94 |
q.size = &size |
| 95 |
return q |
| 96 |
} |
| 97 |
|
| 98 |
func (q *CompletionSuggester) ShardSize(shardSize int) *CompletionSuggester { |
| 99 |
q.shardSize = &shardSize |
| 100 |
return q |
| 101 |
} |
| 102 |
|
| 103 |
func (q *CompletionSuggester) ContextQuery(query SuggesterContextQuery) *CompletionSuggester { |
| 104 |
q.contextQueries = append(q.contextQueries, query) |
| 105 |
return q |
| 106 |
} |
| 107 |
|
| 108 |
func (q *CompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *CompletionSuggester { |
| 109 |
q.contextQueries = append(q.contextQueries, queries...) |
| 110 |
return q |
| 111 |
} |
| 112 |
|
| 113 |
// completionSuggesterRequest is necessary because the order in which |
| 114 |
// the JSON elements are routed to Elasticsearch is relevant. |
| 115 |
// We got into trouble when using plain maps because the text element |
| 116 |
// needs to go before the completion element. |
| 117 |
type completionSuggesterRequest struct { |
| 118 |
Text string `json:"text,omitempty"` |
| 119 |
Prefix string `json:"prefix,omitempty"` |
| 120 |
Regex string `json:"regex,omitempty"` |
| 121 |
Completion interface{} `json:"completion,omitempty"` |
| 122 |
} |
| 123 |
|
| 124 |
// Source creates the JSON data for the completion suggester. |
| 125 |
func (q *CompletionSuggester) Source(includeName bool) (interface{}, error) { |
| 126 |
cs := &completionSuggesterRequest{} |
| 127 |
|
| 128 |
if q.text != "" { |
| 129 |
cs.Text = q.text |
| 130 |
} |
| 131 |
if q.prefix != "" { |
| 132 |
cs.Prefix = q.prefix |
| 133 |
} |
| 134 |
if q.regex != "" { |
| 135 |
cs.Regex = q.regex |
| 136 |
} |
| 137 |
|
| 138 |
suggester := make(map[string]interface{}) |
| 139 |
cs.Completion = suggester |
| 140 |
|
| 141 |
if q.analyzer != "" { |
| 142 |
suggester["analyzer"] = q.analyzer |
| 143 |
} |
| 144 |
if q.field != "" { |
| 145 |
suggester["field"] = q.field |
| 146 |
} |
| 147 |
if q.size != nil { |
| 148 |
suggester["size"] = *q.size |
| 149 |
} |
| 150 |
if q.shardSize != nil { |
| 151 |
suggester["shard_size"] = *q.shardSize |
| 152 |
} |
| 153 |
switch len(q.contextQueries) { |
| 154 |
case 0: |
| 155 |
case 1: |
| 156 |
src, err := q.contextQueries[0].Source() |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
suggester["context"] = src |
| 161 |
default: |
| 162 |
ctxq := make(map[string]interface{}) |
| 163 |
for _, query := range q.contextQueries { |
| 164 |
src, err := query.Source() |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
// Merge the dictionary into ctxq |
| 169 |
m, ok := src.(map[string]interface{}) |
| 170 |
if !ok { |
| 171 |
return nil, errors.New("elastic: context query is not a map") |
| 172 |
} |
| 173 |
for k, v := range m { |
| 174 |
ctxq[k] = v |
| 175 |
} |
| 176 |
} |
| 177 |
suggester["contexts"] = ctxq |
| 178 |
} |
| 179 |
|
| 180 |
// Fuzzy options |
| 181 |
if q.fuzzyOptions != nil { |
| 182 |
src, err := q.fuzzyOptions.Source() |
| 183 |
if err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
suggester["fuzzy"] = src |
| 187 |
} |
| 188 |
|
| 189 |
// Regex options |
| 190 |
if q.regexOptions != nil { |
| 191 |
src, err := q.regexOptions.Source() |
| 192 |
if err != nil { |
| 193 |
return nil, err |
| 194 |
} |
| 195 |
suggester["regex"] = src |
| 196 |
} |
| 197 |
|
| 198 |
// TODO(oe) Add completion-suggester specific parameters here |
| 199 |
|
| 200 |
if !includeName { |
| 201 |
return cs, nil |
| 202 |
} |
| 203 |
|
| 204 |
source := make(map[string]interface{}) |
| 205 |
source[q.name] = cs |
| 206 |
return source, nil |
| 207 |
} |
| 208 |
|
| 209 |
// -- Fuzzy options -- |
| 210 |
|
| 211 |
// FuzzyCompletionSuggesterOptions represents the options for fuzzy completion suggester. |
| 212 |
type FuzzyCompletionSuggesterOptions struct { |
| 213 |
editDistance interface{} |
| 214 |
transpositions *bool |
| 215 |
minLength *int |
| 216 |
prefixLength *int |
| 217 |
unicodeAware *bool |
| 218 |
maxDeterminizedStates *int |
| 219 |
} |
| 220 |
|
| 221 |
// NewFuzzyCompletionSuggesterOptions initializes a new FuzzyCompletionSuggesterOptions instance. |
| 222 |
func NewFuzzyCompletionSuggesterOptions() *FuzzyCompletionSuggesterOptions { |
| 223 |
return &FuzzyCompletionSuggesterOptions{} |
| 224 |
} |
| 225 |
|
| 226 |
// EditDistance specifies the maximum number of edits, e.g. a number like "1" or "2" |
| 227 |
// or a string like "0..2" or ">5". See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/common-options.html#fuzziness |
| 228 |
// for details. |
| 229 |
func (o *FuzzyCompletionSuggesterOptions) EditDistance(editDistance interface{}) *FuzzyCompletionSuggesterOptions { |
| 230 |
o.editDistance = editDistance |
| 231 |
return o |
| 232 |
} |
| 233 |
|
| 234 |
// Transpositions, if set to true, are counted as one change instead of two (defaults to true). |
| 235 |
func (o *FuzzyCompletionSuggesterOptions) Transpositions(transpositions bool) *FuzzyCompletionSuggesterOptions { |
| 236 |
o.transpositions = &transpositions |
| 237 |
return o |
| 238 |
} |
| 239 |
|
| 240 |
// MinLength represents the minimum length of the input before fuzzy suggestions are returned (defaults to 3). |
| 241 |
func (o *FuzzyCompletionSuggesterOptions) MinLength(minLength int) *FuzzyCompletionSuggesterOptions { |
| 242 |
o.minLength = &minLength |
| 243 |
return o |
| 244 |
} |
| 245 |
|
| 246 |
// PrefixLength represents the minimum length of the input, which is not checked for |
| 247 |
// fuzzy alternatives (defaults to 1). |
| 248 |
func (o *FuzzyCompletionSuggesterOptions) PrefixLength(prefixLength int) *FuzzyCompletionSuggesterOptions { |
| 249 |
o.prefixLength = &prefixLength |
| 250 |
return o |
| 251 |
} |
| 252 |
|
| 253 |
// UnicodeAware, if true, all measurements (like fuzzy edit distance, transpositions, and lengths) |
| 254 |
// are measured in Unicode code points instead of in bytes. This is slightly slower than |
| 255 |
// raw bytes, so it is set to false by default. |
| 256 |
func (o *FuzzyCompletionSuggesterOptions) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggesterOptions { |
| 257 |
o.unicodeAware = &unicodeAware |
| 258 |
return o |
| 259 |
} |
| 260 |
|
| 261 |
// MaxDeterminizedStates is currently undocumented in Elasticsearch. It represents |
| 262 |
// the maximum automaton states allowed for fuzzy expansion. |
| 263 |
func (o *FuzzyCompletionSuggesterOptions) MaxDeterminizedStates(max int) *FuzzyCompletionSuggesterOptions { |
| 264 |
o.maxDeterminizedStates = &max |
| 265 |
return o |
| 266 |
} |
| 267 |
|
| 268 |
// Source creates the JSON data. |
| 269 |
func (o *FuzzyCompletionSuggesterOptions) Source() (interface{}, error) { |
| 270 |
out := make(map[string]interface{}) |
| 271 |
|
| 272 |
if o.editDistance != nil { |
| 273 |
out["fuzziness"] = o.editDistance |
| 274 |
} |
| 275 |
if o.transpositions != nil { |
| 276 |
out["transpositions"] = *o.transpositions |
| 277 |
} |
| 278 |
if o.minLength != nil { |
| 279 |
out["min_length"] = *o.minLength |
| 280 |
} |
| 281 |
if o.prefixLength != nil { |
| 282 |
out["prefix_length"] = *o.prefixLength |
| 283 |
} |
| 284 |
if o.unicodeAware != nil { |
| 285 |
out["unicode_aware"] = *o.unicodeAware |
| 286 |
} |
| 287 |
if o.maxDeterminizedStates != nil { |
| 288 |
out["max_determinized_states"] = *o.maxDeterminizedStates |
| 289 |
} |
| 290 |
|
| 291 |
return out, nil |
| 292 |
} |
| 293 |
|
| 294 |
// -- Regex options -- |
| 295 |
|
| 296 |
// RegexCompletionSuggesterOptions represents the options for regex completion suggester. |
| 297 |
type RegexCompletionSuggesterOptions struct { |
| 298 |
flags interface{} // string or int |
| 299 |
maxDeterminizedStates *int |
| 300 |
} |
| 301 |
|
| 302 |
// NewRegexCompletionSuggesterOptions initializes a new RegexCompletionSuggesterOptions instance. |
| 303 |
func NewRegexCompletionSuggesterOptions() *RegexCompletionSuggesterOptions { |
| 304 |
return &RegexCompletionSuggesterOptions{} |
| 305 |
} |
| 306 |
|
| 307 |
// Flags represents internal regex flags. See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex |
| 308 |
// for details. |
| 309 |
func (o *RegexCompletionSuggesterOptions) Flags(flags interface{}) *RegexCompletionSuggesterOptions { |
| 310 |
o.flags = flags |
| 311 |
return o |
| 312 |
} |
| 313 |
|
| 314 |
// MaxDeterminizedStates represents the maximum automaton states allowed for regex expansion. |
| 315 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html#regex |
| 316 |
// for details. |
| 317 |
func (o *RegexCompletionSuggesterOptions) MaxDeterminizedStates(max int) *RegexCompletionSuggesterOptions { |
| 318 |
o.maxDeterminizedStates = &max |
| 319 |
return o |
| 320 |
} |
| 321 |
|
| 322 |
// Source creates the JSON data. |
| 323 |
func (o *RegexCompletionSuggesterOptions) Source() (interface{}, error) { |
| 324 |
out := make(map[string]interface{}) |
| 325 |
|
| 326 |
if o.flags != nil { |
| 327 |
out["flags"] = o.flags |
| 328 |
} |
| 329 |
if o.maxDeterminizedStates != nil { |
| 330 |
out["max_determinized_states"] = *o.maxDeterminizedStates |
| 331 |
} |
| 332 |
|
| 333 |
return out, nil |
| 334 |
} |