Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias field does not work with geo_shape query #74895

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good one :)

}

@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 @@ -16,19 +16,18 @@
import org.elasticsearch.action.get.GetResponse;
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.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
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.test.AbstractQueryTestCase;
import org.junit.After;
import org.locationtech.jts.geom.Coordinate;

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

// 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,57 @@

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.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 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(indexName).setMapping(xcb).setSettings(settings).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).setMapping(mapping).get();
ensureGreen();

Point point = GeometryTestUtils.randomPoint(false);
client().prepareIndex(defaultIndexName).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