-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add support for GeoShape to the scripting fields API #81617
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fefc69
Add a geoshape field to the scripting fields api
jdconrad 40e95e3
add skeleton for painless allow list
jdconrad cb085cb
add tests
jdconrad 337bc9d
Merge branch 'master' into geoshape
jdconrad 0ee29ce
Update docs/changelog/81617.yaml
jdconrad 571b7ae
Merge branch 'master' into geoshape
jdconrad 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 81617 | ||
| summary: Add support for `GeoShape` to the scripting fields API | ||
| area: Infra/Scripting | ||
| type: enhancement | ||
| issues: [] |
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
41 changes: 41 additions & 0 deletions
41
...lugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/SpatialPainlessExtension.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,41 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.spatial; | ||
|
|
||
| import org.elasticsearch.painless.spi.PainlessExtension; | ||
| import org.elasticsearch.painless.spi.Whitelist; | ||
| import org.elasticsearch.painless.spi.WhitelistLoader; | ||
| import org.elasticsearch.script.ScriptContext; | ||
| import org.elasticsearch.script.ScriptModule; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class SpatialPainlessExtension implements PainlessExtension { | ||
|
|
||
| private static final List<Whitelist> WHITELISTS = List.of( | ||
| WhitelistLoader.loadFromResourceFiles( | ||
| SpatialPainlessExtension.class, | ||
| "org.elasticsearch.xpack.spatial.index.fielddata.txt", | ||
| "org.elasticsearch.xpack.spatial.index.fielddata.plain.txt", | ||
| "org.elasticsearch.xpack.spatial.index.mapper.txt" | ||
| ) | ||
| ); | ||
|
|
||
| @Override | ||
| public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() { | ||
| Map<ScriptContext<?>, List<Whitelist>> contextWhitelistMap = new HashMap<>(); | ||
|
|
||
| for (ScriptContext<?> context : ScriptModule.CORE_CONTEXTS.values()) { | ||
| contextWhitelistMap.put(context, WHITELISTS); | ||
| } | ||
|
|
||
| return contextWhitelistMap; | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
|
|
@@ -15,7 +15,9 @@ | |
| import org.apache.lucene.search.Query; | ||
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.common.Explicit; | ||
| import org.elasticsearch.common.geo.GeoBoundingBox; | ||
| import org.elasticsearch.common.geo.GeoFormatterFactory; | ||
| import org.elasticsearch.common.geo.GeoPoint; | ||
| import org.elasticsearch.common.geo.GeoShapeUtils; | ||
| import org.elasticsearch.common.geo.GeometryParser; | ||
| import org.elasticsearch.common.geo.Orientation; | ||
|
|
@@ -24,6 +26,7 @@ | |
| import org.elasticsearch.common.logging.DeprecationLogger; | ||
| import org.elasticsearch.geometry.Geometry; | ||
| import org.elasticsearch.index.fielddata.IndexFieldData; | ||
| import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
| import org.elasticsearch.index.mapper.AbstractShapeGeometryFieldMapper; | ||
| import org.elasticsearch.index.mapper.DocumentParserContext; | ||
| import org.elasticsearch.index.mapper.FieldMapper; | ||
|
|
@@ -39,16 +42,19 @@ | |
| import org.elasticsearch.index.query.QueryShardException; | ||
| import org.elasticsearch.index.query.SearchExecutionContext; | ||
| import org.elasticsearch.legacygeo.mapper.LegacyGeoShapeFieldMapper; | ||
| import org.elasticsearch.script.field.DelegateDocValuesField; | ||
| import org.elasticsearch.script.field.DocValuesField; | ||
| import org.elasticsearch.search.lookup.SearchLookup; | ||
| import org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues; | ||
| import org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractAtomicGeoShapeShapeFieldData; | ||
| import org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractLatLonShapeIndexFieldData; | ||
| import org.elasticsearch.xpack.spatial.search.aggregations.support.GeoShapeValuesSourceType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
@@ -175,16 +181,7 @@ public GeoShapeWithDocValuesFieldType( | |
|
|
||
| public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) { | ||
| failIfNoDocValues(); | ||
| return new AbstractLatLonShapeIndexFieldData.Builder( | ||
| name(), | ||
| GeoShapeValuesSourceType.instance(), | ||
| (dv, n) -> new DelegateDocValuesField( | ||
| new AbstractAtomicGeoShapeShapeFieldData.GeoShapeScriptValues( | ||
| new AbstractAtomicGeoShapeShapeFieldData.GeoShapeSupplier(dv) | ||
| ), | ||
| n | ||
| ) | ||
| ); | ||
| return new AbstractLatLonShapeIndexFieldData.Builder(name(), GeoShapeValuesSourceType.instance(), GeoShapeDocValuesField::new); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -344,4 +341,114 @@ protected void checkIncomingMergeType(FieldMapper mergeWith) { | |
| } | ||
| super.checkIncomingMergeType(mergeWith); | ||
| } | ||
|
|
||
| public static class GeoShapeDocValuesField | ||
| implements | ||
| DocValuesField<GeoShapeValues.GeoShapeValue>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this formatting intended?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guess so since this is what spotless produced. |
||
| ScriptDocValues.GeometrySupplier<GeoShapeValues.GeoShapeValue> { | ||
|
|
||
| private final GeoShapeValues in; | ||
| protected final String name; | ||
|
|
||
| private GeoShapeValues.GeoShapeValue value; | ||
|
|
||
| // maintain bwc by making bounding box and centroid available to GeoShapeValues (ScriptDocValues) | ||
| private final GeoPoint centroid = new GeoPoint(); | ||
| private final GeoBoundingBox boundingBox = new GeoBoundingBox(new GeoPoint(), new GeoPoint()); | ||
| private AbstractAtomicGeoShapeShapeFieldData.GeoShapeScriptValues geoShapeScriptValues; | ||
|
|
||
| public GeoShapeDocValuesField(GeoShapeValues in, String name) { | ||
| this.in = in; | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public void setNextDocId(int docId) throws IOException { | ||
| if (in.advanceExact(docId)) { | ||
| value = in.value(); | ||
| centroid.reset(value.lat(), value.lon()); | ||
| boundingBox.topLeft().reset(value.boundingBox().maxY(), value.boundingBox().minX()); | ||
| boundingBox.bottomRight().reset(value.boundingBox().minY(), value.boundingBox().maxX()); | ||
| } else { | ||
| value = null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ScriptDocValues<GeoShapeValues.GeoShapeValue> getScriptDocValues() { | ||
| if (geoShapeScriptValues == null) { | ||
| geoShapeScriptValues = new AbstractAtomicGeoShapeShapeFieldData.GeoShapeScriptValues(this); | ||
| } | ||
|
|
||
| return geoShapeScriptValues; | ||
| } | ||
|
|
||
| @Override | ||
| public GeoShapeValues.GeoShapeValue getInternal(int index) { | ||
| if (index != 0) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| // maintain bwc by making centroid available to GeoShapeValues (ScriptDocValues) | ||
| @Override | ||
| public GeoPoint getInternalCentroid() { | ||
| return centroid; | ||
| } | ||
|
|
||
| // maintain bwc by making centroid available to GeoShapeValues (ScriptDocValues) | ||
| @Override | ||
| public GeoBoundingBox getInternalBoundingBox() { | ||
| return boundingBox; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return value == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return value == null ? 0 : 1; | ||
| } | ||
|
|
||
| public GeoShapeValues.GeoShapeValue get(GeoShapeValues.GeoShapeValue defaultValue) { | ||
| return get(0, defaultValue); | ||
| } | ||
|
|
||
| public GeoShapeValues.GeoShapeValue get(int index, GeoShapeValues.GeoShapeValue defaultValue) { | ||
| if (isEmpty() || index != 0) { | ||
| return defaultValue; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<GeoShapeValues.GeoShapeValue> iterator() { | ||
| return new Iterator<GeoShapeValues.GeoShapeValue>() { | ||
| private int index = 0; | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return index < size(); | ||
| } | ||
|
|
||
| @Override | ||
| public GeoShapeValues.GeoShapeValue next() { | ||
| if (hasNext() == false) { | ||
| throw new NoSuchElementException(); | ||
| } | ||
| return value; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
...ial/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension
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 @@ | ||
| org.elasticsearch.xpack.spatial.SpatialPainlessExtension |
4 changes: 4 additions & 0 deletions
4
...org/elasticsearch/xpack/spatial/org.elasticsearch.xpack.spatial.index.fielddata.plain.txt
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,4 @@ | ||
| class org.elasticsearch.xpack.spatial.index.fielddata.plain.AbstractAtomicGeoShapeShapeFieldData$GeoShapeScriptValues { | ||
| GeoShapeValues.GeoShapeValue get(int) | ||
| GeoShapeValues.GeoShapeValue getValue() | ||
| } |
2 changes: 2 additions & 0 deletions
2
...urces/org/elasticsearch/xpack/spatial/org.elasticsearch.xpack.spatial.index.fielddata.txt
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,2 @@ | ||
| class org.elasticsearch.xpack.spatial.index.fielddata.GeoShapeValues$GeoShapeValue { | ||
| } |
4 changes: 4 additions & 0 deletions
4
...esources/org/elasticsearch/xpack/spatial/org.elasticsearch.xpack.spatial.index.mapper.txt
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,4 @@ | ||
| class org.elasticsearch.xpack.spatial.index.mapper.GeoShapeWithDocValuesFieldMapper$GeoShapeDocValuesField { | ||
| GeoShapeValues.GeoShapeValue get(GeoShapeValues.GeoShapeValue) | ||
| GeoShapeValues.GeoShapeValue get(int, GeoShapeValues.GeoShapeValue) | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why split these across three different files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it easier to find the classes when they're separated out by package. I realize there's not a huge amount allow-listed here, yet, but I think we want to expand this quite a bit.