minor geo shape fetch improvements

This commit is contained in:
Shay Banon 2012-09-23 21:31:13 +02:00
parent 05e0b4d4e0
commit 6e66f45f58
6 changed files with 122 additions and 26 deletions

View File

@ -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) {

View File

@ -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.
*
* <p/>
* Only those fields mapped using {@link GeoShapeFieldMapper} can be filtered
* using this parser.
*
* <p/>
* Format supported:
*
* <p/>
* <pre>
* "field" : {
* "relation" : "intersects",
* "shape" : {
@ -34,6 +54,7 @@ import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFi
* ]
* }
* }
* </pre>
*/
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();

View File

@ -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) {

View File

@ -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();

View File

@ -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) {

View File

@ -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();