| 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 |
// IndicesQuery can be used when executed across multiple indices, allowing |
| 8 |
// to have a query that executes only when executed on an index that matches |
| 9 |
// a specific list of indices, and another query that executes when it is |
| 10 |
// executed on an index that does not match the listed indices. |
| 11 |
// |
| 12 |
// For more details, see |
| 13 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-indices-query.html |
| 14 |
type IndicesQuery struct { |
| 15 |
query Query |
| 16 |
indices []string |
| 17 |
noMatchQueryType string |
| 18 |
noMatchQuery Query |
| 19 |
queryName string |
| 20 |
} |
| 21 |
|
| 22 |
// NewIndicesQuery creates and initializes a new indices query. |
| 23 |
func NewIndicesQuery(query Query, indices ...string) *IndicesQuery { |
| 24 |
return &IndicesQuery{ |
| 25 |
query: query, |
| 26 |
indices: indices, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// NoMatchQuery sets the query to use when it executes on an index that |
| 31 |
// does not match the indices provided. |
| 32 |
func (q *IndicesQuery) NoMatchQuery(query Query) *IndicesQuery { |
| 33 |
q.noMatchQuery = query |
| 34 |
return q |
| 35 |
} |
| 36 |
|
| 37 |
// NoMatchQueryType sets the no match query which can be either all or none. |
| 38 |
func (q *IndicesQuery) NoMatchQueryType(typ string) *IndicesQuery { |
| 39 |
q.noMatchQueryType = typ |
| 40 |
return q |
| 41 |
} |
| 42 |
|
| 43 |
// QueryName sets the query name for the filter. |
| 44 |
func (q *IndicesQuery) QueryName(queryName string) *IndicesQuery { |
| 45 |
q.queryName = queryName |
| 46 |
return q |
| 47 |
} |
| 48 |
|
| 49 |
// Source returns JSON for the function score query. |
| 50 |
func (q *IndicesQuery) Source() (interface{}, error) { |
| 51 |
// { |
| 52 |
// "indices" : { |
| 53 |
// "indices" : ["index1", "index2"], |
| 54 |
// "query" : { |
| 55 |
// "term" : { "tag" : "wow" } |
| 56 |
// }, |
| 57 |
// "no_match_query" : { |
| 58 |
// "term" : { "tag" : "kow" } |
| 59 |
// } |
| 60 |
// } |
| 61 |
// } |
| 62 |
|
| 63 |
source := make(map[string]interface{}) |
| 64 |
params := make(map[string]interface{}) |
| 65 |
source["indices"] = params |
| 66 |
|
| 67 |
params["indices"] = q.indices |
| 68 |
|
| 69 |
src, err := q.query.Source() |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
params["query"] = src |
| 74 |
|
| 75 |
if q.noMatchQuery != nil { |
| 76 |
src, err := q.noMatchQuery.Source() |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
params["no_match_query"] = src |
| 81 |
} else if q.noMatchQueryType != "" { |
| 82 |
params["no_match_query"] = q.noMatchQueryType |
| 83 |
} |
| 84 |
if q.queryName != "" { |
| 85 |
params["_name"] = q.queryName |
| 86 |
} |
| 87 |
|
| 88 |
return source, nil |
| 89 |
} |