-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script: keyword fields api support (#81266)
* Script: keyword fields api support Adds basic support for keyword fields in the scripting fields api. Refs: #79105
- Loading branch information
1 parent
d031cfd
commit e7d6706
Showing
5 changed files
with
197 additions
and
6 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: 81266 | ||
summary: "Script: keyword fields api support" | ||
area: Infra/Scripting | ||
type: enhancement | ||
issues: [] |
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
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
131 changes: 131 additions & 0 deletions
131
server/src/main/java/org/elasticsearch/script/field/KeywordDocValuesField.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 KeywordDocValuesField implements DocValuesField<String>, ScriptDocValues.Supplier<String> { | ||
|
||
private final SortedBinaryDocValues input; | ||
private final String name; | ||
|
||
private BytesRefBuilder[] values = new BytesRefBuilder[0]; | ||
private int count; | ||
|
||
// used for backwards compatibility for old-style "doc" access | ||
// as a delegate to this field class | ||
private ScriptDocValues.Strings strings = null; | ||
|
||
public KeywordDocValuesField(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()); | ||
} | ||
}; | ||
} | ||
} |