File: suggester_term.go

package info (click to toggle)
golang-gopkg-olivere-elastic.v2 2.0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 1,964 kB
  • sloc: makefile: 17
file content (225 lines) | stat: -rw-r--r-- 5,144 bytes parent folder | download | duplicates (2)
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
// 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

// For more details, see
// http://www.elasticsearch.org/guide/reference/api/search/term-suggest/
type TermSuggester struct {
	Suggester
	name           string
	text           string
	field          string
	analyzer       string
	size           *int
	shardSize      *int
	contextQueries []SuggesterContextQuery

	// fields specific to term suggester
	suggestMode    string
	accuracy       *float32
	sort           string
	stringDistance string
	maxEdits       *int
	maxInspections *int
	maxTermFreq    *float32
	prefixLength   *int
	minWordLength  *int
	minDocFreq     *float32
}

// Creates a new term suggester.
func NewTermSuggester(name string) TermSuggester {
	return TermSuggester{
		name:           name,
		contextQueries: make([]SuggesterContextQuery, 0),
	}
}

func (q TermSuggester) Name() string {
	return q.name
}

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

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

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

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

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

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

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

func (q TermSuggester) SuggestMode(suggestMode string) TermSuggester {
	q.suggestMode = suggestMode
	return q
}

func (q TermSuggester) Accuracy(accuracy float32) TermSuggester {
	q.accuracy = &accuracy
	return q
}

func (q TermSuggester) Sort(sort string) TermSuggester {
	q.sort = sort
	return q
}

func (q TermSuggester) StringDistance(stringDistance string) TermSuggester {
	q.stringDistance = stringDistance
	return q
}

func (q TermSuggester) MaxEdits(maxEdits int) TermSuggester {
	q.maxEdits = &maxEdits
	return q
}

func (q TermSuggester) MaxInspections(maxInspections int) TermSuggester {
	q.maxInspections = &maxInspections
	return q
}

func (q TermSuggester) MaxTermFreq(maxTermFreq float32) TermSuggester {
	q.maxTermFreq = &maxTermFreq
	return q
}

func (q TermSuggester) PrefixLength(prefixLength int) TermSuggester {
	q.prefixLength = &prefixLength
	return q
}

func (q TermSuggester) MinWordLength(minWordLength int) TermSuggester {
	q.minWordLength = &minWordLength
	return q
}

func (q TermSuggester) MinDocFreq(minDocFreq float32) TermSuggester {
	q.minDocFreq = &minDocFreq
	return q
}

// termSuggesterRequest 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 term element.
type termSuggesterRequest struct {
	Text string      `json:"text"`
	Term interface{} `json:"term"`
}

// Creates the source for the term suggester.
func (q TermSuggester) Source(includeName bool) interface{} {
	// "suggest" : {
	//   "my-suggest-1" : {
	//     "text" : "the amsterdma meetpu",
	//     "term" : {
	//       "field" : "body"
	//     }
	//   },
	//   "my-suggest-2" : {
	//     "text" : "the rottredam meetpu",
	//     "term" : {
	//       "field" : "title",
	//     }
	//   }
	// }
	ts := &termSuggesterRequest{}
	if q.text != "" {
		ts.Text = q.text
	}

	suggester := make(map[string]interface{})
	ts.Term = 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
	}

	// Specific to term suggester
	if q.suggestMode != "" {
		suggester["suggest_mode"] = q.suggestMode
	}
	if q.accuracy != nil {
		suggester["accuracy"] = *q.accuracy
	}
	if q.sort != "" {
		suggester["sort"] = q.sort
	}
	if q.stringDistance != "" {
		suggester["string_distance"] = q.stringDistance
	}
	if q.maxEdits != nil {
		suggester["max_edits"] = *q.maxEdits
	}
	if q.maxInspections != nil {
		suggester["max_inspections"] = *q.maxInspections
	}
	if q.maxTermFreq != nil {
		suggester["max_term_freq"] = *q.maxTermFreq
	}
	if q.prefixLength != nil {
		suggester["prefix_len"] = *q.prefixLength
	}
	if q.minWordLength != nil {
		suggester["min_word_len"] = *q.minWordLength
	}
	if q.minDocFreq != nil {
		suggester["min_doc_freq"] = *q.minDocFreq
	}

	if !includeName {
		return ts
	}

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