search_queries_common_terms.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/5.2/query-dsl-common-terms-query.html
  10. type CommonTermsQuery struct {
  11. Query
  12. name string
  13. text interface{}
  14. cutoffFreq *float64
  15. highFreq *float64
  16. highFreqOp string
  17. highFreqMinimumShouldMatch string
  18. lowFreq *float64
  19. lowFreqOp string
  20. lowFreqMinimumShouldMatch string
  21. analyzer string
  22. boost *float64
  23. disableCoord *bool
  24. queryName string
  25. }
  26. // NewCommonTermsQuery creates and initializes a new common terms query.
  27. func NewCommonTermsQuery(name string, text interface{}) *CommonTermsQuery {
  28. return &CommonTermsQuery{name: name, text: text}
  29. }
  30. func (q *CommonTermsQuery) CutoffFrequency(f float64) *CommonTermsQuery {
  31. q.cutoffFreq = &f
  32. return q
  33. }
  34. func (q *CommonTermsQuery) HighFreq(f float64) *CommonTermsQuery {
  35. q.highFreq = &f
  36. return q
  37. }
  38. func (q *CommonTermsQuery) HighFreqOperator(op string) *CommonTermsQuery {
  39. q.highFreqOp = op
  40. return q
  41. }
  42. func (q *CommonTermsQuery) HighFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
  43. q.highFreqMinimumShouldMatch = minShouldMatch
  44. return q
  45. }
  46. func (q *CommonTermsQuery) LowFreq(f float64) *CommonTermsQuery {
  47. q.lowFreq = &f
  48. return q
  49. }
  50. func (q *CommonTermsQuery) LowFreqOperator(op string) *CommonTermsQuery {
  51. q.lowFreqOp = op
  52. return q
  53. }
  54. func (q *CommonTermsQuery) LowFreqMinimumShouldMatch(minShouldMatch string) *CommonTermsQuery {
  55. q.lowFreqMinimumShouldMatch = minShouldMatch
  56. return q
  57. }
  58. func (q *CommonTermsQuery) Analyzer(analyzer string) *CommonTermsQuery {
  59. q.analyzer = analyzer
  60. return q
  61. }
  62. func (q *CommonTermsQuery) Boost(boost float64) *CommonTermsQuery {
  63. q.boost = &boost
  64. return q
  65. }
  66. func (q *CommonTermsQuery) DisableCoord(disableCoord bool) *CommonTermsQuery {
  67. q.disableCoord = &disableCoord
  68. return q
  69. }
  70. func (q *CommonTermsQuery) QueryName(queryName string) *CommonTermsQuery {
  71. q.queryName = queryName
  72. return q
  73. }
  74. // Creates the query source for the common query.
  75. func (q *CommonTermsQuery) Source() (interface{}, error) {
  76. // {
  77. // "common": {
  78. // "body": {
  79. // "query": "this is bonsai cool",
  80. // "cutoff_frequency": 0.001
  81. // }
  82. // }
  83. // }
  84. source := make(map[string]interface{})
  85. body := make(map[string]interface{})
  86. query := make(map[string]interface{})
  87. source["common"] = body
  88. body[q.name] = query
  89. query["query"] = q.text
  90. if q.cutoffFreq != nil {
  91. query["cutoff_frequency"] = *q.cutoffFreq
  92. }
  93. if q.highFreq != nil {
  94. query["high_freq"] = *q.highFreq
  95. }
  96. if q.highFreqOp != "" {
  97. query["high_freq_operator"] = q.highFreqOp
  98. }
  99. if q.lowFreq != nil {
  100. query["low_freq"] = *q.lowFreq
  101. }
  102. if q.lowFreqOp != "" {
  103. query["low_freq_operator"] = q.lowFreqOp
  104. }
  105. if q.lowFreqMinimumShouldMatch != "" || q.highFreqMinimumShouldMatch != "" {
  106. mm := make(map[string]interface{})
  107. if q.lowFreqMinimumShouldMatch != "" {
  108. mm["low_freq"] = q.lowFreqMinimumShouldMatch
  109. }
  110. if q.highFreqMinimumShouldMatch != "" {
  111. mm["high_freq"] = q.highFreqMinimumShouldMatch
  112. }
  113. query["minimum_should_match"] = mm
  114. }
  115. if q.analyzer != "" {
  116. query["analyzer"] = q.analyzer
  117. }
  118. if q.disableCoord != nil {
  119. query["disable_coord"] = *q.disableCoord
  120. }
  121. if q.boost != nil {
  122. query["boost"] = *q.boost
  123. }
  124. if q.queryName != "" {
  125. query["_name"] = q.queryName
  126. }
  127. return source, nil
  128. }