forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoShapeFilterParser.java
198 lines (172 loc) · 8.48 KB
/
GeoShapeFilterParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.query;
import com.spatial4j.core.shape.Shape;
import org.apache.lucene.search.Filter;
import org.apache.lucene.spatial.prefix.PrefixTreeStrategy;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.internal.Nullable;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.geo.GeoShapeFieldMapper;
import org.elasticsearch.index.search.shape.ShapeFetchService;
import java.io.IOException;
import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;
/**
* {@link FilterParser} for filtering Documents based on {@link Shape}s.
* <p/>
* Only those fields mapped using {@link GeoShapeFieldMapper} can be filtered
* using this parser.
* <p/>
* Format supported:
* <p/>
* <pre>
* "field" : {
* "relation" : "intersects",
* "shape" : {
* "type" : "polygon",
* "coordinates" : [
* [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
* ]
* }
* }
* </pre>
*/
public class GeoShapeFilterParser implements FilterParser {
public static final String NAME = "geo_shape";
private ShapeFetchService fetchService;
public static class DEFAULTS {
public static final String INDEX_NAME = "shapes";
public static final String SHAPE_FIELD_NAME = "shape";
}
@Override
public String[] names() {
return new String[]{NAME, "geoShape"};
}
@Override
public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
String fieldName = null;
ShapeRelation shapeRelation = ShapeRelation.INTERSECTS;
String strategyName = null;
ShapeBuilder shape = null;
boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String filterName = null;
String id = null;
String type = null;
String index = DEFAULTS.INDEX_NAME;
String shapePath = DEFAULTS.SHAPE_FIELD_NAME;
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
token = parser.nextToken();
if ("shape".equals(currentFieldName)) {
shape = ShapeBuilder.parse(parser);
} else if ("relation".equals(currentFieldName)) {
shapeRelation = ShapeRelation.getRelationByName(parser.text());
if (shapeRelation == null) {
throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + "]");
}
} else if ("strategy".equals(currentFieldName)) {
strategyName = parser.text();
} else if ("indexed_shape".equals(currentFieldName) || "indexedShape".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("id".equals(currentFieldName)) {
id = parser.text();
} else if ("type".equals(currentFieldName)) {
type = parser.text();
} else if ("index".equals(currentFieldName)) {
index = parser.text();
} else if ("path".equals(currentFieldName)) {
shapePath = parser.text();
}
}
}
if (id == null) {
throw new QueryParsingException(parseContext.index(), "ID for indexed shape not provided");
} else if (type == null) {
throw new QueryParsingException(parseContext.index(), "Type for indexed shape not provided");
}
shape = fetchService.fetch(id, type, index, shapePath);
} else {
throw new QueryParsingException(parseContext.index(), "[geo_shape] filter does not support [" + currentFieldName + "]");
}
}
}
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
filterName = parser.text();
} else if ("_cache".equals(currentFieldName)) {
cache = parser.booleanValue();
} else if ("_cache_key".equals(currentFieldName)) {
cacheKey = new CacheKeyFilter.Key(parser.text());
} else {
throw new QueryParsingException(parseContext.index(), "[geo_shape] filter does not support [" + currentFieldName + "]");
}
}
}
if (shape == null) {
throw new QueryParsingException(parseContext.index(), "No Shape defined");
} else if (shapeRelation == null) {
throw new QueryParsingException(parseContext.index(), "No Shape Relation defined");
}
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers == null || !smartNameFieldMappers.hasMapper()) {
throw new QueryParsingException(parseContext.index(), "Failed to find geo_shape field [" + fieldName + "]");
}
FieldMapper fieldMapper = smartNameFieldMappers.mapper();
// TODO: This isn't the nicest way to check this
if (!(fieldMapper instanceof GeoShapeFieldMapper)) {
throw new QueryParsingException(parseContext.index(), "Field [" + fieldName + "] is not a geo_shape");
}
GeoShapeFieldMapper shapeFieldMapper = (GeoShapeFieldMapper) fieldMapper;
PrefixTreeStrategy strategy = shapeFieldMapper.defaultStrategy();
if (strategyName != null) {
strategy = shapeFieldMapper.resolveStrategy(strategyName);
}
Filter filter = strategy.makeFilter(GeoShapeQueryParser.getArgs(shape, shapeRelation));
if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}
filter = wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext);
if (filterName != null) {
parseContext.addNamedFilter(filterName, filter);
}
return filter;
}
@Inject(optional = true)
public void setFetchService(@Nullable ShapeFetchService fetchService) {
this.fetchService = fetchService;
}
}