search_queries_match.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. package elastic
  5. // MatchQuery is a family of queries that accepts text/numerics/dates,
  6. // analyzes them, and constructs a query.
  7. //
  8. // To create a new MatchQuery, use NewMatchQuery. To create specific types
  9. // of queries, e.g. a match_phrase query, use NewMatchPhrQuery(...).Type("phrase"),
  10. // or use one of the shortcuts e.g. NewMatchPhraseQuery(...).
  11. //
  12. // For more details, see
  13. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-match-query.html
  14. type MatchQuery struct {
  15. name string
  16. text interface{}
  17. operator string // or / and
  18. analyzer string
  19. boost *float64
  20. fuzziness string
  21. prefixLength *int
  22. maxExpansions *int
  23. minimumShouldMatch string
  24. fuzzyRewrite string
  25. lenient *bool
  26. fuzzyTranspositions *bool
  27. zeroTermsQuery string
  28. cutoffFrequency *float64
  29. queryName string
  30. }
  31. // NewMatchQuery creates and initializes a new MatchQuery.
  32. func NewMatchQuery(name string, text interface{}) *MatchQuery {
  33. return &MatchQuery{name: name, text: text}
  34. }
  35. // Operator sets the operator to use when using a boolean query.
  36. // Can be "AND" or "OR" (default).
  37. func (q *MatchQuery) Operator(operator string) *MatchQuery {
  38. q.operator = operator
  39. return q
  40. }
  41. // Analyzer explicitly sets the analyzer to use. It defaults to use explicit
  42. // mapping config for the field, or, if not set, the default search analyzer.
  43. func (q *MatchQuery) Analyzer(analyzer string) *MatchQuery {
  44. q.analyzer = analyzer
  45. return q
  46. }
  47. // Fuzziness sets the fuzziness when evaluated to a fuzzy query type.
  48. // Defaults to "AUTO".
  49. func (q *MatchQuery) Fuzziness(fuzziness string) *MatchQuery {
  50. q.fuzziness = fuzziness
  51. return q
  52. }
  53. // PrefixLength sets the length of a length of common (non-fuzzy)
  54. // prefix for fuzzy match queries. It must be non-negative.
  55. func (q *MatchQuery) PrefixLength(prefixLength int) *MatchQuery {
  56. q.prefixLength = &prefixLength
  57. return q
  58. }
  59. // MaxExpansions is used with fuzzy or prefix type queries. It specifies
  60. // the number of term expansions to use. It defaults to unbounded so that
  61. // its recommended to set it to a reasonable value for faster execution.
  62. func (q *MatchQuery) MaxExpansions(maxExpansions int) *MatchQuery {
  63. q.maxExpansions = &maxExpansions
  64. return q
  65. }
  66. // CutoffFrequency can be a value in [0..1] (or an absolute number >=1).
  67. // It represents the maximum treshold of a terms document frequency to be
  68. // considered a low frequency term.
  69. func (q *MatchQuery) CutoffFrequency(cutoff float64) *MatchQuery {
  70. q.cutoffFrequency = &cutoff
  71. return q
  72. }
  73. // MinimumShouldMatch sets the optional minimumShouldMatch value to
  74. // apply to the query.
  75. func (q *MatchQuery) MinimumShouldMatch(minimumShouldMatch string) *MatchQuery {
  76. q.minimumShouldMatch = minimumShouldMatch
  77. return q
  78. }
  79. // FuzzyRewrite sets the fuzzy_rewrite parameter controlling how the
  80. // fuzzy query will get rewritten.
  81. func (q *MatchQuery) FuzzyRewrite(fuzzyRewrite string) *MatchQuery {
  82. q.fuzzyRewrite = fuzzyRewrite
  83. return q
  84. }
  85. // FuzzyTranspositions sets whether transpositions are supported in
  86. // fuzzy queries.
  87. //
  88. // The default metric used by fuzzy queries to determine a match is
  89. // the Damerau-Levenshtein distance formula which supports transpositions.
  90. // Setting transposition to false will
  91. // * switch to classic Levenshtein distance.
  92. // * If not set, Damerau-Levenshtein distance metric will be used.
  93. func (q *MatchQuery) FuzzyTranspositions(fuzzyTranspositions bool) *MatchQuery {
  94. q.fuzzyTranspositions = &fuzzyTranspositions
  95. return q
  96. }
  97. // Lenient specifies whether format based failures will be ignored.
  98. func (q *MatchQuery) Lenient(lenient bool) *MatchQuery {
  99. q.lenient = &lenient
  100. return q
  101. }
  102. // ZeroTermsQuery can be "all" or "none".
  103. func (q *MatchQuery) ZeroTermsQuery(zeroTermsQuery string) *MatchQuery {
  104. q.zeroTermsQuery = zeroTermsQuery
  105. return q
  106. }
  107. // Boost sets the boost to apply to this query.
  108. func (q *MatchQuery) Boost(boost float64) *MatchQuery {
  109. q.boost = &boost
  110. return q
  111. }
  112. // QueryName sets the query name for the filter that can be used when
  113. // searching for matched filters per hit.
  114. func (q *MatchQuery) QueryName(queryName string) *MatchQuery {
  115. q.queryName = queryName
  116. return q
  117. }
  118. // Source returns JSON for the function score query.
  119. func (q *MatchQuery) Source() (interface{}, error) {
  120. // {"match":{"name":{"query":"value","type":"boolean/phrase"}}}
  121. source := make(map[string]interface{})
  122. match := make(map[string]interface{})
  123. source["match"] = match
  124. query := make(map[string]interface{})
  125. match[q.name] = query
  126. query["query"] = q.text
  127. if q.operator != "" {
  128. query["operator"] = q.operator
  129. }
  130. if q.analyzer != "" {
  131. query["analyzer"] = q.analyzer
  132. }
  133. if q.fuzziness != "" {
  134. query["fuzziness"] = q.fuzziness
  135. }
  136. if q.prefixLength != nil {
  137. query["prefix_length"] = *q.prefixLength
  138. }
  139. if q.maxExpansions != nil {
  140. query["max_expansions"] = *q.maxExpansions
  141. }
  142. if q.minimumShouldMatch != "" {
  143. query["minimum_should_match"] = q.minimumShouldMatch
  144. }
  145. if q.fuzzyRewrite != "" {
  146. query["fuzzy_rewrite"] = q.fuzzyRewrite
  147. }
  148. if q.lenient != nil {
  149. query["lenient"] = *q.lenient
  150. }
  151. if q.fuzzyTranspositions != nil {
  152. query["fuzzy_transpositions"] = *q.fuzzyTranspositions
  153. }
  154. if q.zeroTermsQuery != "" {
  155. query["zero_terms_query"] = q.zeroTermsQuery
  156. }
  157. if q.cutoffFrequency != nil {
  158. query["cutoff_frequency"] = q.cutoffFrequency
  159. }
  160. if q.boost != nil {
  161. query["boost"] = *q.boost
  162. }
  163. if q.queryName != "" {
  164. query["_name"] = q.queryName
  165. }
  166. return source, nil
  167. }