| 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 |
// WildcardQuery matches documents that have fields matching a wildcard |
| 8 |
// expression (not analyzed). Supported wildcards are *, which matches |
| 9 |
// any character sequence (including the empty one), and ?, which matches |
| 10 |
// any single character. Note this query can be slow, as it needs to iterate |
| 11 |
// over many terms. In order to prevent extremely slow wildcard queries, |
| 12 |
// a wildcard term should not start with one of the wildcards * or ?. |
| 13 |
// The wildcard query maps to Lucene WildcardQuery. |
| 14 |
// |
| 15 |
// For more details, see |
| 16 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-wildcard-query.html |
| 17 |
type WildcardQuery struct { |
| 18 |
name string |
| 19 |
wildcard string |
| 20 |
boost *float64 |
| 21 |
rewrite string |
| 22 |
queryName string |
| 23 |
} |
| 24 |
|
| 25 |
// NewWildcardQuery creates and initializes a new WildcardQuery. |
| 26 |
func NewWildcardQuery(name, wildcard string) *WildcardQuery { |
| 27 |
return &WildcardQuery{ |
| 28 |
name: name, |
| 29 |
wildcard: wildcard, |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// Boost sets the boost for this query. |
| 34 |
func (q *WildcardQuery) Boost(boost float64) *WildcardQuery { |
| 35 |
q.boost = &boost |
| 36 |
return q |
| 37 |
} |
| 38 |
|
| 39 |
func (q *WildcardQuery) Rewrite(rewrite string) *WildcardQuery { |
| 40 |
q.rewrite = rewrite |
| 41 |
return q |
| 42 |
} |
| 43 |
|
| 44 |
// QueryName sets the name of this query. |
| 45 |
func (q *WildcardQuery) QueryName(queryName string) *WildcardQuery { |
| 46 |
q.queryName = queryName |
| 47 |
return q |
| 48 |
} |
| 49 |
|
| 50 |
// Source returns the JSON serializable body of this query. |
| 51 |
func (q *WildcardQuery) Source() (interface{}, error) { |
| 52 |
// { |
| 53 |
// "wildcard" : { |
| 54 |
// "user" : { |
| 55 |
// "wildcard" : "ki*y", |
| 56 |
// "boost" : 1.0 |
| 57 |
// } |
| 58 |
// } |
| 59 |
|
| 60 |
source := make(map[string]interface{}) |
| 61 |
|
| 62 |
query := make(map[string]interface{}) |
| 63 |
source["wildcard"] = query |
| 64 |
|
| 65 |
wq := make(map[string]interface{}) |
| 66 |
query[q.name] = wq |
| 67 |
|
| 68 |
wq["wildcard"] = q.wildcard |
| 69 |
|
| 70 |
if q.boost != nil { |
| 71 |
wq["boost"] = *q.boost |
| 72 |
} |
| 73 |
if q.rewrite != "" { |
| 74 |
wq["rewrite"] = q.rewrite |
| 75 |
} |
| 76 |
if q.queryName != "" { |
| 77 |
wq["_name"] = q.queryName |
| 78 |
} |
| 79 |
|
| 80 |
return source, nil |
| 81 |
} |