search_queries_has_parent.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 has_parent query works the same as the has_parent filter,
  6. // by automatically wrapping the filter with a
  7. // constant_score (when using the default score type).
  8. // It has the same syntax as the has_parent filter.
  9. // For more details, see
  10. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html
  11. type HasParentQuery struct {
  12. query Query
  13. parentType string
  14. boost *float32
  15. scoreType string
  16. queryName string
  17. }
  18. // NewHasParentQuery creates a new has_parent query.
  19. func NewHasParentQuery(parentType string, query Query) HasParentQuery {
  20. q := HasParentQuery{
  21. query: query,
  22. parentType: parentType,
  23. }
  24. return q
  25. }
  26. func (q HasParentQuery) Boost(boost float32) HasParentQuery {
  27. q.boost = &boost
  28. return q
  29. }
  30. func (q HasParentQuery) ScoreType(scoreType string) HasParentQuery {
  31. q.scoreType = scoreType
  32. return q
  33. }
  34. func (q HasParentQuery) QueryName(queryName string) HasParentQuery {
  35. q.queryName = queryName
  36. return q
  37. }
  38. // Creates the query source for the ids query.
  39. func (q HasParentQuery) Source() interface{} {
  40. // {
  41. // "has_parent" : {
  42. // "parent_type" : "blog",
  43. // "query" : {
  44. // "term" : {
  45. // "tag" : "something"
  46. // }
  47. // }
  48. // }
  49. // }
  50. source := make(map[string]interface{})
  51. query := make(map[string]interface{})
  52. source["has_parent"] = query
  53. query["query"] = q.query.Source()
  54. query["parent_type"] = q.parentType
  55. if q.boost != nil {
  56. query["boost"] = *q.boost
  57. }
  58. if q.scoreType != "" {
  59. query["score_type"] = q.scoreType
  60. }
  61. if q.queryName != "" {
  62. query["_name"] = q.queryName
  63. }
  64. return source
  65. }