forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script: fields API for x-pack constant_keyword (elastic#82292)
* Script: fields API for x-pack constant_keyword Adds the fields API for the constant_keyword field mapper Moves implementation to `AbstractKeywordDocValuesField`, allowing code sharing with `KeywordDocValuesField`. API: ``` field('const').get('default') field('const').get(0, 'default') ``` Refs: elastic#79105
- Loading branch information
1 parent
d77b6ce
commit a0b2cdc
Showing
11 changed files
with
282 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 82292 | ||
summary: "Script: fields API for x-pack `constant_keyword`" | ||
area: Infra/Scripting | ||
type: enhancement | ||
issues: [] |
131 changes: 131 additions & 0 deletions
131
server/src/main/java/org/elasticsearch/script/field/AbstractKeywordDocValuesField.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,131 @@ | ||
/* | ||
* 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.field; | ||
|
||
import org.apache.lucene.util.ArrayUtil; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.apache.lucene.util.BytesRefBuilder; | ||
import org.elasticsearch.index.fielddata.ScriptDocValues; | ||
import org.elasticsearch.index.fielddata.SortedBinaryDocValues; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
public class AbstractKeywordDocValuesField implements DocValuesField<String>, ScriptDocValues.Supplier<String> { | ||
|
||
protected final SortedBinaryDocValues input; | ||
protected final String name; | ||
|
||
protected BytesRefBuilder[] values = new BytesRefBuilder[0]; | ||
protected int count; | ||
|
||
// used for backwards compatibility for old-style "doc" access | ||
// as a delegate to this field class | ||
protected ScriptDocValues.Strings strings = null; | ||
|
||
public AbstractKeywordDocValuesField(SortedBinaryDocValues input, String name) { | ||
this.input = input; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void setNextDocId(int docId) throws IOException { | ||
if (input.advanceExact(docId)) { | ||
resize(input.docValueCount()); | ||
for (int i = 0; i < count; i++) { | ||
// We need to make a copy here, because BytesBinaryDVLeafFieldData's SortedBinaryDocValues | ||
// implementation reuses the returned BytesRef. Otherwise we would end up with the same BytesRef | ||
// instance for all slots in the values array. | ||
values[i].copyBytes(input.nextValue()); | ||
} | ||
} else { | ||
resize(0); | ||
} | ||
} | ||
|
||
private void resize(int newSize) { | ||
count = newSize; | ||
assert count >= 0 : "size must be positive (got " + count + "): likely integer overflow?"; | ||
if (newSize > values.length) { | ||
final int oldLength = values.length; | ||
values = ArrayUtil.grow(values, count); | ||
for (int i = oldLength; i < values.length; ++i) { | ||
values[i] = new BytesRefBuilder(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ScriptDocValues<String> getScriptDocValues() { | ||
if (strings == null) { | ||
strings = new ScriptDocValues.Strings(this); | ||
} | ||
|
||
return strings; | ||
} | ||
|
||
// this method is required to support the Boolean return values | ||
// for the old-style "doc" access in ScriptDocValues | ||
@Override | ||
public String getInternal(int index) { | ||
return bytesToString(values[index].toBytesRef()); | ||
} | ||
|
||
protected String bytesToString(BytesRef bytesRef) { | ||
return bytesRef.utf8ToString(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return count == 0; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return count; | ||
} | ||
|
||
public String get(String defaultValue) { | ||
return get(0, defaultValue); | ||
} | ||
|
||
public String get(int index, String defaultValue) { | ||
if (isEmpty() || index < 0 || index >= count) { | ||
return defaultValue; | ||
} | ||
|
||
return bytesToString(values[index].toBytesRef()); | ||
} | ||
|
||
@Override | ||
public Iterator<String> iterator() { | ||
return new Iterator<String>() { | ||
private int index = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return index < count; | ||
} | ||
|
||
@Override | ||
public String next() { | ||
if (hasNext() == false) { | ||
throw new NoSuchElementException(); | ||
} | ||
return bytesToString(values[index++].toBytesRef()); | ||
} | ||
}; | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
.../src/main/java/org/elasticsearch/xpack/constantkeyword/ConstantKeywordDocValuesField.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,17 @@ | ||
/* | ||
* 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.constantkeyword; | ||
|
||
import org.elasticsearch.index.fielddata.SortedBinaryDocValues; | ||
import org.elasticsearch.script.field.AbstractKeywordDocValuesField; | ||
|
||
public class ConstantKeywordDocValuesField extends AbstractKeywordDocValuesField { | ||
public ConstantKeywordDocValuesField(SortedBinaryDocValues input, String name) { | ||
super(input, name); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...c/main/java/org/elasticsearch/xpack/constantkeyword/ConstantKeywordPainlessExtension.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,37 @@ | ||
/* | ||
* 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.constantkeyword; | ||
|
||
import org.elasticsearch.painless.spi.PainlessExtension; | ||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.painless.spi.WhitelistLoader; | ||
import org.elasticsearch.script.ScriptContext; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.elasticsearch.script.ScriptModule.CORE_CONTEXTS; | ||
|
||
public class ConstantKeywordPainlessExtension implements PainlessExtension { | ||
private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles( | ||
ConstantKeywordPainlessExtension.class, | ||
"org.elasticsearch.xpack.constantkeyword.txt" | ||
); | ||
|
||
@Override | ||
public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() { | ||
List<Whitelist> whitelist = singletonList(WHITELIST); | ||
Map<ScriptContext<?>, List<Whitelist>> contextWhitelists = new HashMap<>(CORE_CONTEXTS.size()); | ||
for (ScriptContext<?> scriptContext : CORE_CONTEXTS.values()) { | ||
contextWhitelists.put(scriptContext, whitelist); | ||
} | ||
return contextWhitelists; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
...ord/src/main/resources/META-INF/services/org.elasticsearch.painless.spi.PainlessExtension
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 @@ | ||
org.elasticsearch.xpack.constantkeyword.ConstantKeywordPainlessExtension |
12 changes: 12 additions & 0 deletions
12
...urces/org/elasticsearch/xpack/constantkeyword/org.elasticsearch.xpack.constantkeyword.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,12 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
class org.elasticsearch.xpack.constantkeyword.ConstantKeywordDocValuesField @dynamic_type { | ||
String get(String) | ||
String get(int, String) | ||
} |
Oops, something went wrong.