search_queries_match_all.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // MatchAllQuery is the most simple query, which matches all documents,
  6. // giving them all a _score of 1.0.
  7. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-match-all-query.html
  10. type MatchAllQuery struct {
  11. boost *float64
  12. queryName string
  13. }
  14. // NewMatchAllQuery creates and initializes a new match all query.
  15. func NewMatchAllQuery() *MatchAllQuery {
  16. return &MatchAllQuery{}
  17. }
  18. // Boost sets the boost for this query. Documents matching this query will
  19. // (in addition to the normal weightings) have their score multiplied by the
  20. // boost provided.
  21. func (q *MatchAllQuery) Boost(boost float64) *MatchAllQuery {
  22. q.boost = &boost
  23. return q
  24. }
  25. // QueryName sets the query name.
  26. func (q *MatchAllQuery) QueryName(name string) *MatchAllQuery {
  27. q.queryName = name
  28. return q
  29. }
  30. // Source returns JSON for the match all query.
  31. func (q MatchAllQuery) Source() (interface{}, error) {
  32. // {
  33. // "match_all" : { ... }
  34. // }
  35. source := make(map[string]interface{})
  36. params := make(map[string]interface{})
  37. source["match_all"] = params
  38. if q.boost != nil {
  39. params["boost"] = *q.boost
  40. }
  41. if q.queryName != "" {
  42. params["_name"] = q.queryName
  43. }
  44. return source, nil
  45. }