|
20 | 20 | package org.elasticsearch.search.geo; |
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.get.GetResponse; |
| 23 | +import org.elasticsearch.action.index.IndexRequest; |
23 | 24 | import org.elasticsearch.action.search.SearchResponse; |
| 25 | +import org.elasticsearch.common.CheckedSupplier; |
24 | 26 | import org.elasticsearch.common.Strings; |
25 | 27 | import org.elasticsearch.common.geo.ShapeRelation; |
26 | 28 | import org.elasticsearch.common.geo.builders.CoordinatesBuilder; |
|
32 | 34 | import org.elasticsearch.common.settings.Settings; |
33 | 35 | import org.elasticsearch.common.xcontent.XContentBuilder; |
34 | 36 | import org.elasticsearch.common.xcontent.XContentFactory; |
| 37 | +import org.elasticsearch.common.xcontent.XContentParser; |
35 | 38 | import org.elasticsearch.common.xcontent.XContentType; |
36 | 39 | import org.elasticsearch.index.mapper.MapperParsingException; |
37 | 40 | import org.elasticsearch.index.query.GeoShapeQueryBuilder; |
@@ -531,4 +534,73 @@ public void testFieldAlias() throws IOException { |
531 | 534 | .execute().actionGet(); |
532 | 535 | assertEquals(1, response.getHits().getTotalHits()); |
533 | 536 | } |
| 537 | + |
| 538 | + // Test for issue #34418 |
| 539 | + public void testEnvelopeSpanningDateline() throws IOException { |
| 540 | + XContentBuilder mapping = XContentFactory.jsonBuilder().startObject() |
| 541 | + .startObject("doc") |
| 542 | + .startObject("properties") |
| 543 | + .startObject("geo").field("type", "geo_shape").endObject() |
| 544 | + .endObject() |
| 545 | + .endObject() |
| 546 | + .endObject(); |
| 547 | + |
| 548 | + createIndex("test", Settings.builder().put("index.number_of_shards", 1).build(), "doc", mapping); |
| 549 | + |
| 550 | + String doc1 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-33.918711,\r\n" + "18.847685\r\n" + "],\r\n" + |
| 551 | + "\"type\": \"Point\"\r\n" + "}}"; |
| 552 | + client().index(new IndexRequest("test", "doc", "1").source(doc1, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); |
| 553 | + |
| 554 | + String doc2 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-49.0,\r\n" + "18.847685\r\n" + "],\r\n" + |
| 555 | + "\"type\": \"Point\"\r\n" + "}}"; |
| 556 | + client().index(new IndexRequest("test", "doc", "2").source(doc2, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); |
| 557 | + |
| 558 | + String doc3 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "49.0,\r\n" + "18.847685\r\n" + "],\r\n" + |
| 559 | + "\"type\": \"Point\"\r\n" + "}}"; |
| 560 | + client().index(new IndexRequest("test", "doc", "3").source(doc3, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); |
| 561 | + |
| 562 | + @SuppressWarnings("unchecked") CheckedSupplier<GeoShapeQueryBuilder, IOException> querySupplier = randomFrom( |
| 563 | + () -> QueryBuilders.geoShapeQuery( |
| 564 | + "geo", |
| 565 | + new EnvelopeBuilder(new Coordinate(-21, 44), new Coordinate(-39, 9)) |
| 566 | + ).relation(ShapeRelation.WITHIN), |
| 567 | + () -> { |
| 568 | + XContentBuilder builder = XContentFactory.jsonBuilder().startObject() |
| 569 | + .startObject("geo") |
| 570 | + .startObject("shape") |
| 571 | + .field("type", "envelope") |
| 572 | + .startArray("coordinates") |
| 573 | + .startArray().value(-21).value(44).endArray() |
| 574 | + .startArray().value(-39).value(9).endArray() |
| 575 | + .endArray() |
| 576 | + .endObject() |
| 577 | + .field("relation", "within") |
| 578 | + .endObject() |
| 579 | + .endObject(); |
| 580 | + try (XContentParser parser = createParser(builder)){ |
| 581 | + parser.nextToken(); |
| 582 | + return GeoShapeQueryBuilder.fromXContent(parser); |
| 583 | + } |
| 584 | + }, |
| 585 | + () -> { |
| 586 | + XContentBuilder builder = XContentFactory.jsonBuilder().startObject() |
| 587 | + .startObject("geo") |
| 588 | + .field("shape", "BBOX (-21, -39, 44, 9)") |
| 589 | + .field("relation", "within") |
| 590 | + .endObject() |
| 591 | + .endObject(); |
| 592 | + try (XContentParser parser = createParser(builder)){ |
| 593 | + parser.nextToken(); |
| 594 | + return GeoShapeQueryBuilder.fromXContent(parser); |
| 595 | + } |
| 596 | + } |
| 597 | + ); |
| 598 | + |
| 599 | + SearchResponse response = client().prepareSearch("test") |
| 600 | + .setQuery(querySupplier.get()) |
| 601 | + .execute().actionGet(); |
| 602 | + assertEquals(2, response.getHits().getTotalHits()); |
| 603 | + assertNotEquals("1", response.getHits().getAt(0).getId()); |
| 604 | + assertNotEquals("1", response.getHits().getAt(1).getId()); |
| 605 | + } |
534 | 606 | } |
0 commit comments