From 6e66f45f58db2361540b1952b2e50d3983c3bc85 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sun, 23 Sep 2012 21:31:13 +0200 Subject: [PATCH] minor geo shape fetch improvements --- .../index/query/GeoShapeFilterBuilder.java | 25 +++++++++++++-- .../index/query/GeoShapeFilterParser.java | 31 ++++++++++++++++--- .../index/query/GeoShapeQueryBuilder.java | 26 ++++++++++++++-- .../index/query/GeoShapeQueryParser.java | 22 +++++++++++-- .../index/search/shape/ShapeFetchService.java | 13 ++++---- .../search/geo/GeoShapeIntegrationTests.java | 31 ++++++++++++++----- 6 files changed, 122 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterBuilder.java b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterBuilder.java index 35ec86ecaf1..01de7d05fed 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterBuilder.java @@ -1,3 +1,22 @@ +/* + * 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; @@ -33,7 +52,7 @@ public class GeoShapeFilterBuilder extends BaseFilterBuilder { * Creates a new GeoShapeFilterBuilder whose Filter will be against the * given field name using the given Shape * - * @param name Name of the field that will be filtered + * @param name Name of the field that will be filtered * @param shape Shape used in the filter */ public GeoShapeFilterBuilder(String name, Shape shape) { @@ -44,8 +63,8 @@ public class GeoShapeFilterBuilder extends BaseFilterBuilder { * Creates a new GeoShapeFilterBuilder whose Filter will be against the given field name * and will use the Shape found with the given ID in the given type * - * @param name Name of the field that will be filtered - * @param indexedShapeId ID of the indexed Shape that will be used in the Filter + * @param name Name of the field that will be filtered + * @param indexedShapeId ID of the indexed Shape that will be used in the Filter * @param indexedShapeType Index type of the indexed Shapes */ public GeoShapeFilterBuilder(String name, String indexedShapeId, String indexedShapeType) { diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java index 53ebeb3db70..e9a2ec0d0e2 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeFilterParser.java @@ -1,9 +1,28 @@ +/* + * 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.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.GeoJSONShapeParser; +import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.internal.Nullable; import org.elasticsearch.common.xcontent.XContentParser; @@ -19,12 +38,13 @@ import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFi /** * {@link FilterParser} for filtering Documents based on {@link Shape}s. - * + *

* Only those fields mapped using {@link GeoShapeFieldMapper} can be filtered * using this parser. - * + *

* Format supported: - * + *

+ *

  * "field" : {
  *     "relation" : "intersects",
  *     "shape" : {
@@ -34,6 +54,7 @@ import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFi
  *         ]
  *     }
  * }
+ * 
*/ public class GeoShapeFilterParser implements FilterParser { @@ -88,7 +109,7 @@ public class GeoShapeFilterParser implements FilterParser { if (shapeRelation == null) { throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + " ]"); } - } else if ("indexed_shape".equals(currentFieldName)) { + } 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(); diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java index ec2c594d56c..2d42289fec8 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryBuilder.java @@ -1,3 +1,22 @@ +/* + * 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; @@ -29,18 +48,19 @@ public class GeoShapeQueryBuilder extends BaseQueryBuilder implements BoostableQ * Creates a new GeoShapeQueryBuilder whose Query will be against the * given field name using the given Shape * - * @param name Name of the field that will be queried + * @param name Name of the field that will be queried * @param shape Shape used in the query */ public GeoShapeQueryBuilder(String name, Shape shape) { this(name, shape, null, null); } + /** * Creates a new GeoShapeQueryBuilder whose Query will be against the given field name * and will use the Shape found with the given ID in the given type * - * @param name Name of the field that will be queried - * @param indexedShapeId ID of the indexed Shape that will be used in the Query + * @param name Name of the field that will be queried + * @param indexedShapeId ID of the indexed Shape that will be used in the Query * @param indexedShapeType Index type of the indexed Shapes */ public GeoShapeQueryBuilder(String name, String indexedShapeId, String indexedShapeType) { diff --git a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java index afa12ca6e36..25eaaf6ebd3 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoShapeQueryParser.java @@ -1,3 +1,22 @@ +/* + * 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; @@ -57,7 +76,6 @@ public class GeoShapeQueryParser implements QueryParser { 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 = GeoJSONShapeParser.parse(parser); @@ -66,7 +84,7 @@ public class GeoShapeQueryParser implements QueryParser { if (shapeRelation == null) { throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + " ]"); } - } else if ("indexed_shape".equals(currentFieldName)) { + } 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(); diff --git a/src/main/java/org/elasticsearch/index/search/shape/ShapeFetchService.java b/src/main/java/org/elasticsearch/index/search/shape/ShapeFetchService.java index 8d2e8ad7d39..8cdd436570d 100644 --- a/src/main/java/org/elasticsearch/index/search/shape/ShapeFetchService.java +++ b/src/main/java/org/elasticsearch/index/search/shape/ShapeFetchService.java @@ -22,14 +22,15 @@ package org.elasticsearch.index.search.shape; import com.spatial4j.core.shape.Shape; import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalStateException; +import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.geo.GeoJSONShapeParser; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.json.JsonXContent; import java.io.IOException; @@ -49,22 +50,22 @@ public class ShapeFetchService extends AbstractComponent { /** * Fetches the Shape with the given ID in the given type and index. * - * @param id ID of the Shape to fetch - * @param type Index type where the Shape is indexed - * @param index Index where the Shape is indexed + * @param id ID of the Shape to fetch + * @param type Index type where the Shape is indexed + * @param index Index where the Shape is indexed * @param shapeField Name of the field in the Shape Document where the Shape itself is located * @return Shape with the given ID * @throws IOException Can be thrown while parsing the Shape Document and extracting the Shape */ public Shape fetch(String id, String type, String index, String shapeField) throws IOException { - GetResponse response = client.prepareGet(index, type, id).setPreference("_local").execute().actionGet(); + GetResponse response = client.get(new GetRequest(index, type, id).preference("_local")).actionGet(); if (!response.exists()) { throw new ElasticSearchIllegalArgumentException("Shape with ID [" + id + "] in type [" + type + "] not found"); } XContentParser parser = null; try { - parser = JsonXContent.jsonXContent.createParser(response.source()); + parser = XContentHelper.createParser(response.sourceRef()); XContentParser.Token currentToken; while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (currentToken == XContentParser.Token.FIELD_NAME) { diff --git a/src/test/java/org/elasticsearch/test/integration/search/geo/GeoShapeIntegrationTests.java b/src/test/java/org/elasticsearch/test/integration/search/geo/GeoShapeIntegrationTests.java index 4697cb92670..273f9857564 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/geo/GeoShapeIntegrationTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/geo/GeoShapeIntegrationTests.java @@ -1,3 +1,22 @@ +/* + * 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.test.integration.search.geo; import com.spatial4j.core.shape.Shape; @@ -15,9 +34,7 @@ import org.testng.annotations.Test; import static org.elasticsearch.common.geo.ShapeBuilder.newRectangle; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.geoShapeFilter; -import static org.elasticsearch.index.query.QueryBuilders.filteredQuery; -import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; +import static org.elasticsearch.index.query.QueryBuilders.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -48,8 +65,8 @@ public class GeoShapeIntegrationTests extends AbstractNodesTests { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("properties").startObject("location") - .field("type", "geo_shape") - .field("tree", "quadtree") + .field("type", "geo_shape") + .field("tree", "quadtree") .endObject().endObject() .endObject().endObject().string(); client.admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet(); @@ -66,8 +83,8 @@ public class GeoShapeIntegrationTests extends AbstractNodesTests { client.prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject() .field("name", "Document 2") .startObject("location") - .field("type", "point") - .startArray("coordinates").value(-45).value(-50).endArray() + .field("type", "point") + .startArray("coordinates").value(-45).value(-50).endArray() .endObject() .endObject()).execute().actionGet();