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