summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/olivere/elastic.v5/search_queries_match.go
blob: b38b12452989108f1ec555d151869e4ab4ab32db (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
// 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

// MatchQuery is a family of queries that accepts text/numerics/dates,
// analyzes them, and constructs a query.
//
// To create a new MatchQuery, use NewMatchQuery. To create specific types
// of queries, e.g. a match_phrase query, use NewMatchPhrQuery(...).Type("phrase"),
// or use one of the shortcuts e.g. NewMatchPhraseQuery(...).
//
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/6.0/query-dsl-match-query.html
type MatchQuery struct {
	name                string
	text                interface{}
	operator            string // or / and
	analyzer            string
	boost               *float64
	fuzziness           string
	prefixLength        *int
	maxExpansions       *int
	minimumShouldMatch  string
	fuzzyRewrite        string
	lenient             *bool
	fuzzyTranspositions *bool
	zeroTermsQuery      string
	cutoffFrequency     *float64
	queryName           string
}

// NewMatchQuery creates and initializes a new MatchQuery.
func NewMatchQuery(name string, text interface{}) *MatchQuery {
	return &MatchQuery{name: name, text: text}
}

// Operator sets the operator to use when using a boolean query.
// Can be "AND" or "OR" (default).
func (q *MatchQuery) Operator(operator string) *MatchQuery {
	q.operator = operator
	return q
}

// Analyzer explicitly sets the analyzer to use. It defaults to use explicit
// mapping config for the field, or, if not set, the default search analyzer.
func (q *MatchQuery) Analyzer(analyzer string) *MatchQuery {
	q.analyzer = analyzer
	return q
}

// Fuzziness sets the fuzziness when evaluated to a fuzzy query type.
// Defaults to "AUTO".
func (q *MatchQuery) Fuzziness(fuzziness string) *MatchQuery {
	q.fuzziness = fuzziness
	return q
}

// PrefixLength sets the length of a length of common (non-fuzzy)
// prefix for fuzzy match queries. It must be non-negative.
func (q *MatchQuery) PrefixLength(prefixLength int) *MatchQuery {
	q.prefixLength = &prefixLength
	return q
}

// MaxExpansions is used with fuzzy or prefix type queries. It specifies
// the number of term expansions to use. It defaults to unbounded so that
// its recommended to set it to a reasonable value for faster execution.
func (q *MatchQuery) MaxExpansions(maxExpansions int) *MatchQuery {
	q.maxExpansions = &maxExpansions
	return q
}

// CutoffFrequency can be a value in [0..1] (or an absolute number >=1).
// It represents the maximum treshold of a terms document frequency to be
// considered a low frequency term.
func (q *MatchQuery) CutoffFrequency(cutoff float64) *MatchQuery {
	q.cutoffFrequency = &cutoff
	return q
}

// MinimumShouldMatch sets the optional minimumShouldMatch value to
// apply to the query.
func (q *MatchQuery) MinimumShouldMatch(minimumShouldMatch string) *MatchQuery {
	q.minimumShouldMatch = minimumShouldMatch
	return q
}

// FuzzyRewrite sets the fuzzy_rewrite parameter controlling how the
// fuzzy query will get rewritten.
func (q *MatchQuery) FuzzyRewrite(fuzzyRewrite string) *MatchQuery {
	q.fuzzyRewrite = fuzzyRewrite
	return q
}

// FuzzyTranspositions sets whether transpositions are supported in
// fuzzy queries.
//
// The default metric used by fuzzy queries to determine a match is
// the Damerau-Levenshtein distance formula which supports transpositions.
// Setting transposition to false will
// * switch to classic Levenshtein distance.
// * If not set, Damerau-Levenshtein distance metric will be used.
func (q *MatchQuery) FuzzyTranspositions(fuzzyTranspositions bool) *MatchQuery {
	q.fuzzyTranspositions = &fuzzyTranspositions
	return q
}

// Lenient specifies whether format based failures will be ignored.
func (q *MatchQuery) Lenient(lenient bool) *MatchQuery {
	q.lenient = &lenient
	return q
}

// ZeroTermsQuery can be "all" or "none".
func (q *MatchQuery) ZeroTermsQuery(zeroTermsQuery string) *MatchQuery {
	q.zeroTermsQuery = zeroTermsQuery
	return q
}

// Boost sets the boost to apply to this query.
func (q *MatchQuery) Boost(boost float64) *MatchQuery {
	q.boost = &boost
	return q
}

// QueryName sets the query name for the filter that can be used when
// searching for matched filters per hit.
func (q *MatchQuery) QueryName(queryName string) *MatchQuery {
	q.queryName = queryName
	return q
}

// Source returns JSON for the function score query.
func (q *MatchQuery) Source() (interface{}, error) {
	// {"match":{"name":{"query":"value","type":"boolean/phrase"}}}
	source := make(map[string]interface{})

	match := make(map[string]interface{})
	source["match"] = match

	query := make(map[string]interface{})
	match[q.name] = query

	query["query"] = q.text

	if q.operator != "" {
		query["operator"] = q.operator
	}
	if q.analyzer != "" {
		query["analyzer"] = q.analyzer
	}
	if q.fuzziness != "" {
		query["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		query["prefix_length"] = *q.prefixLength
	}
	if q.maxExpansions != nil {
		query["max_expansions"] = *q.maxExpansions
	}
	if q.minimumShouldMatch != "" {
		query["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.fuzzyRewrite != "" {
		query["fuzzy_rewrite"] = q.fuzzyRewrite
	}
	if q.lenient != nil {
		query["lenient"] = *q.lenient
	}
	if q.fuzzyTranspositions != nil {
		query["fuzzy_transpositions"] = *q.fuzzyTranspositions
	}
	if q.zeroTermsQuery != "" {
		query["zero_terms_query"] = q.zeroTermsQuery
	}
	if q.cutoffFrequency != nil {
		query["cutoff_frequency"] = q.cutoffFrequency
	}
	if q.boost != nil {
		query["boost"] = *q.boost
	}
	if q.queryName != "" {
		query["_name"] = q.queryName
	}

	return source, nil
}