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// RegexpFilter allows filtering for regular expressions. 8// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-filter.html 9// and http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax 10// for details. 11type RegexpFilter struct { 12 Filter 13 name string 14 regexp string 15 flags *string 16 maxDeterminizedStates *int 17 cache *bool 18 cacheKey string 19 filterName string 20} 21 22// NewRegexpFilter sets up a new RegexpFilter. 23func NewRegexpFilter(name, regexp string) RegexpFilter { 24 return RegexpFilter{name: name, regexp: regexp} 25} 26 27// Flags sets the regexp flags. 28// See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#_optional_operators 29// for details. 30func (f RegexpFilter) Flags(flags string) RegexpFilter { 31 f.flags = &flags 32 return f 33} 34 35func (f RegexpFilter) MaxDeterminizedStates(maxDeterminizedStates int) RegexpFilter { 36 f.maxDeterminizedStates = &maxDeterminizedStates 37 return f 38} 39 40func (f RegexpFilter) Cache(cache bool) RegexpFilter { 41 f.cache = &cache 42 return f 43} 44 45func (f RegexpFilter) CacheKey(cacheKey string) RegexpFilter { 46 f.cacheKey = cacheKey 47 return f 48} 49 50func (f RegexpFilter) FilterName(filterName string) RegexpFilter { 51 f.filterName = filterName 52 return f 53} 54 55func (f RegexpFilter) Source() interface{} { 56 // { 57 // "regexp" : { 58 // "..." : "..." 59 // } 60 // } 61 62 source := make(map[string]interface{}) 63 64 params := make(map[string]interface{}) 65 source["regexp"] = params 66 67 if f.flags == nil { 68 params[f.name] = f.regexp 69 } else { 70 x := make(map[string]interface{}) 71 x["value"] = f.regexp 72 x["flags"] = *f.flags 73 if f.maxDeterminizedStates != nil { 74 x["max_determinized_states"] = *f.maxDeterminizedStates 75 } 76 params[f.name] = x 77 } 78 79 if f.filterName != "" { 80 params["_name"] = f.filterName 81 } 82 if f.cache != nil { 83 params["_cache"] = *f.cache 84 } 85 if f.cacheKey != "" { 86 params["_cache_key"] = f.cacheKey 87 } 88 89 return source 90} 91