-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Add runtime field of type geo_shape #100492
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
13 commits
Select commit
Hold shift + click to select a range
efa0a6d
Add runtime field of type geo_shape
iverase ef5fc98
Update docs/changelog/100492.yaml
iverase 9216e9c
Merge branch 'main' into geoshape-runtime
iverase e6fd8fa
Address review comments
iverase bcded20
Merge branch 'main' into geoshape-runtime
iverase 7c29dd2
index does not throw IOException anymore
iverase 139e69c
doh! need to move the Geometry reader to matches too
iverase 3a3d7bc
Add GeometryFieldScript to MockScriptEngine
iverase 521a7c2
Merge branch 'main' into geoshape-runtime
elasticmachine e1dc926
Merge branch 'main' into geoshape-runtime
iverase 999e8b7
Merge branch 'geoshape-runtime' of github.com:iverase/elasticsearch i…
iverase b56c486
painless bit fixed
iverase 161318b
improve GeoShapeScriptFieldTypeTests
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pr: 100492 | ||
summary: Add runtime field of type `geo_shape` | ||
area: Geo | ||
type: enhancement | ||
issues: | ||
- 61299 |
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
20 changes: 20 additions & 0 deletions
20
...src/main/resources/org/elasticsearch/painless/org.elasticsearch.script.geometry_field.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,20 @@ | ||
# | ||
# 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 and the Server Side Public License, v 1; you may not use this file except | ||
# in compliance with, at your election, the Elastic License 2.0 or the Server | ||
# Side Public License, v 1. | ||
# | ||
|
||
# The whitelist for runtime fields that generate geometries | ||
|
||
# These two whitelists are required for painless to find the classes | ||
class org.elasticsearch.script.GeometryFieldScript @no_import { | ||
} | ||
class org.elasticsearch.script.GeometryFieldScript$Factory @no_import { | ||
} | ||
|
||
static_import { | ||
# The `emit` callback to collect values for the field | ||
void emit(org.elasticsearch.script.GeometryFieldScript, Object) bound_to org.elasticsearch.script.GeometryFieldScript$Emit | ||
} |
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
143 changes: 143 additions & 0 deletions
143
server/src/main/java/org/elasticsearch/script/GeometryFieldScript.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,143 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.geo.GeometryParser; | ||
import org.elasticsearch.common.geo.Orientation; | ||
import org.elasticsearch.geometry.Geometry; | ||
import org.elasticsearch.geometry.GeometryCollection; | ||
import org.elasticsearch.index.mapper.OnScriptError; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Script producing geometries. It generates a unique {@link Geometry} for each document. | ||
*/ | ||
public abstract class GeometryFieldScript extends AbstractFieldScript { | ||
public static final ScriptContext<Factory> CONTEXT = newContext("geometry_field", Factory.class); | ||
|
||
public static final Factory PARSE_FROM_SOURCE = new Factory() { | ||
@Override | ||
public LeafFactory newFactory(String field, Map<String, Object> params, SearchLookup lookup, OnScriptError onScriptError) { | ||
return ctx -> new GeometryFieldScript(field, params, lookup, OnScriptError.FAIL, ctx) { | ||
@Override | ||
public void execute() { | ||
emitFromSource(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean isResultDeterministic() { | ||
return true; | ||
} | ||
}; | ||
|
||
public static Factory leafAdapter(Function<SearchLookup, CompositeFieldScript.LeafFactory> parentFactory) { | ||
return (leafFieldName, params, searchLookup, onScriptError) -> { | ||
CompositeFieldScript.LeafFactory parentLeafFactory = parentFactory.apply(searchLookup); | ||
return (LeafFactory) ctx -> { | ||
CompositeFieldScript compositeFieldScript = parentLeafFactory.newInstance(ctx); | ||
return new GeometryFieldScript(leafFieldName, params, searchLookup, onScriptError, ctx) { | ||
@Override | ||
public void setDocument(int docId) { | ||
compositeFieldScript.setDocument(docId); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
emitFromCompositeScript(compositeFieldScript); | ||
} | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static final String[] PARAMETERS = {}; | ||
|
||
public interface Factory extends ScriptFactory { | ||
LeafFactory newFactory(String fieldName, Map<String, Object> params, SearchLookup searchLookup, OnScriptError onScriptError); | ||
} | ||
|
||
public interface LeafFactory { | ||
GeometryFieldScript newInstance(LeafReaderContext ctx); | ||
} | ||
|
||
private final List<Geometry> geometries = new ArrayList<>(); | ||
|
||
private final GeometryParser geometryParser; | ||
|
||
public GeometryFieldScript( | ||
String fieldName, | ||
Map<String, Object> params, | ||
SearchLookup searchLookup, | ||
OnScriptError onScriptError, | ||
LeafReaderContext ctx | ||
) { | ||
super(fieldName, params, searchLookup, ctx, onScriptError); | ||
geometryParser = new GeometryParser(Orientation.CCW.getAsBoolean(), false, true); | ||
} | ||
|
||
@Override | ||
protected void prepareExecute() { | ||
geometries.clear(); | ||
} | ||
|
||
/** | ||
* Execute the script for the provided {@code docId}, passing results to the {@code consumer} | ||
*/ | ||
public final void runForDoc(int docId, Consumer<Geometry> consumer) { | ||
runForDoc(docId); | ||
consumer.accept(geometry()); | ||
} | ||
|
||
/** | ||
* {@link Geometry} from the last time {@link #runForDoc(int)} was called. | ||
*/ | ||
public final Geometry geometry() { | ||
if (geometries.isEmpty()) { | ||
return null; | ||
} | ||
return geometries.size() == 1 ? geometries.get(0) : new GeometryCollection<>(geometries); | ||
} | ||
|
||
/** | ||
* The number of results produced the last time {@link #runForDoc(int)} was called. It is 1 if | ||
* the document exists, otherwise 0. | ||
*/ | ||
public final int count() { | ||
// Note that emitting multiple geometries gets handled by a GeometryCollection | ||
return geometries.isEmpty() ? 0 : 1; | ||
} | ||
|
||
@Override | ||
protected void emitFromObject(Object value) { | ||
geometries.add(geometryParser.parseGeometry(value)); | ||
} | ||
|
||
public static class Emit { | ||
private final GeometryFieldScript script; | ||
|
||
public Emit(GeometryFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
public void emit(Object object) { | ||
script.checkMaxSize(script.geometries.size()); | ||
script.emitFromObject(object); | ||
} | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
server/src/test/java/org/elasticsearch/index/mapper/GeometryFieldScriptTests.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,82 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.document.StoredField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.tests.index.RandomIndexWriter; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.script.AbstractFieldScript; | ||
import org.elasticsearch.script.GeometryFieldScript; | ||
import org.elasticsearch.script.ScriptContext; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class GeometryFieldScriptTests extends FieldScriptTestCase<GeometryFieldScript.Factory> { | ||
public static final GeometryFieldScript.Factory DUMMY = (fieldName, params, lookup, onScriptError) -> ctx -> new GeometryFieldScript( | ||
fieldName, | ||
params, | ||
lookup, | ||
OnScriptError.FAIL, | ||
ctx | ||
) { | ||
@Override | ||
public void execute() { | ||
emitFromObject("POINT(0 0)"); | ||
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 there perhaps value in testing something more than a point, perhaps a two point linestring? |
||
} | ||
}; | ||
|
||
@Override | ||
protected ScriptContext<GeometryFieldScript.Factory> context() { | ||
return GeometryFieldScript.CONTEXT; | ||
} | ||
|
||
@Override | ||
protected GeometryFieldScript.Factory dummyScript() { | ||
return DUMMY; | ||
} | ||
|
||
@Override | ||
protected GeometryFieldScript.Factory fromSource() { | ||
return GeometryFieldScript.PARSE_FROM_SOURCE; | ||
} | ||
|
||
public void testTooManyValues() throws IOException { | ||
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { | ||
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{}")))); | ||
try (DirectoryReader reader = iw.getReader()) { | ||
GeometryFieldScript script = new GeometryFieldScript( | ||
"test", | ||
Map.of(), | ||
new SearchLookup(field -> null, (ft, lookup, fdt) -> null, (ctx, doc) -> null), | ||
OnScriptError.FAIL, | ||
reader.leaves().get(0) | ||
) { | ||
@Override | ||
public void execute() { | ||
for (int i = 0; i <= AbstractFieldScript.MAX_VALUES; i++) { | ||
new Emit(this).emit("POINT(0 0)"); | ||
} | ||
} | ||
}; | ||
Exception e = expectThrows(IllegalArgumentException.class, script::execute); | ||
assertThat( | ||
e.getMessage(), | ||
equalTo("Runtime field [test] is emitting [101] values while the maximum number of values allowed is [100]") | ||
); | ||
} | ||
} | ||
} | ||
} |
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
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.