|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.index.query; |
| 10 | + |
| 11 | +import org.apache.lucene.document.LatLonDocValuesField; |
| 12 | +import org.apache.lucene.document.LatLonPoint; |
| 13 | +import org.apache.lucene.geo.GeoEncodingUtils; |
| 14 | +import org.apache.lucene.search.IndexOrDocValuesQuery; |
| 15 | +import org.apache.lucene.search.MatchNoDocsQuery; |
| 16 | +import org.apache.lucene.search.Query; |
| 17 | +import org.elasticsearch.index.mapper.GeoPointFieldMapper; |
| 18 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 19 | +import org.elasticsearch.search.geo.GeoBoundingBoxQueryBuilderTestCase; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +@SuppressWarnings("checkstyle:MissingJavadocMethod") |
| 24 | +public class GeoBoundingBoxQueryBuilderGeoPointTests extends GeoBoundingBoxQueryBuilderTestCase { |
| 25 | + |
| 26 | + @Override |
| 27 | + protected String getFieldName() { |
| 28 | + return randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void doAssertLuceneQuery(GeoBoundingBoxQueryBuilder queryBuilder, Query query, SearchExecutionContext context) |
| 33 | + throws IOException { |
| 34 | + final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); |
| 35 | + if (fieldType == null) { |
| 36 | + assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); |
| 37 | + } |
| 38 | + assertEquals(GeoPointFieldMapper.GeoPointFieldType.class, fieldType.getClass()); |
| 39 | + assertEquals(IndexOrDocValuesQuery.class, query.getClass()); |
| 40 | + Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery(); |
| 41 | + String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); |
| 42 | + double qMinLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.bottomRight().lat())); |
| 43 | + double qMaxLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.topLeft().lat())); |
| 44 | + double qMinLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.topLeft().lon())); |
| 45 | + double qMaxLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.bottomRight().lon())); |
| 46 | + assertEquals(LatLonPoint.newBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), indexQuery); |
| 47 | + Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery(); |
| 48 | + assertEquals(LatLonDocValuesField.newSlowBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), dvQuery); |
| 49 | + } |
| 50 | + |
| 51 | + public void testValidation() { |
| 52 | + PointTester[] testers = { new TopTester(), new LeftTester(), new BottomTester(), new RightTester() }; |
| 53 | + |
| 54 | + for (PointTester tester : testers) { |
| 55 | + GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); |
| 56 | + tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.COERCE), false); |
| 57 | + QueryValidationException except = builder.checkLatLon(); |
| 58 | + assertNull( |
| 59 | + "validation w/ coerce should ignore invalid " |
| 60 | + + tester.getClass().getName() |
| 61 | + + " coordinate: " |
| 62 | + + tester.invalidCoordinate |
| 63 | + + " ", |
| 64 | + except |
| 65 | + ); |
| 66 | + |
| 67 | + tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.STRICT), false); |
| 68 | + except = builder.checkLatLon(); |
| 69 | + assertNotNull( |
| 70 | + "validation w/o coerce should detect invalid coordinate: " |
| 71 | + + tester.getClass().getName() |
| 72 | + + " coordinate: " |
| 73 | + + tester.invalidCoordinate, |
| 74 | + except |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments