Skip to content

Commit 6e66f45

Browse files
committed
minor geo shape fetch improvements
1 parent 05e0b4d commit 6e66f45

File tree

6 files changed

+122
-26
lines changed

6 files changed

+122
-26
lines changed

src/main/java/org/elasticsearch/index/query/GeoShapeFilterBuilder.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.elasticsearch.index.query;
221

322
import com.spatial4j.core.shape.Shape;
@@ -33,7 +52,7 @@ public class GeoShapeFilterBuilder extends BaseFilterBuilder {
3352
* Creates a new GeoShapeFilterBuilder whose Filter will be against the
3453
* given field name using the given Shape
3554
*
36-
* @param name Name of the field that will be filtered
55+
* @param name Name of the field that will be filtered
3756
* @param shape Shape used in the filter
3857
*/
3958
public GeoShapeFilterBuilder(String name, Shape shape) {
@@ -44,8 +63,8 @@ public GeoShapeFilterBuilder(String name, Shape shape) {
4463
* Creates a new GeoShapeFilterBuilder whose Filter will be against the given field name
4564
* and will use the Shape found with the given ID in the given type
4665
*
47-
* @param name Name of the field that will be filtered
48-
* @param indexedShapeId ID of the indexed Shape that will be used in the Filter
66+
* @param name Name of the field that will be filtered
67+
* @param indexedShapeId ID of the indexed Shape that will be used in the Filter
4968
* @param indexedShapeType Index type of the indexed Shapes
5069
*/
5170
public GeoShapeFilterBuilder(String name, String indexedShapeId, String indexedShapeType) {

src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.elasticsearch.index.query;
221

322
import com.spatial4j.core.shape.Shape;
423
import org.apache.lucene.search.Filter;
5-
import org.elasticsearch.common.geo.ShapeRelation;
624
import org.elasticsearch.common.geo.GeoJSONShapeParser;
25+
import org.elasticsearch.common.geo.ShapeRelation;
726
import org.elasticsearch.common.inject.Inject;
827
import org.elasticsearch.common.inject.internal.Nullable;
928
import org.elasticsearch.common.xcontent.XContentParser;
@@ -19,12 +38,13 @@
1938

2039
/**
2140
* {@link FilterParser} for filtering Documents based on {@link Shape}s.
22-
*
41+
* <p/>
2342
* Only those fields mapped using {@link GeoShapeFieldMapper} can be filtered
2443
* using this parser.
25-
*
44+
* <p/>
2645
* Format supported:
27-
*
46+
* <p/>
47+
* <pre>
2848
* "field" : {
2949
* "relation" : "intersects",
3050
* "shape" : {
@@ -34,6 +54,7 @@
3454
* ]
3555
* }
3656
* }
57+
* </pre>
3758
*/
3859
public class GeoShapeFilterParser implements FilterParser {
3960

@@ -88,7 +109,7 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
88109
if (shapeRelation == null) {
89110
throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + " ]");
90111
}
91-
} else if ("indexed_shape".equals(currentFieldName)) {
112+
} else if ("indexed_shape".equals(currentFieldName) || "indexedShape".equals(currentFieldName)) {
92113
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
93114
if (token == XContentParser.Token.FIELD_NAME) {
94115
currentFieldName = parser.currentName();

src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.elasticsearch.index.query;
221

322
import com.spatial4j.core.shape.Shape;
@@ -29,18 +48,19 @@ public class GeoShapeQueryBuilder extends BaseQueryBuilder implements BoostableQ
2948
* Creates a new GeoShapeQueryBuilder whose Query will be against the
3049
* given field name using the given Shape
3150
*
32-
* @param name Name of the field that will be queried
51+
* @param name Name of the field that will be queried
3352
* @param shape Shape used in the query
3453
*/
3554
public GeoShapeQueryBuilder(String name, Shape shape) {
3655
this(name, shape, null, null);
3756
}
57+
3858
/**
3959
* Creates a new GeoShapeQueryBuilder whose Query will be against the given field name
4060
* and will use the Shape found with the given ID in the given type
4161
*
42-
* @param name Name of the field that will be queried
43-
* @param indexedShapeId ID of the indexed Shape that will be used in the Query
62+
* @param name Name of the field that will be queried
63+
* @param indexedShapeId ID of the indexed Shape that will be used in the Query
4464
* @param indexedShapeType Index type of the indexed Shapes
4565
*/
4666
public GeoShapeQueryBuilder(String name, String indexedShapeId, String indexedShapeType) {

src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.elasticsearch.index.query;
221

322
import com.spatial4j.core.shape.Shape;
@@ -57,7 +76,6 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
5776
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
5877
if (token == XContentParser.Token.FIELD_NAME) {
5978
currentFieldName = parser.currentName();
60-
6179
token = parser.nextToken();
6280
if ("shape".equals(currentFieldName)) {
6381
shape = GeoJSONShapeParser.parse(parser);
@@ -66,7 +84,7 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
6684
if (shapeRelation == null) {
6785
throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + " ]");
6886
}
69-
} else if ("indexed_shape".equals(currentFieldName)) {
87+
} else if ("indexed_shape".equals(currentFieldName) || "indexedShape".equals(currentFieldName)) {
7088
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
7189
if (token == XContentParser.Token.FIELD_NAME) {
7290
currentFieldName = parser.currentName();

src/main/java/org/elasticsearch/index/search/shape/ShapeFetchService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
import com.spatial4j.core.shape.Shape;
2323
import org.elasticsearch.ElasticSearchIllegalArgumentException;
2424
import org.elasticsearch.ElasticSearchIllegalStateException;
25+
import org.elasticsearch.action.get.GetRequest;
2526
import org.elasticsearch.action.get.GetResponse;
2627
import org.elasticsearch.client.Client;
2728
import org.elasticsearch.common.component.AbstractComponent;
2829
import org.elasticsearch.common.geo.GeoJSONShapeParser;
2930
import org.elasticsearch.common.inject.Inject;
3031
import org.elasticsearch.common.settings.Settings;
32+
import org.elasticsearch.common.xcontent.XContentHelper;
3133
import org.elasticsearch.common.xcontent.XContentParser;
32-
import org.elasticsearch.common.xcontent.json.JsonXContent;
3334

3435
import java.io.IOException;
3536

@@ -49,22 +50,22 @@ public ShapeFetchService(Client client, Settings settings) {
4950
/**
5051
* Fetches the Shape with the given ID in the given type and index.
5152
*
52-
* @param id ID of the Shape to fetch
53-
* @param type Index type where the Shape is indexed
54-
* @param index Index where the Shape is indexed
53+
* @param id ID of the Shape to fetch
54+
* @param type Index type where the Shape is indexed
55+
* @param index Index where the Shape is indexed
5556
* @param shapeField Name of the field in the Shape Document where the Shape itself is located
5657
* @return Shape with the given ID
5758
* @throws IOException Can be thrown while parsing the Shape Document and extracting the Shape
5859
*/
5960
public Shape fetch(String id, String type, String index, String shapeField) throws IOException {
60-
GetResponse response = client.prepareGet(index, type, id).setPreference("_local").execute().actionGet();
61+
GetResponse response = client.get(new GetRequest(index, type, id).preference("_local")).actionGet();
6162
if (!response.exists()) {
6263
throw new ElasticSearchIllegalArgumentException("Shape with ID [" + id + "] in type [" + type + "] not found");
6364
}
6465

6566
XContentParser parser = null;
6667
try {
67-
parser = JsonXContent.jsonXContent.createParser(response.source());
68+
parser = XContentHelper.createParser(response.sourceRef());
6869
XContentParser.Token currentToken;
6970
while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
7071
if (currentToken == XContentParser.Token.FIELD_NAME) {

src/test/java/org/elasticsearch/test/integration/search/geo/GeoShapeIntegrationTests.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Licensed to ElasticSearch and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. ElasticSearch licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.elasticsearch.test.integration.search.geo;
221

322
import com.spatial4j.core.shape.Shape;
@@ -15,9 +34,7 @@
1534
import static org.elasticsearch.common.geo.ShapeBuilder.newRectangle;
1635
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
1736
import static org.elasticsearch.index.query.FilterBuilders.geoShapeFilter;
18-
import static org.elasticsearch.index.query.QueryBuilders.filteredQuery;
19-
import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery;
20-
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
37+
import static org.elasticsearch.index.query.QueryBuilders.*;
2138
import static org.hamcrest.MatcherAssert.assertThat;
2239
import static org.hamcrest.Matchers.equalTo;
2340

@@ -48,8 +65,8 @@ public void testIndexPointsFilterRectangle() throws Exception {
4865

4966
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1")
5067
.startObject("properties").startObject("location")
51-
.field("type", "geo_shape")
52-
.field("tree", "quadtree")
68+
.field("type", "geo_shape")
69+
.field("tree", "quadtree")
5370
.endObject().endObject()
5471
.endObject().endObject().string();
5572
client.admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet();
@@ -66,8 +83,8 @@ public void testIndexPointsFilterRectangle() throws Exception {
6683
client.prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject()
6784
.field("name", "Document 2")
6885
.startObject("location")
69-
.field("type", "point")
70-
.startArray("coordinates").value(-45).value(-50).endArray()
86+
.field("type", "point")
87+
.startArray("coordinates").value(-45).value(-50).endArray()
7188
.endObject()
7289
.endObject()).execute().actionGet();
7390

0 commit comments

Comments
 (0)