25
25
import org .apache .lucene .index .IndexableField ;
26
26
import org .apache .lucene .index .Terms ;
27
27
import org .apache .lucene .search .Query ;
28
+ import org .apache .lucene .spatial .util .MortonEncoder ;
29
+ import org .apache .lucene .util .BytesRef ;
28
30
import org .apache .lucene .util .LegacyNumericUtils ;
29
- import org .apache .lucene .util .NumericUtils ;
30
31
import org .elasticsearch .ElasticsearchParseException ;
31
32
import org .elasticsearch .Version ;
32
33
import org .elasticsearch .action .fieldstats .FieldStats ;
@@ -306,6 +307,7 @@ public static class LegacyGeoPointFieldType extends GeoPointFieldType {
306
307
307
308
protected MappedFieldType latFieldType ;
308
309
protected MappedFieldType lonFieldType ;
310
+ protected boolean numericEncoded ;
309
311
310
312
LegacyGeoPointFieldType () {}
311
313
@@ -316,6 +318,7 @@ public static class LegacyGeoPointFieldType extends GeoPointFieldType {
316
318
this .geoHashPrefixEnabled = ref .geoHashPrefixEnabled ;
317
319
this .latFieldType = ref .latFieldType ; // copying ref is ok, this can never be modified
318
320
this .lonFieldType = ref .lonFieldType ; // copying ref is ok, this can never be modified
321
+ this .numericEncoded = ref .numericEncoded ;
319
322
}
320
323
321
324
@ Override
@@ -329,15 +332,16 @@ public boolean equals(Object o) {
329
332
LegacyGeoPointFieldType that = (LegacyGeoPointFieldType ) o ;
330
333
return geoHashPrecision == that .geoHashPrecision &&
331
334
geoHashPrefixEnabled == that .geoHashPrefixEnabled &&
335
+ numericEncoded == that .numericEncoded &&
332
336
java .util .Objects .equals (geoHashFieldType , that .geoHashFieldType ) &&
333
337
java .util .Objects .equals (latFieldType , that .latFieldType ) &&
334
338
java .util .Objects .equals (lonFieldType , that .lonFieldType );
335
339
}
336
340
337
341
@ Override
338
342
public int hashCode () {
339
- return java .util .Objects .hash (super .hashCode (), geoHashFieldType , geoHashPrecision , geoHashPrefixEnabled , latFieldType ,
340
- lonFieldType );
343
+ return java .util .Objects .hash (super .hashCode (), geoHashFieldType , geoHashPrecision , geoHashPrefixEnabled ,
344
+ numericEncoded , latFieldType , lonFieldType );
341
345
}
342
346
343
347
@ Override
@@ -437,10 +441,9 @@ public FieldStats.GeoPoint stats(IndexReader reader) throws IOException {
437
441
if (terms == null ) {
438
442
return new FieldStats .GeoPoint (reader .maxDoc (), 0L , -1L , -1L , isSearchable (), isAggregatable ());
439
443
}
440
- GeoPoint minPt = GeoPoint .fromGeohash (NumericUtils .sortableBytesToLong (terms .getMin ().bytes , terms .getMin ().offset ));
441
- GeoPoint maxPt = GeoPoint .fromGeohash (NumericUtils .sortableBytesToLong (terms .getMax ().bytes , terms .getMax ().offset ));
442
444
return new FieldStats .GeoPoint (reader .maxDoc (), terms .getDocCount (), -1L , terms .getSumTotalTermFreq (), isSearchable (),
443
- isAggregatable (), minPt , maxPt );
445
+ isAggregatable (), prefixCodedToGeoPoint (terms .getMin (), numericEncoded ),
446
+ prefixCodedToGeoPoint (terms .getMax (), numericEncoded ));
444
447
}
445
448
}
446
449
@@ -657,4 +660,19 @@ public FieldMapper updateFieldType(Map<String, MappedFieldType> fullNameToFieldT
657
660
updated .lonMapper = lonUpdated ;
658
661
return updated ;
659
662
}
663
+
664
+ private static GeoPoint prefixCodedToGeoPoint (BytesRef val , boolean isGeoCoded ) {
665
+ final long encoded = isGeoCoded ? prefixCodedToGeoCoded (val ) : LegacyNumericUtils .prefixCodedToLong (val );
666
+ return new GeoPoint (MortonEncoder .decodeLatitude (encoded ), MortonEncoder .decodeLongitude (encoded ));
667
+ }
668
+
669
+ private static long prefixCodedToGeoCoded (BytesRef val ) {
670
+ long result = fromBytes ((byte )0 , (byte )0 , (byte )0 , (byte )0 , val .bytes [val .offset + 0 ], val .bytes [val .offset + 1 ],
671
+ val .bytes [val .offset + 2 ], val .bytes [val .offset + 3 ]);
672
+ return result << 32 ;
673
+ }
674
+
675
+ private static long fromBytes (byte b1 , byte b2 , byte b3 , byte b4 , byte b5 , byte b6 , byte b7 , byte b8 ) {
676
+ return ((long )b1 & 255L ) << 56 | ((long )b2 & 255L ) << 48 | ((long )b3 & 255L ) << 40 | ((long )b4 & 255L ) << 32 | ((long )b5 & 255L ) << 24 | ((long )b6 & 255L ) << 16 | ((long )b7 & 255L ) << 8 | (long )b8 & 255L ;
677
+ }
660
678
}
0 commit comments