| 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 |
// TermSuggester suggests terms based on edit distance. |
| 8 |
// For more details, see |
| 9 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-term.html. |
| 10 |
type TermSuggester struct { |
| 11 |
Suggester |
| 12 |
name string |
| 13 |
text string |
| 14 |
field string |
| 15 |
analyzer string |
| 16 |
size *int |
| 17 |
shardSize *int |
| 18 |
contextQueries []SuggesterContextQuery |
| 19 |
|
| 20 |
// fields specific to term suggester |
| 21 |
suggestMode string |
| 22 |
accuracy *float64 |
| 23 |
sort string |
| 24 |
stringDistance string |
| 25 |
maxEdits *int |
| 26 |
maxInspections *int |
| 27 |
maxTermFreq *float64 |
| 28 |
prefixLength *int |
| 29 |
minWordLength *int |
| 30 |
minDocFreq *float64 |
| 31 |
} |
| 32 |
|
| 33 |
// NewTermSuggester creates a new TermSuggester. |
| 34 |
func NewTermSuggester(name string) *TermSuggester { |
| 35 |
return &TermSuggester{ |
| 36 |
name: name, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func (q *TermSuggester) Name() string { |
| 41 |
return q.name |
| 42 |
} |
| 43 |
|
| 44 |
func (q *TermSuggester) Text(text string) *TermSuggester { |
| 45 |
q.text = text |
| 46 |
return q |
| 47 |
} |
| 48 |
|
| 49 |
func (q *TermSuggester) Field(field string) *TermSuggester { |
| 50 |
q.field = field |
| 51 |
return q |
| 52 |
} |
| 53 |
|
| 54 |
func (q *TermSuggester) Analyzer(analyzer string) *TermSuggester { |
| 55 |
q.analyzer = analyzer |
| 56 |
return q |
| 57 |
} |
| 58 |
|
| 59 |
func (q *TermSuggester) Size(size int) *TermSuggester { |
| 60 |
q.size = &size |
| 61 |
return q |
| 62 |
} |
| 63 |
|
| 64 |
func (q *TermSuggester) ShardSize(shardSize int) *TermSuggester { |
| 65 |
q.shardSize = &shardSize |
| 66 |
return q |
| 67 |
} |
| 68 |
|
| 69 |
func (q *TermSuggester) ContextQuery(query SuggesterContextQuery) *TermSuggester { |
| 70 |
q.contextQueries = append(q.contextQueries, query) |
| 71 |
return q |
| 72 |
} |
| 73 |
|
| 74 |
func (q *TermSuggester) ContextQueries(queries ...SuggesterContextQuery) *TermSuggester { |
| 75 |
q.contextQueries = append(q.contextQueries, queries...) |
| 76 |
return q |
| 77 |
} |
| 78 |
|
| 79 |
func (q *TermSuggester) SuggestMode(suggestMode string) *TermSuggester { |
| 80 |
q.suggestMode = suggestMode |
| 81 |
return q |
| 82 |
} |
| 83 |
|
| 84 |
func (q *TermSuggester) Accuracy(accuracy float64) *TermSuggester { |
| 85 |
q.accuracy = &accuracy |
| 86 |
return q |
| 87 |
} |
| 88 |
|
| 89 |
func (q *TermSuggester) Sort(sort string) *TermSuggester { |
| 90 |
q.sort = sort |
| 91 |
return q |
| 92 |
} |
| 93 |
|
| 94 |
func (q *TermSuggester) StringDistance(stringDistance string) *TermSuggester { |
| 95 |
q.stringDistance = stringDistance |
| 96 |
return q |
| 97 |
} |
| 98 |
|
| 99 |
func (q *TermSuggester) MaxEdits(maxEdits int) *TermSuggester { |
| 100 |
q.maxEdits = &maxEdits |
| 101 |
return q |
| 102 |
} |
| 103 |
|
| 104 |
func (q *TermSuggester) MaxInspections(maxInspections int) *TermSuggester { |
| 105 |
q.maxInspections = &maxInspections |
| 106 |
return q |
| 107 |
} |
| 108 |
|
| 109 |
func (q *TermSuggester) MaxTermFreq(maxTermFreq float64) *TermSuggester { |
| 110 |
q.maxTermFreq = &maxTermFreq |
| 111 |
return q |
| 112 |
} |
| 113 |
|
| 114 |
func (q *TermSuggester) PrefixLength(prefixLength int) *TermSuggester { |
| 115 |
q.prefixLength = &prefixLength |
| 116 |
return q |
| 117 |
} |
| 118 |
|
| 119 |
func (q *TermSuggester) MinWordLength(minWordLength int) *TermSuggester { |
| 120 |
q.minWordLength = &minWordLength |
| 121 |
return q |
| 122 |
} |
| 123 |
|
| 124 |
func (q *TermSuggester) MinDocFreq(minDocFreq float64) *TermSuggester { |
| 125 |
q.minDocFreq = &minDocFreq |
| 126 |
return q |
| 127 |
} |
| 128 |
|
| 129 |
// termSuggesterRequest is necessary because the order in which |
| 130 |
// the JSON elements are routed to Elasticsearch is relevant. |
| 131 |
// We got into trouble when using plain maps because the text element |
| 132 |
// needs to go before the term element. |
| 133 |
type termSuggesterRequest struct { |
| 134 |
Text string `json:"text"` |
| 135 |
Term interface{} `json:"term"` |
| 136 |
} |
| 137 |
|
| 138 |
// Source generates the source for the term suggester. |
| 139 |
func (q *TermSuggester) Source(includeName bool) (interface{}, error) { |
| 140 |
// "suggest" : { |
| 141 |
// "my-suggest-1" : { |
| 142 |
// "text" : "the amsterdma meetpu", |
| 143 |
// "term" : { |
| 144 |
// "field" : "body" |
| 145 |
// } |
| 146 |
// }, |
| 147 |
// "my-suggest-2" : { |
| 148 |
// "text" : "the rottredam meetpu", |
| 149 |
// "term" : { |
| 150 |
// "field" : "title", |
| 151 |
// } |
| 152 |
// } |
| 153 |
// } |
| 154 |
ts := &termSuggesterRequest{} |
| 155 |
if q.text != "" { |
| 156 |
ts.Text = q.text |
| 157 |
} |
| 158 |
|
| 159 |
suggester := make(map[string]interface{}) |
| 160 |
ts.Term = suggester |
| 161 |
|
| 162 |
if q.analyzer != "" { |
| 163 |
suggester["analyzer"] = q.analyzer |
| 164 |
} |
| 165 |
if q.field != "" { |
| 166 |
suggester["field"] = q.field |
| 167 |
} |
| 168 |
if q.size != nil { |
| 169 |
suggester["size"] = *q.size |
| 170 |
} |
| 171 |
if q.shardSize != nil { |
| 172 |
suggester["shard_size"] = *q.shardSize |
| 173 |
} |
| 174 |
switch len(q.contextQueries) { |
| 175 |
case 0: |
| 176 |
case 1: |
| 177 |
src, err := q.contextQueries[0].Source() |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
suggester["context"] = src |
| 182 |
default: |
| 183 |
ctxq := make([]interface{}, len(q.contextQueries)) |
| 184 |
for i, query := range q.contextQueries { |
| 185 |
src, err := query.Source() |
| 186 |
if err != nil { |
| 187 |
return nil, err |
| 188 |
} |
| 189 |
ctxq[i] = src |
| 190 |
} |
| 191 |
suggester["context"] = ctxq |
| 192 |
} |
| 193 |
|
| 194 |
// Specific to term suggester |
| 195 |
if q.suggestMode != "" { |
| 196 |
suggester["suggest_mode"] = q.suggestMode |
| 197 |
} |
| 198 |
if q.accuracy != nil { |
| 199 |
suggester["accuracy"] = *q.accuracy |
| 200 |
} |
| 201 |
if q.sort != "" { |
| 202 |
suggester["sort"] = q.sort |
| 203 |
} |
| 204 |
if q.stringDistance != "" { |
| 205 |
suggester["string_distance"] = q.stringDistance |
| 206 |
} |
| 207 |
if q.maxEdits != nil { |
| 208 |
suggester["max_edits"] = *q.maxEdits |
| 209 |
} |
| 210 |
if q.maxInspections != nil { |
| 211 |
suggester["max_inspections"] = *q.maxInspections |
| 212 |
} |
| 213 |
if q.maxTermFreq != nil { |
| 214 |
suggester["max_term_freq"] = *q.maxTermFreq |
| 215 |
} |
| 216 |
if q.prefixLength != nil { |
| 217 |
suggester["prefix_length"] = *q.prefixLength |
| 218 |
} |
| 219 |
if q.minWordLength != nil { |
| 220 |
suggester["min_word_len"] = *q.minWordLength |
| 221 |
} |
| 222 |
if q.minDocFreq != nil { |
| 223 |
suggester["min_doc_freq"] = *q.minDocFreq |
| 224 |
} |
| 225 |
|
| 226 |
if !includeName { |
| 227 |
return ts, nil |
| 228 |
} |
| 229 |
|
| 230 |
source := make(map[string]interface{}) |
| 231 |
source[q.name] = ts |
| 232 |
return source, nil |
| 233 |
} |