search_queries_has_parent.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // HasParentQuery accepts a query and a parent type. The query is executed
  6. // in the parent document space which is specified by the parent type.
  7. // This query returns child documents which associated parents have matched.
  8. // For the rest has_parent query has the same options and works in the
  9. // same manner as has_child query.
  10. //
  11. // For more details, see
  12. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-has-parent-query.html
  13. type HasParentQuery struct {
  14. query Query
  15. parentType string
  16. boost *float64
  17. score *bool
  18. queryName string
  19. innerHit *InnerHit
  20. }
  21. // NewHasParentQuery creates and initializes a new has_parent query.
  22. func NewHasParentQuery(parentType string, query Query) *HasParentQuery {
  23. return &HasParentQuery{
  24. query: query,
  25. parentType: parentType,
  26. }
  27. }
  28. // Boost sets the boost for this query.
  29. func (q *HasParentQuery) Boost(boost float64) *HasParentQuery {
  30. q.boost = &boost
  31. return q
  32. }
  33. // Score defines if the parent score is mapped into the child documents.
  34. func (q *HasParentQuery) Score(score bool) *HasParentQuery {
  35. q.score = &score
  36. return q
  37. }
  38. // QueryName specifies the query name for the filter that can be used when
  39. // searching for matched filters per hit.
  40. func (q *HasParentQuery) QueryName(queryName string) *HasParentQuery {
  41. q.queryName = queryName
  42. return q
  43. }
  44. // InnerHit sets the inner hit definition in the scope of this query and
  45. // reusing the defined type and query.
  46. func (q *HasParentQuery) InnerHit(innerHit *InnerHit) *HasParentQuery {
  47. q.innerHit = innerHit
  48. return q
  49. }
  50. // Source returns JSON for the function score query.
  51. func (q *HasParentQuery) Source() (interface{}, error) {
  52. // {
  53. // "has_parent" : {
  54. // "parent_type" : "blog",
  55. // "query" : {
  56. // "term" : {
  57. // "tag" : "something"
  58. // }
  59. // }
  60. // }
  61. // }
  62. source := make(map[string]interface{})
  63. query := make(map[string]interface{})
  64. source["has_parent"] = query
  65. src, err := q.query.Source()
  66. if err != nil {
  67. return nil, err
  68. }
  69. query["query"] = src
  70. query["parent_type"] = q.parentType
  71. if q.boost != nil {
  72. query["boost"] = *q.boost
  73. }
  74. if q.score != nil {
  75. query["score"] = *q.score
  76. }
  77. if q.queryName != "" {
  78. query["_name"] = q.queryName
  79. }
  80. if q.innerHit != nil {
  81. src, err := q.innerHit.Source()
  82. if err != nil {
  83. return nil, err
  84. }
  85. query["inner_hits"] = src
  86. }
  87. return source, nil
  88. }