Skip to content

Commit

Permalink
Move geo_shape percolator test to the spatial module (#88554)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase authored Jul 18, 2022
1 parent 04bdefd commit 051d0e9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.geometry.LinearRing;
import org.elasticsearch.geometry.Polygon;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
Expand All @@ -26,7 +27,6 @@
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.elasticsearch.xcontent.XContentType;
Expand All @@ -40,7 +40,7 @@
import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoBoundingBoxQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoDistanceQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoPolygonQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
Expand All @@ -64,7 +64,7 @@ public class PercolatorQuerySearchIT extends ESIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Arrays.asList(PercolatorPlugin.class, TestGeoShapeFieldMapperPlugin.class);
return Arrays.asList(PercolatorPlugin.class);
}

public void testPercolatorQuery() throws Exception {
Expand Down Expand Up @@ -285,7 +285,7 @@ public void testPercolatorGeoQueries() throws Exception {
client().admin()
.indices()
.prepareCreate("test")
.setMapping("id", "type=keyword", "field1", "type=geo_point", "field2", "type=geo_shape", "query", "type=percolator")
.setMapping("id", "type=keyword", "field1", "type=geo_point", "query", "type=percolator")
);

client().prepareIndex("test")
Expand Down Expand Up @@ -314,7 +314,10 @@ public void testPercolatorGeoQueries() throws Exception {
jsonBuilder().startObject()
.field(
"query",
geoPolygonQuery("field1", Arrays.asList(new GeoPoint(52.1, 4.4), new GeoPoint(52.3, 4.5), new GeoPoint(52.1, 4.6)))
geoShapeQuery(
"field1",
new Polygon(new LinearRing(new double[] { 4.4, 4.5, 4.6, 4.4 }, new double[] { 52.1, 52.3, 52.1, 52.1 }))
)
)
.field("id", "3")
.endObject()
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/spatial/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
api "org.apache.lucene:lucene-spatial3d:${versions.lucene}"
api project(":libs:elasticsearch-h3")
testImplementation(testArtifact(project(xpackModule('core'))))
testImplementation project(path: ':modules:percolator')
testImplementation project(path: xpackModule('vector-tile'))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@
package org.elasticsearch.xpack.spatial.search;

import org.elasticsearch.Version;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.geometry.LinearRing;
import org.elasticsearch.geometry.Polygon;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.percolator.PercolateQueryBuilder;
import org.elasticsearch.percolator.PercolatorPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.geo.GeoShapeIntegTestCase;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.geoBoundingBoxQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoDistanceQuery;
import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class GeoShapeWithDocValuesIT extends GeoShapeIntegTestCase {

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Collections.singleton(LocalStateSpatialPlugin.class);
return List.of(LocalStateSpatialPlugin.class, PercolatorPlugin.class);
}

@Override
Expand Down Expand Up @@ -88,4 +102,60 @@ public void testMappingUpdate() {
);
}
}

public void testPercolatorGeoQueries() throws Exception {
assertAcked(
client().admin()
.indices()
.prepareCreate("test")
.setMapping("id", "type=keyword", "field1", "type=geo_shape", "query", "type=percolator")
);

client().prepareIndex("test")
.setId("1")
.setSource(
jsonBuilder().startObject()
.field("query", geoDistanceQuery("field1").point(52.18, 4.38).distance(50, DistanceUnit.KILOMETERS))
.field("id", "1")
.endObject()
)
.get();

client().prepareIndex("test")
.setId("2")
.setSource(
jsonBuilder().startObject()
.field("query", geoBoundingBoxQuery("field1").setCorners(52.3, 4.4, 52.1, 4.6))
.field("id", "2")
.endObject()
)
.get();

client().prepareIndex("test")
.setId("3")
.setSource(
jsonBuilder().startObject()
.field(
"query",
geoShapeQuery(
"field1",
new Polygon(new LinearRing(new double[] { 4.4, 4.5, 4.6, 4.4 }, new double[] { 52.1, 52.3, 52.1, 52.1 }))
)
)
.field("id", "3")
.endObject()
)
.get();
refresh();

BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "POINT(4.51 52.20)").endObject());
SearchResponse response = client().prepareSearch()
.setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON))
.addSort("id", SortOrder.ASC)
.get();
assertHitCount(response, 3);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
assertThat(response.getHits().getAt(2).getId(), equalTo("3"));
}
}

0 comments on commit 051d0e9

Please sign in to comment.