search_queries_has_child.go 2.0 KB

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