Search in sources :

Example 1 with LatLonDocValuesField

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));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoPoint(org.elasticsearch.common.geo.GeoPoint) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 2 with LatLonDocValuesField

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()));
}
Also used : LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) LatLonPoint(org.apache.lucene.document.LatLonPoint) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with LatLonDocValuesField

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());
        }
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) HashSet(java.util.HashSet)

Aggregations

LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Document (org.apache.lucene.document.Document)1 LatLonPoint (org.apache.lucene.document.LatLonPoint)1 IndexReader (org.apache.lucene.index.IndexReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Directory (org.apache.lucene.store.Directory)1 BytesRef (org.apache.lucene.util.BytesRef)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1