search_queries_simple_query_string.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. import (
  6. "fmt"
  7. "strings"
  8. )
  9. // SimpleQueryStringQuery is a query that uses the SimpleQueryParser
  10. // to parse its context. Unlike the regular query_string query,
  11. // the simple_query_string query will never throw an exception,
  12. // and discards invalid parts of the query.
  13. //
  14. // For more details, see
  15. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-simple-query-string-query.html
  16. type SimpleQueryStringQuery struct {
  17. queryText string
  18. analyzer string
  19. quoteFieldSuffix string
  20. operator string
  21. fields []string
  22. fieldBoosts map[string]*float64
  23. minimumShouldMatch string
  24. flags string
  25. boost *float64
  26. lowercaseExpandedTerms *bool
  27. lenient *bool
  28. analyzeWildcard *bool
  29. locale string
  30. queryName string
  31. }
  32. // NewSimpleQueryStringQuery creates and initializes a new SimpleQueryStringQuery.
  33. func NewSimpleQueryStringQuery(text string) *SimpleQueryStringQuery {
  34. return &SimpleQueryStringQuery{
  35. queryText: text,
  36. fields: make([]string, 0),
  37. fieldBoosts: make(map[string]*float64),
  38. }
  39. }
  40. // Field adds a field to run the query against.
  41. func (q *SimpleQueryStringQuery) Field(field string) *SimpleQueryStringQuery {
  42. q.fields = append(q.fields, field)
  43. return q
  44. }
  45. // Field adds a field to run the query against with a specific boost.
  46. func (q *SimpleQueryStringQuery) FieldWithBoost(field string, boost float64) *SimpleQueryStringQuery {
  47. q.fields = append(q.fields, field)
  48. q.fieldBoosts[field] = &boost
  49. return q
  50. }
  51. // Boost sets the boost for this query.
  52. func (q *SimpleQueryStringQuery) Boost(boost float64) *SimpleQueryStringQuery {
  53. q.boost = &boost
  54. return q
  55. }
  56. // QueryName sets the query name for the filter that can be used when
  57. // searching for matched_filters per hit.
  58. func (q *SimpleQueryStringQuery) QueryName(queryName string) *SimpleQueryStringQuery {
  59. q.queryName = queryName
  60. return q
  61. }
  62. // Analyzer specifies the analyzer to use for the query.
  63. func (q *SimpleQueryStringQuery) Analyzer(analyzer string) *SimpleQueryStringQuery {
  64. q.analyzer = analyzer
  65. return q
  66. }
  67. // QuoteFieldSuffix is an optional field name suffix to automatically
  68. // try and add to the field searched when using quoted text.
  69. func (q *SimpleQueryStringQuery) QuoteFieldSuffix(quoteFieldSuffix string) *SimpleQueryStringQuery {
  70. q.quoteFieldSuffix = quoteFieldSuffix
  71. return q
  72. }
  73. // DefaultOperator specifies the default operator for the query.
  74. func (q *SimpleQueryStringQuery) DefaultOperator(defaultOperator string) *SimpleQueryStringQuery {
  75. q.operator = defaultOperator
  76. return q
  77. }
  78. // Flags sets the flags for the query.
  79. func (q *SimpleQueryStringQuery) Flags(flags string) *SimpleQueryStringQuery {
  80. q.flags = flags
  81. return q
  82. }
  83. // LowercaseExpandedTerms indicates whether terms of wildcard, prefix, fuzzy
  84. // and range queries are automatically lower-cased or not. Default is true.
  85. func (q *SimpleQueryStringQuery) LowercaseExpandedTerms(lowercaseExpandedTerms bool) *SimpleQueryStringQuery {
  86. q.lowercaseExpandedTerms = &lowercaseExpandedTerms
  87. return q
  88. }
  89. func (q *SimpleQueryStringQuery) Locale(locale string) *SimpleQueryStringQuery {
  90. q.locale = locale
  91. return q
  92. }
  93. // Lenient indicates whether the query string parser should be lenient
  94. // when parsing field values. It defaults to the index setting and if not
  95. // set, defaults to false.
  96. func (q *SimpleQueryStringQuery) Lenient(lenient bool) *SimpleQueryStringQuery {
  97. q.lenient = &lenient
  98. return q
  99. }
  100. // AnalyzeWildcard indicates whether to enabled analysis on wildcard and prefix queries.
  101. func (q *SimpleQueryStringQuery) AnalyzeWildcard(analyzeWildcard bool) *SimpleQueryStringQuery {
  102. q.analyzeWildcard = &analyzeWildcard
  103. return q
  104. }
  105. func (q *SimpleQueryStringQuery) MinimumShouldMatch(minimumShouldMatch string) *SimpleQueryStringQuery {
  106. q.minimumShouldMatch = minimumShouldMatch
  107. return q
  108. }
  109. // Source returns JSON for the query.
  110. func (q *SimpleQueryStringQuery) Source() (interface{}, error) {
  111. // {
  112. // "simple_query_string" : {
  113. // "query" : "\"fried eggs\" +(eggplant | potato) -frittata",
  114. // "analyzer" : "snowball",
  115. // "fields" : ["body^5","_all"],
  116. // "default_operator" : "and"
  117. // }
  118. // }
  119. source := make(map[string]interface{})
  120. query := make(map[string]interface{})
  121. source["simple_query_string"] = query
  122. query["query"] = q.queryText
  123. if len(q.fields) > 0 {
  124. var fields []string
  125. for _, field := range q.fields {
  126. if boost, found := q.fieldBoosts[field]; found {
  127. if boost != nil {
  128. fields = append(fields, fmt.Sprintf("%s^%f", field, *boost))
  129. } else {
  130. fields = append(fields, field)
  131. }
  132. } else {
  133. fields = append(fields, field)
  134. }
  135. }
  136. query["fields"] = fields
  137. }
  138. if q.flags != "" {
  139. query["flags"] = q.flags
  140. }
  141. if q.analyzer != "" {
  142. query["analyzer"] = q.analyzer
  143. }
  144. if q.operator != "" {
  145. query["default_operator"] = strings.ToLower(q.operator)
  146. }
  147. if q.lowercaseExpandedTerms != nil {
  148. query["lowercase_expanded_terms"] = *q.lowercaseExpandedTerms
  149. }
  150. if q.lenient != nil {
  151. query["lenient"] = *q.lenient
  152. }
  153. if q.analyzeWildcard != nil {
  154. query["analyze_wildcard"] = *q.analyzeWildcard
  155. }
  156. if q.locale != "" {
  157. query["locale"] = q.locale
  158. }
  159. if q.queryName != "" {
  160. query["_name"] = q.queryName
  161. }
  162. if q.minimumShouldMatch != "" {
  163. query["minimum_should_match"] = q.minimumShouldMatch
  164. }
  165. if q.quoteFieldSuffix != "" {
  166. query["quote_field_suffix"] = q.quoteFieldSuffix
  167. }
  168. if q.boost != nil {
  169. query["boost"] = *q.boost
  170. }
  171. return source, nil
  172. }