search_queries_nested.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // NestedQuery allows to query nested objects / docs.
  6. // The query is executed against the nested objects / docs as if they were
  7. // indexed as separate docs (they are, internally) and resulting in the
  8. // root parent doc (or parent nested mapping).
  9. //
  10. // For more details, see
  11. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-nested-query.html
  12. type NestedQuery struct {
  13. query Query
  14. path string
  15. scoreMode string
  16. boost *float64
  17. queryName string
  18. innerHit *InnerHit
  19. ignoreUnmapped *bool
  20. }
  21. // NewNestedQuery creates and initializes a new NestedQuery.
  22. func NewNestedQuery(path string, query Query) *NestedQuery {
  23. return &NestedQuery{path: path, query: query}
  24. }
  25. // ScoreMode specifies the score mode.
  26. func (q *NestedQuery) ScoreMode(scoreMode string) *NestedQuery {
  27. q.scoreMode = scoreMode
  28. return q
  29. }
  30. // Boost sets the boost for this query.
  31. func (q *NestedQuery) Boost(boost float64) *NestedQuery {
  32. q.boost = &boost
  33. return q
  34. }
  35. // QueryName sets the query name for the filter that can be used
  36. // when searching for matched_filters per hit
  37. func (q *NestedQuery) QueryName(queryName string) *NestedQuery {
  38. q.queryName = queryName
  39. return q
  40. }
  41. // InnerHit sets the inner hit definition in the scope of this nested query
  42. // and reusing the defined path and query.
  43. func (q *NestedQuery) InnerHit(innerHit *InnerHit) *NestedQuery {
  44. q.innerHit = innerHit
  45. return q
  46. }
  47. // IgnoreUnmapped sets the ignore_unmapped option for the filter that ignores
  48. // unmapped nested fields
  49. func (q *NestedQuery) IgnoreUnmapped(value bool) *NestedQuery {
  50. q.ignoreUnmapped = &value
  51. return q
  52. }
  53. // Source returns JSON for the query.
  54. func (q *NestedQuery) Source() (interface{}, error) {
  55. query := make(map[string]interface{})
  56. nq := make(map[string]interface{})
  57. query["nested"] = nq
  58. src, err := q.query.Source()
  59. if err != nil {
  60. return nil, err
  61. }
  62. nq["query"] = src
  63. nq["path"] = q.path
  64. if q.scoreMode != "" {
  65. nq["score_mode"] = q.scoreMode
  66. }
  67. if q.boost != nil {
  68. nq["boost"] = *q.boost
  69. }
  70. if q.queryName != "" {
  71. nq["_name"] = q.queryName
  72. }
  73. if q.ignoreUnmapped != nil {
  74. nq["ignore_unmapped"] = *q.ignoreUnmapped
  75. }
  76. if q.innerHit != nil {
  77. src, err := q.innerHit.Source()
  78. if err != nil {
  79. return nil, err
  80. }
  81. nq["inner_hits"] = src
  82. }
  83. return query, nil
  84. }