search_queries_common.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2012-2015 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. // The common terms query 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. // http://www.elasticsearch.org/guide/reference/query-dsl/common-terms-query/
  10. type CommonQuery struct {
  11. Query
  12. name string
  13. query string
  14. cutoffFreq *float64
  15. highFreq *float64
  16. highFreqOp string
  17. highFreqMinMatch interface{}
  18. lowFreq *float64
  19. lowFreqOp string
  20. lowFreqMinMatch interface{}
  21. analyzer string
  22. boost *float64
  23. disableCoords *bool
  24. }
  25. // Creates a new common query.
  26. func NewCommonQuery(name string, query string) CommonQuery {
  27. q := CommonQuery{name: name, query: query}
  28. return q
  29. }
  30. func (q *CommonQuery) CutoffFrequency(f float64) *CommonQuery {
  31. q.cutoffFreq = &f
  32. return q
  33. }
  34. func (q *CommonQuery) HighFreq(f float64) *CommonQuery {
  35. q.highFreq = &f
  36. return q
  37. }
  38. func (q *CommonQuery) HighFreqOperator(op string) *CommonQuery {
  39. q.highFreqOp = op
  40. return q
  41. }
  42. func (q *CommonQuery) HighFreqMinMatch(min interface{}) *CommonQuery {
  43. q.highFreqMinMatch = min
  44. return q
  45. }
  46. func (q *CommonQuery) LowFreq(f float64) *CommonQuery {
  47. q.lowFreq = &f
  48. return q
  49. }
  50. func (q *CommonQuery) LowFreqOperator(op string) *CommonQuery {
  51. q.lowFreqOp = op
  52. return q
  53. }
  54. func (q *CommonQuery) LowFreqMinMatch(min interface{}) *CommonQuery {
  55. q.lowFreqMinMatch = min
  56. return q
  57. }
  58. func (q *CommonQuery) Analyzer(analyzer string) *CommonQuery {
  59. q.analyzer = analyzer
  60. return q
  61. }
  62. func (q *CommonQuery) Boost(boost float64) *CommonQuery {
  63. q.boost = &boost
  64. return q
  65. }
  66. func (q *CommonQuery) DisableCoords(disable bool) *CommonQuery {
  67. q.disableCoords = &disable
  68. return q
  69. }
  70. // Creates the query source for the common query.
  71. func (q CommonQuery) Source() interface{} {
  72. // {
  73. // "common": {
  74. // "body": {
  75. // "query": "this is bonsai cool",
  76. // "cutoff_frequency": 0.001
  77. // }
  78. // }
  79. // }
  80. source := make(map[string]interface{})
  81. body := make(map[string]interface{})
  82. query := make(map[string]interface{})
  83. source["common"] = body
  84. body[q.name] = query
  85. query["query"] = q.query
  86. if q.cutoffFreq != nil {
  87. query["cutoff_frequency"] = *(q.cutoffFreq)
  88. }
  89. if q.highFreq != nil {
  90. query["high_freq"] = *(q.highFreq)
  91. }
  92. if q.highFreqOp != "" {
  93. query["high_freq_operator"] = q.highFreqOp
  94. }
  95. if q.lowFreq != nil {
  96. query["low_freq"] = *(q.lowFreq)
  97. }
  98. if q.lowFreqOp != "" {
  99. query["low_freq_operator"] = q.lowFreqOp
  100. }
  101. if q.lowFreqMinMatch != nil || q.highFreqMinMatch != nil {
  102. mm := make(map[string]interface{})
  103. if q.lowFreqMinMatch != nil {
  104. mm["low_freq"] = q.lowFreqMinMatch
  105. }
  106. if q.highFreqMinMatch != nil {
  107. mm["high_freq"] = q.highFreqMinMatch
  108. }
  109. query["minimum_should_match"] = mm
  110. }
  111. if q.analyzer != "" {
  112. query["analyzer"] = q.analyzer
  113. }
  114. if q.disableCoords != nil {
  115. query["disable_coords"] = *(q.disableCoords)
  116. }
  117. if q.boost != nil {
  118. query["boost"] = *(q.boost)
  119. }
  120. return source
  121. }