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// A filter that matches documents using AND boolean operator 8// on other filters. Can be placed within queries that accept a filter. 9// For details, see: 10// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-and-filter.html 11type AndFilter struct { 12 filters []Filter 13 cache *bool 14 cacheKey string 15 filterName string 16} 17 18func NewAndFilter(filters ...Filter) AndFilter { 19 f := AndFilter{ 20 filters: make([]Filter, 0), 21 } 22 if len(filters) > 0 { 23 f.filters = append(f.filters, filters...) 24 } 25 return f 26} 27 28func (f AndFilter) Add(filter Filter) AndFilter { 29 f.filters = append(f.filters, filter) 30 return f 31} 32 33func (f AndFilter) Cache(cache bool) AndFilter { 34 f.cache = &cache 35 return f 36} 37 38func (f AndFilter) CacheKey(cacheKey string) AndFilter { 39 f.cacheKey = cacheKey 40 return f 41} 42 43func (f AndFilter) FilterName(filterName string) AndFilter { 44 f.filterName = filterName 45 return f 46} 47 48func (f AndFilter) Source() interface{} { 49 // { 50 // "and" : [ 51 // ... filters ... 52 // ] 53 // } 54 55 source := make(map[string]interface{}) 56 57 params := make(map[string]interface{}) 58 source["and"] = params 59 60 filters := make([]interface{}, 0) 61 for _, filter := range f.filters { 62 filters = append(filters, filter.Source()) 63 } 64 params["filters"] = filters 65 66 if f.cache != nil { 67 params["_cache"] = *f.cache 68 } 69 if f.cacheKey != "" { 70 params["_cache_key"] = f.cacheKey 71 } 72 if f.filterName != "" { 73 params["_name"] = f.filterName 74 } 75 return source 76} 77