use of org.apache.lucene.document.LatLonDocValuesField in project elasticsearch by elastic.
the class GeoBoundsAggregatorTests method testRandom.
public void testRandom() throws Exception {
double top = Double.NEGATIVE_INFINITY;
double bottom = Double.POSITIVE_INFINITY;
double posLeft = Double.POSITIVE_INFINITY;
double posRight = Double.NEGATIVE_INFINITY;
double negLeft = Double.POSITIVE_INFINITY;
double negRight = Double.NEGATIVE_INFINITY;
int numDocs = randomIntBetween(50, 100);
try (Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
int numValues = randomIntBetween(1, 5);
for (int j = 0; j < numValues; j++) {
GeoPoint point = RandomGeoGenerator.randomPoint(random());
if (point.getLat() > top) {
top = point.getLat();
}
if (point.getLat() < bottom) {
bottom = point.getLat();
}
if (point.getLon() >= 0 && point.getLon() < posLeft) {
posLeft = point.getLon();
}
if (point.getLon() >= 0 && point.getLon() > posRight) {
posRight = point.getLon();
}
if (point.getLon() < 0 && point.getLon() < negLeft) {
negLeft = point.getLon();
}
if (point.getLon() < 0 && point.getLon() > negRight) {
negRight = point.getLon();
}
doc.add(new LatLonDocValuesField("field", point.getLat(), point.getLon()));
}
w.addDocument(doc);
}
GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false);
MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType();
fieldType.setHasDocValues(true);
fieldType.setName("field");
try (IndexReader reader = w.getReader()) {
IndexSearcher searcher = new IndexSearcher(reader);
InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
assertThat(bounds.top, closeTo(top, GEOHASH_TOLERANCE));
assertThat(bounds.bottom, closeTo(bottom, GEOHASH_TOLERANCE));
assertThat(bounds.posLeft, closeTo(posLeft, GEOHASH_TOLERANCE));
assertThat(bounds.posRight, closeTo(posRight, GEOHASH_TOLERANCE));
assertThat(bounds.negRight, closeTo(negRight, GEOHASH_TOLERANCE));
assertThat(bounds.negLeft, closeTo(negLeft, GEOHASH_TOLERANCE));
}
}
}
use of org.apache.lucene.document.LatLonDocValuesField in project elasticsearch by elastic.
the class GeoPoint method resetFromIndexableField.
// todo this is a crutch because LatLonPoint doesn't have a helper for returning .stringValue()
// todo remove with next release of lucene
public GeoPoint resetFromIndexableField(IndexableField field) {
if (field instanceof LatLonPoint) {
BytesRef br = field.binaryValue();
byte[] bytes = Arrays.copyOfRange(br.bytes, br.offset, br.length);
return this.reset(GeoEncodingUtils.decodeLatitude(bytes, 0), GeoEncodingUtils.decodeLongitude(bytes, Integer.BYTES));
} else if (field instanceof LatLonDocValuesField) {
long encoded = (long) (field.numericValue());
return this.reset(GeoEncodingUtils.decodeLatitude((int) (encoded >>> 32)), GeoEncodingUtils.decodeLongitude((int) encoded));
}
return resetFromIndexHash(Long.parseLong(field.stringValue()));
}
use of org.apache.lucene.document.LatLonDocValuesField in project elasticsearch by elastic.
the class GeoHashGridAggregatorTests method testWithSeveralDocs.
public void testWithSeveralDocs() throws IOException {
int precision = randomIntBetween(1, 12);
int numPoints = randomIntBetween(8, 128);
Map<String, Integer> expectedCountPerGeoHash = new HashMap<>();
testCase(new MatchAllDocsQuery(), FIELD_NAME, precision, iw -> {
List<LatLonDocValuesField> points = new ArrayList<>();
Set<String> distinctHashesPerDoc = new HashSet<>();
for (int pointId = 0; pointId < numPoints; pointId++) {
double lat = (180d * randomDouble()) - 90d;
double lng = (360d * randomDouble()) - 180d;
points.add(new LatLonDocValuesField(FIELD_NAME, lat, lng));
String hash = stringEncode(lng, lat, precision);
if (distinctHashesPerDoc.contains(hash) == false) {
expectedCountPerGeoHash.put(hash, expectedCountPerGeoHash.getOrDefault(hash, 0) + 1);
}
distinctHashesPerDoc.add(hash);
if (usually()) {
iw.addDocument(points);
points.clear();
distinctHashesPerDoc.clear();
}
}
if (points.size() != 0) {
iw.addDocument(points);
}
}, geoHashGrid -> {
assertEquals(expectedCountPerGeoHash.size(), geoHashGrid.getBuckets().size());
for (GeoHashGrid.Bucket bucket : geoHashGrid.getBuckets()) {
assertEquals((long) expectedCountPerGeoHash.get(bucket.getKeyAsString()), bucket.getDocCount());
}
});
}
Aggregations