forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,733 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
142 changes: 142 additions & 0 deletions
142
server/src/main/java/org/elasticsearch/script/GeometryFieldScript.java
This file contains 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,142 @@ | ||
/* | ||
* 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() { | ||
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 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 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)"); | ||
} | ||
}; | ||
|
||
@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 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.