search_queries_has_child.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // HasChildQuery accepts a query and the child type to run against, and results
  6. // in parent documents that have child docs matching the query.
  7. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-has-child-query.html
  10. type HasChildQuery struct {
  11. query Query
  12. childType string
  13. boost *float64
  14. scoreMode string
  15. minChildren *int
  16. maxChildren *int
  17. shortCircuitCutoff *int
  18. queryName string
  19. innerHit *InnerHit
  20. }
  21. // NewHasChildQuery creates and initializes a new has_child query.
  22. func NewHasChildQuery(childType string, query Query) *HasChildQuery {
  23. return &HasChildQuery{
  24. query: query,
  25. childType: childType,
  26. }
  27. }
  28. // Boost sets the boost for this query.
  29. func (q *HasChildQuery) Boost(boost float64) *HasChildQuery {
  30. q.boost = &boost
  31. return q
  32. }
  33. // ScoreMode defines how the scores from the matching child documents
  34. // are mapped into the parent document. Allowed values are: min, max,
  35. // avg, or none.
  36. func (q *HasChildQuery) ScoreMode(scoreMode string) *HasChildQuery {
  37. q.scoreMode = scoreMode
  38. return q
  39. }
  40. // MinChildren defines the minimum number of children that are required
  41. // to match for the parent to be considered a match.
  42. func (q *HasChildQuery) MinChildren(minChildren int) *HasChildQuery {
  43. q.minChildren = &minChildren
  44. return q
  45. }
  46. // MaxChildren defines the maximum number of children that are required
  47. // to match for the parent to be considered a match.
  48. func (q *HasChildQuery) MaxChildren(maxChildren int) *HasChildQuery {
  49. q.maxChildren = &maxChildren
  50. return q
  51. }
  52. // ShortCircuitCutoff configures what cut off point only to evaluate
  53. // parent documents that contain the matching parent id terms instead
  54. // of evaluating all parent docs.
  55. func (q *HasChildQuery) ShortCircuitCutoff(shortCircuitCutoff int) *HasChildQuery {
  56. q.shortCircuitCutoff = &shortCircuitCutoff
  57. return q
  58. }
  59. // QueryName specifies the query name for the filter that can be used when
  60. // searching for matched filters per hit.
  61. func (q *HasChildQuery) QueryName(queryName string) *HasChildQuery {
  62. q.queryName = queryName
  63. return q
  64. }
  65. // InnerHit sets the inner hit definition in the scope of this query and
  66. // reusing the defined type and query.
  67. func (q *HasChildQuery) InnerHit(innerHit *InnerHit) *HasChildQuery {
  68. q.innerHit = innerHit
  69. return q
  70. }
  71. // Source returns JSON for the function score query.
  72. func (q *HasChildQuery) Source() (interface{}, error) {
  73. // {
  74. // "has_child" : {
  75. // "type" : "blog_tag",
  76. // "score_mode" : "min",
  77. // "query" : {
  78. // "term" : {
  79. // "tag" : "something"
  80. // }
  81. // }
  82. // }
  83. // }
  84. source := make(map[string]interface{})
  85. query := make(map[string]interface{})
  86. source["has_child"] = query
  87. src, err := q.query.Source()
  88. if err != nil {
  89. return nil, err
  90. }
  91. query["query"] = src
  92. query["type"] = q.childType
  93. if q.boost != nil {
  94. query["boost"] = *q.boost
  95. }
  96. if q.scoreMode != "" {
  97. query["score_mode"] = q.scoreMode
  98. }
  99. if q.minChildren != nil {
  100. query["min_children"] = *q.minChildren
  101. }
  102. if q.maxChildren != nil {
  103. query["max_children"] = *q.maxChildren
  104. }
  105. if q.shortCircuitCutoff != nil {
  106. query["short_circuit_cutoff"] = *q.shortCircuitCutoff
  107. }
  108. if q.queryName != "" {
  109. query["_name"] = q.queryName
  110. }
  111. if q.innerHit != nil {
  112. src, err := q.innerHit.Source()
  113. if err != nil {
  114. return nil, err
  115. }
  116. query["inner_hits"] = src
  117. }
  118. return source, nil
  119. }