1// Copyright 2012-2015 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 5package elastic 6 7// RegexpQuery allows you to use regular expression term queries. 8// For more details, see 9// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html. 10type RegexpQuery struct { 11 Query 12 name string 13 regexp string 14 flags *string 15 boost *float64 16 rewrite *string 17 queryName *string 18 maxDeterminizedStates *int 19} 20 21// NewRegexpQuery creates a new regexp query. 22func NewRegexpQuery(name string, regexp string) RegexpQuery { 23 return RegexpQuery{name: name, regexp: regexp} 24} 25 26// Flags sets the regexp flags. 27// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#_optional_operators 28// for details. 29func (q RegexpQuery) Flags(flags string) RegexpQuery { 30 q.flags = &flags 31 return q 32} 33 34func (q RegexpQuery) MaxDeterminizedStates(maxDeterminizedStates int) RegexpQuery { 35 q.maxDeterminizedStates = &maxDeterminizedStates 36 return q 37} 38 39func (q RegexpQuery) Boost(boost float64) RegexpQuery { 40 q.boost = &boost 41 return q 42} 43 44func (q RegexpQuery) Rewrite(rewrite string) RegexpQuery { 45 q.rewrite = &rewrite 46 return q 47} 48 49func (q RegexpQuery) QueryName(queryName string) RegexpQuery { 50 q.queryName = &queryName 51 return q 52} 53 54// Source returns the JSON-serializable query data. 55func (q RegexpQuery) Source() interface{} { 56 // { 57 // "regexp" : { 58 // "name.first" : { 59 // "value" : "s.*y", 60 // "boost" : 1.2 61 // } 62 // } 63 // } 64 65 source := make(map[string]interface{}) 66 query := make(map[string]interface{}) 67 source["regexp"] = query 68 69 x := make(map[string]interface{}) 70 x["value"] = q.regexp 71 if q.flags != nil { 72 x["flags"] = *q.flags 73 } 74 if q.maxDeterminizedStates != nil { 75 x["max_determinized_states"] = *q.maxDeterminizedStates 76 } 77 if q.boost != nil { 78 x["boost"] = *q.boost 79 } 80 if q.rewrite != nil { 81 x["rewrite"] = *q.rewrite 82 } 83 if q.queryName != nil { 84 x["name"] = *q.queryName 85 } 86 query[q.name] = x 87 88 return source 89} 90