You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_queries_common_terms.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // CommonTermsQuery is a modern alternative to stopwords
  6. // which improves the precision and recall of search results
  7. // (by taking stopwords into account), without sacrificing performance.
  8. // For more details, see:
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-common-terms-query.html
  10. //
  11. // Deprecated: Use Match query instead (7.3.0+), which skips blocks of
  12. // documents efficiently, without any configuration, provided that the
  13. // total number of hits is not tracked.
  14. type CommonTermsQuery struct {
  15. Query
  16. name string
  17. text interface{}
  18. cutoffFreq *float64
  19. highFreq *float64
  20. highFreqOp string
  21. highFreqMinimumShouldMatch string
  22. lowFreq *float64
  23. lowFreqOp string
  24. lowFreqMinimumShouldMatch string
  25. analyzer string
  26. boost *float64
  27. queryName string
  28. }
  29. // NewCommonTermsQuery creates and initializes a new common terms query.
  30. func NewCommonTermsQuery(name string, text interface{}) *CommonTermsQuery {
  31. return &CommonTermsQuery{name: name, text: text}
  32. }
  33. func (q *CommonTermsQuery) CutoffFrequency(f float64) *CommonTermsQuery {
  34. q.cutoffFreq = &f
  35. return q
  36. }
  37. func (q *CommonTermsQuery) HighFreq(f float64) *CommonTermsQuery {
  38. q.highFreq = &f
  39. return q
  40. }
  41. func (q *CommonTermsQuery) HighFreqOperator(op string) *CommonTermsQuery {
  42. q.highFreqOp = op
  43. return q
  44. }
  45. func (q *CommonTermsQuery) HighFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
  46. q.highFreqMinimumShouldMatch = minShouldMatch
  47. return q
  48. }
  49. func (q *CommonTermsQuery) LowFreq(f float64) *CommonTermsQuery {
  50. q.lowFreq = &f
  51. return q
  52. }
  53. func (q *CommonTermsQuery) LowFreqOperator(op string) *CommonTermsQuery {
  54. q.lowFreqOp = op
  55. return q
  56. }
  57. func (q *CommonTermsQuery) LowFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
  58. q.lowFreqMinimumShouldMatch = minShouldMatch
  59. return q
  60. }
  61. func (q *CommonTermsQuery) Analyzer(analyzer string) *CommonTermsQuery {
  62. q.analyzer = analyzer
  63. return q
  64. }
  65. func (q *CommonTermsQuery) Boost(boost float64) *CommonTermsQuery {
  66. q.boost = &boost
  67. return q
  68. }
  69. func (q *CommonTermsQuery) QueryName(queryName string) *CommonTermsQuery {
  70. q.queryName = queryName
  71. return q
  72. }
  73. // Creates the query source for the common query.
  74. func (q *CommonTermsQuery) Source() (interface{}, error) {
  75. // {
  76. // "common": {
  77. // "body": {
  78. // "query": "this is bonsai cool",
  79. // "cutoff_frequency": 0.001
  80. // }
  81. // }
  82. // }
  83. source := make(map[string]interface{})
  84. body := make(map[string]interface{})
  85. query := make(map[string]interface{})
  86. source["common"] = body
  87. body[q.name] = query
  88. query["query"] = q.text
  89. if q.cutoffFreq != nil {
  90. query["cutoff_frequency"] = *q.cutoffFreq
  91. }
  92. if q.highFreq != nil {
  93. query["high_freq"] = *q.highFreq
  94. }
  95. if q.highFreqOp != "" {
  96. query["high_freq_operator"] = q.highFreqOp
  97. }
  98. if q.lowFreq != nil {
  99. query["low_freq"] = *q.lowFreq
  100. }
  101. if q.lowFreqOp != "" {
  102. query["low_freq_operator"] = q.lowFreqOp
  103. }
  104. if q.lowFreqMinimumShouldMatch != "" || q.highFreqMinimumShouldMatch != "" {
  105. mm := make(map[string]interface{})
  106. if q.lowFreqMinimumShouldMatch != "" {
  107. mm["low_freq"] = q.lowFreqMinimumShouldMatch
  108. }
  109. if q.highFreqMinimumShouldMatch != "" {
  110. mm["high_freq"] = q.highFreqMinimumShouldMatch
  111. }
  112. query["minimum_should_match"] = mm
  113. }
  114. if q.analyzer != "" {
  115. query["analyzer"] = q.analyzer
  116. }
  117. if q.boost != nil {
  118. query["boost"] = *q.boost
  119. }
  120. if q.queryName != "" {
  121. query["_name"] = q.queryName
  122. }
  123. return source, nil
  124. }