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 allowing to include hits that only fall within a polygon of points. 8// For details, see: 9// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-filter.html 10type GeoPolygonFilter struct { 11 Filter 12 name string 13 points []*GeoPoint 14 cache *bool 15 cacheKey string 16 filterName string 17} 18 19func NewGeoPolygonFilter(name string) GeoPolygonFilter { 20 f := GeoPolygonFilter{name: name, points: make([]*GeoPoint, 0)} 21 return f 22} 23 24func (f GeoPolygonFilter) Cache(cache bool) GeoPolygonFilter { 25 f.cache = &cache 26 return f 27} 28 29func (f GeoPolygonFilter) CacheKey(cacheKey string) GeoPolygonFilter { 30 f.cacheKey = cacheKey 31 return f 32} 33 34func (f GeoPolygonFilter) FilterName(filterName string) GeoPolygonFilter { 35 f.filterName = filterName 36 return f 37} 38 39func (f GeoPolygonFilter) AddPoint(point *GeoPoint) GeoPolygonFilter { 40 f.points = append(f.points, point) 41 return f 42} 43 44func (f GeoPolygonFilter) Source() interface{} { 45 // "geo_polygon" : { 46 // "person.location" : { 47 // "points" : [ 48 // {"lat" : 40, "lon" : -70}, 49 // {"lat" : 30, "lon" : -80}, 50 // {"lat" : 20, "lon" : -90} 51 // ] 52 // } 53 // } 54 source := make(map[string]interface{}) 55 56 params := make(map[string]interface{}) 57 source["geo_polygon"] = params 58 59 polygon := make(map[string]interface{}) 60 params[f.name] = polygon 61 62 points := make([]interface{}, 0) 63 for _, point := range f.points { 64 points = append(points, point.Source()) 65 } 66 polygon["points"] = points 67 68 if f.filterName != "" { 69 params["_name"] = f.filterName 70 } 71 72 if f.cache != nil { 73 params["_cache"] = *f.cache 74 } 75 76 if f.cacheKey != "" { 77 params["_cache_key"] = f.cacheKey 78 } 79 80 return source 81} 82