| 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 |
// FuzzyCompletionSuggester is a CompletionSuggester that allows fuzzy |
| 8 |
// completion. |
| 9 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html |
| 10 |
// for details, and |
| 11 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html#fuzzy |
| 12 |
// for details about the fuzzy completion suggester. |
| 13 |
// |
| 14 |
// @Deprecated Use CompletionSuggester with FuzzyOptions instead. |
| 15 |
type FuzzyCompletionSuggester struct { |
| 16 |
Suggester |
| 17 |
name string |
| 18 |
text string |
| 19 |
field string |
| 20 |
analyzer string |
| 21 |
size *int |
| 22 |
shardSize *int |
| 23 |
contextQueries []SuggesterContextQuery |
| 24 |
|
| 25 |
fuzziness interface{} |
| 26 |
fuzzyTranspositions *bool |
| 27 |
fuzzyMinLength *int |
| 28 |
fuzzyPrefixLength *int |
| 29 |
unicodeAware *bool |
| 30 |
} |
| 31 |
|
| 32 |
// Fuzziness defines the fuzziness which is used in FuzzyCompletionSuggester. |
| 33 |
type Fuzziness struct { |
| 34 |
} |
| 35 |
|
| 36 |
// Creates a new completion suggester. |
| 37 |
func NewFuzzyCompletionSuggester(name string) *FuzzyCompletionSuggester { |
| 38 |
return &FuzzyCompletionSuggester{ |
| 39 |
name: name, |
| 40 |
contextQueries: make([]SuggesterContextQuery, 0), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func (q *FuzzyCompletionSuggester) Name() string { |
| 45 |
return q.name |
| 46 |
} |
| 47 |
|
| 48 |
func (q *FuzzyCompletionSuggester) Text(text string) *FuzzyCompletionSuggester { |
| 49 |
q.text = text |
| 50 |
return q |
| 51 |
} |
| 52 |
|
| 53 |
func (q *FuzzyCompletionSuggester) Field(field string) *FuzzyCompletionSuggester { |
| 54 |
q.field = field |
| 55 |
return q |
| 56 |
} |
| 57 |
|
| 58 |
func (q *FuzzyCompletionSuggester) Analyzer(analyzer string) *FuzzyCompletionSuggester { |
| 59 |
q.analyzer = analyzer |
| 60 |
return q |
| 61 |
} |
| 62 |
|
| 63 |
func (q *FuzzyCompletionSuggester) Size(size int) *FuzzyCompletionSuggester { |
| 64 |
q.size = &size |
| 65 |
return q |
| 66 |
} |
| 67 |
|
| 68 |
func (q *FuzzyCompletionSuggester) ShardSize(shardSize int) *FuzzyCompletionSuggester { |
| 69 |
q.shardSize = &shardSize |
| 70 |
return q |
| 71 |
} |
| 72 |
|
| 73 |
func (q *FuzzyCompletionSuggester) ContextQuery(query SuggesterContextQuery) *FuzzyCompletionSuggester { |
| 74 |
q.contextQueries = append(q.contextQueries, query) |
| 75 |
return q |
| 76 |
} |
| 77 |
|
| 78 |
func (q *FuzzyCompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *FuzzyCompletionSuggester { |
| 79 |
q.contextQueries = append(q.contextQueries, queries...) |
| 80 |
return q |
| 81 |
} |
| 82 |
|
| 83 |
// Fuzziness defines the strategy used to describe what "fuzzy" actually |
| 84 |
// means for the suggester, e.g. 1, 2, "0", "1..2", ">4", or "AUTO". |
| 85 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/common-options.html#fuzziness |
| 86 |
// for a detailed description. |
| 87 |
func (q *FuzzyCompletionSuggester) Fuzziness(fuzziness interface{}) *FuzzyCompletionSuggester { |
| 88 |
q.fuzziness = fuzziness |
| 89 |
return q |
| 90 |
} |
| 91 |
|
| 92 |
func (q *FuzzyCompletionSuggester) FuzzyTranspositions(fuzzyTranspositions bool) *FuzzyCompletionSuggester { |
| 93 |
q.fuzzyTranspositions = &fuzzyTranspositions |
| 94 |
return q |
| 95 |
} |
| 96 |
|
| 97 |
func (q *FuzzyCompletionSuggester) FuzzyMinLength(minLength int) *FuzzyCompletionSuggester { |
| 98 |
q.fuzzyMinLength = &minLength |
| 99 |
return q |
| 100 |
} |
| 101 |
|
| 102 |
func (q *FuzzyCompletionSuggester) FuzzyPrefixLength(prefixLength int) *FuzzyCompletionSuggester { |
| 103 |
q.fuzzyPrefixLength = &prefixLength |
| 104 |
return q |
| 105 |
} |
| 106 |
|
| 107 |
func (q *FuzzyCompletionSuggester) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggester { |
| 108 |
q.unicodeAware = &unicodeAware |
| 109 |
return q |
| 110 |
} |
| 111 |
|
| 112 |
// Creates the source for the completion suggester. |
| 113 |
func (q *FuzzyCompletionSuggester) Source(includeName bool) (interface{}, error) { |
| 114 |
cs := &completionSuggesterRequest{} |
| 115 |
|
| 116 |
if q.text != "" { |
| 117 |
cs.Text = q.text |
| 118 |
} |
| 119 |
|
| 120 |
suggester := make(map[string]interface{}) |
| 121 |
cs.Completion = suggester |
| 122 |
|
| 123 |
if q.analyzer != "" { |
| 124 |
suggester["analyzer"] = q.analyzer |
| 125 |
} |
| 126 |
if q.field != "" { |
| 127 |
suggester["field"] = q.field |
| 128 |
} |
| 129 |
if q.size != nil { |
| 130 |
suggester["size"] = *q.size |
| 131 |
} |
| 132 |
if q.shardSize != nil { |
| 133 |
suggester["shard_size"] = *q.shardSize |
| 134 |
} |
| 135 |
switch len(q.contextQueries) { |
| 136 |
case 0: |
| 137 |
case 1: |
| 138 |
src, err := q.contextQueries[0].Source() |
| 139 |
if err != nil { |
| 140 |
return nil, err |
| 141 |
} |
| 142 |
suggester["context"] = src |
| 143 |
default: |
| 144 |
var ctxq []interface{} |
| 145 |
for _, query := range q.contextQueries { |
| 146 |
src, err := query.Source() |
| 147 |
if err != nil { |
| 148 |
return nil, err |
| 149 |
} |
| 150 |
ctxq = append(ctxq, src) |
| 151 |
} |
| 152 |
suggester["context"] = ctxq |
| 153 |
} |
| 154 |
|
| 155 |
// Fuzzy Completion Suggester fields |
| 156 |
fuzzy := make(map[string]interface{}) |
| 157 |
suggester["fuzzy"] = fuzzy |
| 158 |
if q.fuzziness != nil { |
| 159 |
fuzzy["fuzziness"] = q.fuzziness |
| 160 |
} |
| 161 |
if q.fuzzyTranspositions != nil { |
| 162 |
fuzzy["transpositions"] = *q.fuzzyTranspositions |
| 163 |
} |
| 164 |
if q.fuzzyMinLength != nil { |
| 165 |
fuzzy["min_length"] = *q.fuzzyMinLength |
| 166 |
} |
| 167 |
if q.fuzzyPrefixLength != nil { |
| 168 |
fuzzy["prefix_length"] = *q.fuzzyPrefixLength |
| 169 |
} |
| 170 |
if q.unicodeAware != nil { |
| 171 |
fuzzy["unicode_aware"] = *q.unicodeAware |
| 172 |
} |
| 173 |
|
| 174 |
if !includeName { |
| 175 |
return cs, nil |
| 176 |
} |
| 177 |
|
| 178 |
source := make(map[string]interface{}) |
| 179 |
source[q.name] = cs |
| 180 |
return source, nil |
| 181 |
} |