-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add boolean values script fields #60830
Merged
nik9000
merged 7 commits into
elastic:feature/runtime_fields
from
nik9000:script_field_boolean
Aug 17, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5d113a
Add boolean values script fields
nik9000 da4b24f
Merge branch 'feature/runtime_fields' into script_field_boolean
nik9000 97b51fb
Fixup
nik9000 9667b9c
Merge branch 'feature/runtime_fields' into script_field_boolean
nik9000 ce7b062
Fixup after merge
nik9000 7173f5b
Universal parsing
nik9000 21a810f
Dualing queries
nik9000 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 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
92 changes: 92 additions & 0 deletions
92
...-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.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,92 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.elasticsearch.common.Booleans; | ||
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 java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public abstract class BooleanScriptFieldScript extends AbstractScriptFieldScript { | ||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("boolean_script_field", Factory.class); | ||
|
||
static List<Whitelist> whitelist() { | ||
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "boolean_whitelist.txt")); | ||
} | ||
|
||
public static final String[] PARAMETERS = {}; | ||
|
||
public interface Factory extends ScriptFactory { | ||
LeafFactory newFactory(Map<String, Object> params, SearchLookup searchLookup); | ||
} | ||
|
||
public interface LeafFactory { | ||
BooleanScriptFieldScript newInstance(LeafReaderContext ctx) throws IOException; | ||
} | ||
|
||
private int trues; | ||
private int falses; | ||
|
||
public BooleanScriptFieldScript(Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) { | ||
super(params, searchLookup, ctx); | ||
} | ||
|
||
/** | ||
* Execute the script for the provided {@code docId}. | ||
*/ | ||
public final void runForDoc(int docId) { | ||
trues = 0; | ||
falses = 0; | ||
setDocument(docId); | ||
execute(); | ||
} | ||
|
||
/** | ||
* How many {@code true} values were returned for this document. | ||
*/ | ||
public final int trues() { | ||
return trues; | ||
} | ||
|
||
/** | ||
* How many {@code false} values were returned for this document. | ||
*/ | ||
public final int falses() { | ||
return falses; | ||
} | ||
|
||
private void collectValue(boolean v) { | ||
if (v) { | ||
trues++; | ||
} else { | ||
falses++; | ||
} | ||
} | ||
|
||
public static boolean parse(Object str) { | ||
return Booleans.parseBoolean(str.toString()); | ||
} | ||
|
||
public static class Value { | ||
private final BooleanScriptFieldScript script; | ||
|
||
public Value(BooleanScriptFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
public void value(boolean v) { | ||
script.collectValue(v); | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...src/main/java/org/elasticsearch/xpack/runtimefields/fielddata/ScriptBooleanDocValues.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,39 @@ | ||
/* | ||
* 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.elasticsearch.index.fielddata.AbstractSortedNumericDocValues; | ||
import org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript; | ||
|
||
import java.io.IOException; | ||
|
||
public final class ScriptBooleanDocValues extends AbstractSortedNumericDocValues { | ||
private final BooleanScriptFieldScript script; | ||
private int cursor; | ||
|
||
ScriptBooleanDocValues(BooleanScriptFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
@Override | ||
public boolean advanceExact(int docId) { | ||
script.runForDoc(docId); | ||
cursor = 0; | ||
return script.trues() > 0 || script.falses() > 0; | ||
} | ||
|
||
@Override | ||
public long nextValue() throws IOException { | ||
// Emit all false values before all true values | ||
return cursor++ < script.falses() ? 0 : 1; | ||
} | ||
|
||
@Override | ||
public int docValueCount() { | ||
return script.trues() + script.falses(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...src/main/java/org/elasticsearch/xpack/runtimefields/fielddata/ScriptBooleanFieldData.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,99 @@ | ||
/* | ||
* 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.index.SortedNumericDocValues; | ||
import org.elasticsearch.ExceptionsHelper; | ||
import org.elasticsearch.index.fielddata.IndexFieldData; | ||
import org.elasticsearch.index.fielddata.IndexFieldDataCache; | ||
import org.elasticsearch.index.fielddata.IndexNumericFieldData; | ||
import org.elasticsearch.index.fielddata.plain.LeafLongFieldData; | ||
import org.elasticsearch.index.mapper.MapperService; | ||
import org.elasticsearch.indices.breaker.CircuitBreakerService; | ||
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; | ||
import org.elasticsearch.search.aggregations.support.ValuesSourceType; | ||
import org.elasticsearch.xpack.runtimefields.BooleanScriptFieldScript; | ||
|
||
import java.io.IOException; | ||
|
||
public final class ScriptBooleanFieldData extends IndexNumericFieldData { | ||
|
||
public static class Builder implements IndexFieldData.Builder { | ||
private final String name; | ||
private final BooleanScriptFieldScript.LeafFactory leafFactory; | ||
|
||
public Builder(String name, BooleanScriptFieldScript.LeafFactory leafFactory) { | ||
this.name = name; | ||
this.leafFactory = leafFactory; | ||
} | ||
|
||
@Override | ||
public ScriptBooleanFieldData build(IndexFieldDataCache cache, CircuitBreakerService breakerService, MapperService mapperService) { | ||
return new ScriptBooleanFieldData(name, leafFactory); | ||
} | ||
} | ||
|
||
private final String fieldName; | ||
private final BooleanScriptFieldScript.LeafFactory leafFactory; | ||
|
||
private ScriptBooleanFieldData(String fieldName, BooleanScriptFieldScript.LeafFactory leafFactory) { | ||
this.fieldName = fieldName; | ||
this.leafFactory = leafFactory; | ||
} | ||
|
||
@Override | ||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
@Override | ||
public ValuesSourceType getValuesSourceType() { | ||
return CoreValuesSourceType.BOOLEAN; | ||
} | ||
|
||
@Override | ||
public ScriptBooleanLeafFieldData load(LeafReaderContext context) { | ||
try { | ||
return loadDirect(context); | ||
} catch (Exception e) { | ||
throw ExceptionsHelper.convertToElastic(e); | ||
} | ||
} | ||
|
||
@Override | ||
public ScriptBooleanLeafFieldData loadDirect(LeafReaderContext context) throws IOException { | ||
return new ScriptBooleanLeafFieldData(new ScriptBooleanDocValues(leafFactory.newInstance(context))); | ||
} | ||
|
||
@Override | ||
public NumericType getNumericType() { | ||
return NumericType.BOOLEAN; | ||
} | ||
|
||
@Override | ||
protected boolean sortRequiresCustomComparator() { | ||
return true; | ||
} | ||
|
||
public static class ScriptBooleanLeafFieldData extends LeafLongFieldData { | ||
private final ScriptBooleanDocValues scriptBooleanDocValues; | ||
|
||
ScriptBooleanLeafFieldData(ScriptBooleanDocValues scriptBooleanDocValues) { | ||
super(0, NumericType.BOOLEAN); | ||
this.scriptBooleanDocValues = scriptBooleanDocValues; | ||
} | ||
|
||
@Override | ||
public SortedNumericDocValues getLongValues() { | ||
return scriptBooleanDocValues; | ||
} | ||
|
||
@Override | ||
public void close() {} | ||
} | ||
} |
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.
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.
I must say that multiple values for booleans look really weird, especially trying to summarize multiple values into one, and re-sorting them. Is this behaviour documented anywhere?
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'm not sure it is, though it is what happens when you use doc values for out of the box booleans.