Skip to content

Commit

Permalink
Alias field does not work with geo_shape query (#74895) (#75100)
Browse files Browse the repository at this point in the history
Fixes the resolution of the field name in the geo_shape query.
  • Loading branch information
iverase authored Jul 8, 2021
1 parent a049585 commit 0dc8d02
Show file tree
Hide file tree
Showing 10 changed files with 958 additions and 791 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public Query buildShapeQuery(SearchExecutionContext context, MappedFieldType fie
"Field [" + fieldName + "] is of unsupported type [" + fieldType.typeName() + "] for [" + NAME + "] query");
}
final GeoShapeQueryable ft = (GeoShapeQueryable) fieldType;
return new ConstantScoreQuery(ft.geoShapeQuery(shape, fieldName, strategy, relation, context));
return new ConstantScoreQuery(ft.geoShapeQuery(shape, fieldType.name(), strategy, relation, context));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static Shape buildS4J(Geometry geometry) {

@Override
public ShapeBuilder<?, ?, ?> visit(LinearRing ring) {
throw new UnsupportedOperationException("circle is not supported");
throw new UnsupportedOperationException("LinearRing is not supported");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.geo.builders.EnvelopeBuilder;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -27,13 +26,13 @@
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.Geometry;
import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.geometry.utils.WellKnownText;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.VersionUtils;
import org.junit.After;
import org.locationtech.jts.geom.Coordinate;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -145,10 +144,8 @@ public void testNoRelation() throws IOException {

// see #3878
public void testThatXContentSerializationInsideOfArrayWorks() throws Exception {
EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(0, 10), new Coordinate(10, 0));
GeoShapeQueryBuilder geoQuery = randomBoolean() ?
QueryBuilders.geoShapeQuery("searchGeometry", envelopeBuilder) :
QueryBuilders.geoShapeQuery("searchGeometry", envelopeBuilder.buildGeometry());
Rectangle rectangle = new Rectangle(0, 10, 10, 0);
GeoShapeQueryBuilder geoQuery = QueryBuilders.geoShapeQuery("searchGeometry", rectangle);
JsonXContent.contentBuilder().startArray().value(geoQuery).endArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,58 @@

package org.elasticsearch.search.geo;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geometry.Point;
import org.elasticsearch.geometry.utils.WellKnownText;

public class GeoPointShapeQueryTests extends GeoQueryTests {
import java.io.IOException;

import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery;

public class GeoPointShapeQueryTests extends GeoPointShapeQueryTestCase {

@Override
protected XContentBuilder createDefaultMapping() throws Exception {
protected void createMapping(String indexName, String type, String fieldName, Settings settings) throws Exception {
XContentBuilder xcb = XContentFactory.jsonBuilder().startObject()
.startObject("properties").startObject(defaultGeoFieldName)
.startObject("properties").startObject(fieldName)
.field("type", "geo_point")
.endObject().endObject().endObject();
client().admin().indices().prepareCreate(defaultIndexName).addMapping(type, xcb).get();
}

public void testFieldAlias() throws IOException {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject(defaultGeoFieldName)
.field("type", "geo_point")
.endObject()
.startObject("alias")
.field("type", "alias")
.field("path", defaultGeoFieldName)
.endObject()
.endObject()
.endObject());

client().admin().indices().prepareCreate(defaultIndexName).addMapping(defaultType, mapping, XContentType.JSON).get();
ensureGreen();

Point point = GeometryTestUtils.randomPoint(false);
client().prepareIndex(defaultIndexName, defaultType).setId("1")
.setSource(jsonBuilder().startObject().field(defaultGeoFieldName, WellKnownText.toWKT(point)).endObject())
.setRefreshPolicy(IMMEDIATE).get();

return xcb;
SearchResponse response = client().prepareSearch(defaultIndexName)
.setQuery(geoShapeQuery("alias", point))
.get();
assertEquals(1, response.getHits().getTotalHits().value);
}
}
Loading

0 comments on commit 0dc8d02

Please sign in to comment.