search_queries_exists.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // ExistsQuery is a query that only matches on documents that the field
  6. // has a value in them.
  7. //
  8. // For more details, see:
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-exists-query.html
  10. type ExistsQuery struct {
  11. name string
  12. queryName string
  13. }
  14. // NewExistsQuery creates and initializes a new dis max query.
  15. func NewExistsQuery(name string) *ExistsQuery {
  16. return &ExistsQuery{
  17. name: name,
  18. }
  19. }
  20. // QueryName sets the query name for the filter that can be used
  21. // when searching for matched queries per hit.
  22. func (q *ExistsQuery) QueryName(queryName string) *ExistsQuery {
  23. q.queryName = queryName
  24. return q
  25. }
  26. // Source returns the JSON serializable content for this query.
  27. func (q *ExistsQuery) Source() (interface{}, error) {
  28. // {
  29. // "exists" : {
  30. // "field" : "user"
  31. // }
  32. // }
  33. query := make(map[string]interface{})
  34. params := make(map[string]interface{})
  35. query["exists"] = params
  36. params["field"] = q.name
  37. if q.queryName != "" {
  38. params["_name"] = q.queryName
  39. }
  40. return query, nil
  41. }