GeoDistanceSortField extends SortField { @Override public org.apache.lucene.search.SortField sortField(Schema schema) { final Mapper mapper = schema.mapper(field); if (mapper == null) { throw new IndexException("Field '{}' is not found", field); } else if (!(mapper instanceof GeoPointMapper)) { throw new IndexException("Field '{}' type is not geo_point", field); } GeoPointMapper geoPointMapper = (GeoPointMapper) mapper; Point point = CONTEXT.makePoint(longitude, latitude); SpatialStrategy strategy = geoPointMapper.strategy.getGeometryStrategy(); ValueSource valueSource = strategy.makeDistanceValueSource(point, DistanceUtils.DEG_TO_KM); return valueSource.getSortField(reverse); } GeoDistanceSortField(String field, Boolean reverse, double latitude, double longitude); @Override org.apache.lucene.search.SortField sortField(Schema schema); Set<String> postProcessingFields(); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); final String field; final double latitude; final double longitude; }
@Test public void testGeoDistanceSortFieldDefaults() { Schema schema = schema().mapper("field", geoPointMapper("latitude", "longitude")).build(); GeoDistanceSortField sortField = new GeoDistanceSortField("field", null, 0.0, 0.0); org.apache.lucene.search.SortField luceneSortField = sortField.sortField(schema); assertNotNull("SortField is not created", luceneSortField); assertEquals("SortField reverse is wrong", SortField.DEFAULT_REVERSE, luceneSortField.getReverse()); assertEquals("SortField type is wrong", luceneSortField.getType(), org.apache.lucene.search.SortField.Type.REWRITEABLE); }
@Test public void testGeoDistanceSortFieldReverse() { Schema schema = schema().mapper("field", geoPointMapper("latitude", "longitude")).build(); GeoDistanceSortField sortField = new GeoDistanceSortField("field", true, 0.0, 0.0); org.apache.lucene.search.SortField luceneSortField = sortField.sortField(schema); assertNotNull("SortField is not created", luceneSortField); assertTrue("sortField reverse is wrong", luceneSortField.getReverse()); }
@Test(expected = IndexException.class) public void testGeoDistanceSortFieldWithoutMapper() { Schema schema = schema().build(); GeoDistanceSortField sortField = new GeoDistanceSortField("field", false, 0.0, 0.0); sortField.sortField(schema); }