search_queries_parent_id.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // ParentIdQuery can be used to find child documents which belong to a
  6. // particular parent. Given the following mapping definition.
  7. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-parent-id-query.html
  10. type ParentIdQuery struct {
  11. typ string
  12. id string
  13. ignoreUnmapped *bool
  14. boost *float64
  15. queryName string
  16. innerHit *InnerHit
  17. }
  18. // NewParentIdQuery creates and initializes a new parent_id query.
  19. func NewParentIdQuery(typ, id string) *ParentIdQuery {
  20. return &ParentIdQuery{
  21. typ: typ,
  22. id: id,
  23. }
  24. }
  25. // Type sets the parent type.
  26. func (q *ParentIdQuery) Type(typ string) *ParentIdQuery {
  27. q.typ = typ
  28. return q
  29. }
  30. // Id sets the id.
  31. func (q *ParentIdQuery) Id(id string) *ParentIdQuery {
  32. q.id = id
  33. return q
  34. }
  35. // IgnoreUnmapped specifies whether unmapped types should be ignored.
  36. // If set to false, the query failes when an unmapped type is found.
  37. func (q *ParentIdQuery) IgnoreUnmapped(ignore bool) *ParentIdQuery {
  38. q.ignoreUnmapped = &ignore
  39. return q
  40. }
  41. // Boost sets the boost for this query.
  42. func (q *ParentIdQuery) Boost(boost float64) *ParentIdQuery {
  43. q.boost = &boost
  44. return q
  45. }
  46. // QueryName specifies the query name for the filter that can be used when
  47. // searching for matched filters per hit.
  48. func (q *ParentIdQuery) QueryName(queryName string) *ParentIdQuery {
  49. q.queryName = queryName
  50. return q
  51. }
  52. // InnerHit sets the inner hit definition in the scope of this query and
  53. // reusing the defined type and query.
  54. func (q *ParentIdQuery) InnerHit(innerHit *InnerHit) *ParentIdQuery {
  55. q.innerHit = innerHit
  56. return q
  57. }
  58. // Source returns JSON for the parent_id query.
  59. func (q *ParentIdQuery) Source() (interface{}, error) {
  60. // {
  61. // "parent_id" : {
  62. // "type" : "blog",
  63. // "id" : "1"
  64. // }
  65. // }
  66. source := make(map[string]interface{})
  67. query := make(map[string]interface{})
  68. source["parent_id"] = query
  69. query["type"] = q.typ
  70. query["id"] = q.id
  71. if q.boost != nil {
  72. query["boost"] = *q.boost
  73. }
  74. if q.ignoreUnmapped != nil {
  75. query["ignore_unmapped"] = *q.ignoreUnmapped
  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. }