-
Notifications
You must be signed in to change notification settings - Fork 25.2k
geo_point runtime field implementation #63164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fd9eff3
geo_point runtime implementation
iverase 47e4eb3
Add geo_distance sort yaml test
iverase 767d398
iter
iverase 2a6509a
Shorten field type class name
iverase 5a88e14
cleanup tests
iverase 9117fd9
iter
iverase d94874c
iter
iverase f53b662
Move creation of LatLonGeometries to GeoShapeUtils
iverase bb6cab1
Merge branch 'master' into runtime_geopoint
iverase e7d84ca
compile errors
iverase 2e9ec53
precommit
iverase 8af770c
Merge branch 'master' into runtime_geopoint
iverase a77aaa2
remove copy/paste nosense
iverase c983c00
Merge branch 'master' into runtime_geopoint
iverase 503d332
Merge branch 'master' into runtime_geopoint
iverase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rc/main/java/org/elasticsearch/xpack/runtimefields/fielddata/GeoPointScriptDocValues.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.fielddata; | ||
|
||
import org.apache.lucene.geo.GeoEncodingUtils; | ||
import org.elasticsearch.common.geo.GeoPoint; | ||
import org.elasticsearch.index.fielddata.MultiGeoPointValues; | ||
import org.elasticsearch.xpack.runtimefields.mapper.GeoPointFieldScript; | ||
|
||
import java.util.Arrays; | ||
|
||
public final class GeoPointScriptDocValues extends MultiGeoPointValues { | ||
private final GeoPointFieldScript script; | ||
private final GeoPoint point; | ||
private int cursor; | ||
|
||
GeoPointScriptDocValues(GeoPointFieldScript script) { | ||
this.script = script; | ||
this.point = new GeoPoint(); | ||
} | ||
|
||
@Override | ||
public boolean advanceExact(int docId) { | ||
script.runForDoc(docId); | ||
if (script.count() == 0) { | ||
return false; | ||
} | ||
Arrays.sort(script.values(), 0, script.count()); | ||
cursor = 0; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int docValueCount() { | ||
return script.count(); | ||
} | ||
|
||
@Override | ||
public GeoPoint nextValue() { | ||
final long value = script.values()[cursor++]; | ||
final int lat = (int) (value >>> 32); | ||
final int lon = (int) (value & 0xFFFFFFFF); | ||
return point.reset(GeoEncodingUtils.decodeLatitude(lat), GeoEncodingUtils.decodeLongitude(lon)); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...rc/main/java/org/elasticsearch/xpack/runtimefields/fielddata/GeoPointScriptFieldData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.fielddata; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.SortField; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.index.fielddata.IndexFieldData; | ||
import org.elasticsearch.index.fielddata.IndexFieldDataCache; | ||
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; | ||
import org.elasticsearch.index.fielddata.LeafGeoPointFieldData; | ||
import org.elasticsearch.index.fielddata.MultiGeoPointValues; | ||
import org.elasticsearch.index.fielddata.plain.AbstractLeafGeoPointFieldData; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.indices.breaker.CircuitBreakerService; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.MultiValueMode; | ||
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; | ||
import org.elasticsearch.search.aggregations.support.ValuesSourceType; | ||
import org.elasticsearch.search.sort.BucketedSort; | ||
import org.elasticsearch.search.sort.SortOrder; | ||
import org.elasticsearch.xpack.runtimefields.mapper.GeoPointFieldScript; | ||
|
||
public class GeoPointScriptFieldData implements IndexGeoPointFieldData { | ||
public static class Builder implements IndexFieldData.Builder { | ||
private final String name; | ||
private final GeoPointFieldScript.LeafFactory leafFactory; | ||
|
||
public Builder(String name, GeoPointFieldScript.LeafFactory leafFactory) { | ||
this.name = name; | ||
this.leafFactory = leafFactory; | ||
} | ||
|
||
@Override | ||
public GeoPointScriptFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService, MapperService mapperService) { | ||
return new GeoPointScriptFieldData(name, leafFactory); | ||
} | ||
} | ||
|
||
private final GeoPointFieldScript.LeafFactory leafFactory; | ||
private final String name; | ||
|
||
private GeoPointScriptFieldData(String fieldName, GeoPointFieldScript.LeafFactory leafFactory) { | ||
this.name = fieldName; | ||
this.leafFactory = leafFactory; | ||
} | ||
|
||
@Override | ||
public SortField sortField(Object missingValue, MultiValueMode sortMode, XFieldComparatorSource.Nested nested, boolean reverse) { | ||
throw new IllegalArgumentException("can't sort on geo_point field without using specific sorting feature, like geo_distance"); | ||
} | ||
|
||
@Override | ||
public BucketedSort newBucketedSort( | ||
BigArrays bigArrays, | ||
Object missingValue, | ||
MultiValueMode sortMode, | ||
XFieldComparatorSource.Nested nested, | ||
SortOrder sortOrder, | ||
DocValueFormat format, | ||
int bucketSize, | ||
BucketedSort.ExtraData extra | ||
) { | ||
throw new IllegalArgumentException("can't sort on geo_point field without using specific sorting feature, like geo_distance"); | ||
} | ||
|
||
@Override | ||
public String getFieldName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public ValuesSourceType getValuesSourceType() { | ||
return CoreValuesSourceType.GEOPOINT; | ||
} | ||
|
||
@Override | ||
public LeafGeoPointFieldData load(LeafReaderContext context) { | ||
GeoPointFieldScript script = leafFactory.newInstance(context); | ||
return new AbstractLeafGeoPointFieldData() { | ||
@Override | ||
public MultiGeoPointValues getGeoPointValues() { | ||
return new GeoPointScriptDocValues(script); | ||
} | ||
|
||
@Override | ||
public long ramBytesUsed() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public LeafGeoPointFieldData loadDirect(LeafReaderContext context) { | ||
return load(context); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ields/src/main/java/org/elasticsearch/xpack/runtimefields/mapper/GeoPointFieldScript.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.mapper; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.painless.spi.WhitelistLoader; | ||
import org.elasticsearch.script.ScriptContext; | ||
import org.elasticsearch.script.ScriptFactory; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
import org.apache.lucene.document.LatLonDocValuesField; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.apache.lucene.geo.GeoEncodingUtils.encodeLatitude; | ||
import static org.apache.lucene.geo.GeoEncodingUtils.encodeLongitude; | ||
|
||
/** | ||
* Script producing geo points. Similarly to what {@link LatLonDocValuesField} does, | ||
* it encodes the points as a long value. | ||
*/ | ||
public abstract class GeoPointFieldScript extends AbstractLongFieldScript { | ||
public static final ScriptContext<Factory> CONTEXT = newContext("geo_point_script_field", Factory.class); | ||
|
||
static List<Whitelist> whitelist() { | ||
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "geo_point_whitelist.txt")); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static final String[] PARAMETERS = {}; | ||
|
||
public interface Factory extends ScriptFactory { | ||
LeafFactory newFactory(String fieldName, Map<String, Object> params, SearchLookup searchLookup); | ||
} | ||
|
||
public interface LeafFactory { | ||
GeoPointFieldScript newInstance(LeafReaderContext ctx); | ||
} | ||
|
||
public GeoPointFieldScript(String fieldName, Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) { | ||
super(fieldName, params, searchLookup, ctx); | ||
} | ||
|
||
protected void emit(double lat, double lon) { | ||
int latitudeEncoded = encodeLatitude(lat); | ||
int longitudeEncoded = encodeLongitude(lon); | ||
emit(Long.valueOf((((long) latitudeEncoded) << 32) | (longitudeEncoded & 0xFFFFFFFFL))); | ||
} | ||
|
||
public static class Emit { | ||
private final GeoPointFieldScript script; | ||
|
||
public Emit(GeoPointFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
public void emit(double lat, double lon) { | ||
script.emit(lat, lon); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.