Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/82763.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 82763
summary: Make wildcard accessible from the scripting field API
area: Infra/Scripting
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
package org.elasticsearch.index.fielddata.plain;

import org.apache.lucene.index.BinaryDocValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.script.field.DelegateDocValuesField;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.script.field.DocValuesField;
import org.elasticsearch.script.field.ToScriptField;

final class StringBinaryDVLeafFieldData extends AbstractBinaryDVLeafFieldData {
StringBinaryDVLeafFieldData(BinaryDocValues values) {

protected final ToScriptField<SortedBinaryDocValues> toScriptField;

StringBinaryDVLeafFieldData(BinaryDocValues values, ToScriptField<SortedBinaryDocValues> toScriptField) {
super(values);

this.toScriptField = toScriptField;
}

@Override
public DocValuesField<?> getScriptField(String name) {
return new DelegateDocValuesField(new ScriptDocValues.Strings(new ScriptDocValues.StringsSupplier(getBytesValues())), name);
return toScriptField.getScriptField(getBytesValues(), name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource;
import org.elasticsearch.script.field.ToScriptField;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
Expand All @@ -27,10 +29,16 @@ public class StringBinaryIndexFieldData implements IndexFieldData<StringBinaryDV

protected final String fieldName;
protected final ValuesSourceType valuesSourceType;
protected final ToScriptField<SortedBinaryDocValues> toScriptField;

public StringBinaryIndexFieldData(String fieldName, ValuesSourceType valuesSourceType) {
public StringBinaryIndexFieldData(
String fieldName,
ValuesSourceType valuesSourceType,
ToScriptField<SortedBinaryDocValues> toScriptField
) {
this.fieldName = fieldName;
this.valuesSourceType = valuesSourceType;
this.toScriptField = toScriptField;
}

@Override
Expand All @@ -52,7 +60,7 @@ public SortField sortField(Object missingValue, MultiValueMode sortMode, Nested
@Override
public StringBinaryDVLeafFieldData load(LeafReaderContext context) {
try {
return new StringBinaryDVLeafFieldData(DocValues.getBinary(context.reader(), fieldName));
return new StringBinaryDVLeafFieldData(DocValues.getBinary(context.reader(), fieldName), toScriptField);
} catch (IOException e) {
throw new IllegalStateException("Cannot load doc values", e);
}
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugin/wildcard/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import org.elasticsearch.gradle.internal.info.BuildParams

apply plugin: 'elasticsearch.internal-es-plugin'
apply plugin: 'elasticsearch.internal-yaml-rest-test'

esplugin {
name 'wildcard'
description 'A plugin for a keyword field type with efficient wildcard search'
classname 'org.elasticsearch.xpack.wildcard.Wildcard'
extendedPlugins = ['x-pack-core']
extendedPlugins = ['x-pack-core', 'lang-painless']
}
archivesBaseName = 'x-pack-wildcard'

dependencies {
compileOnly project(':modules:lang-painless:spi')
compileOnly project(path: xpackModule('core'))
testImplementation(testArtifact(project(xpackModule('core'))))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.wildcard;

import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.script.field.AbstractKeywordDocValuesField;

public class WildcardDocValuesField extends AbstractKeywordDocValuesField {

public WildcardDocValuesField(SortedBinaryDocValues input, String name) {
super(input, name);
}
}
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.wildcard;

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 WildcardPainlessExtension implements PainlessExtension {
private static final Whitelist WHITELIST = WhitelistLoader.loadFromResourceFiles(
WildcardPainlessExtension.class,
"org.elasticsearch.xpack.wildcard.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.wildcard.WildcardDocValuesField;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -834,7 +835,11 @@ public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
@Override
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, Supplier<SearchLookup> searchLookup) {
failIfNoDocValues();
return (cache, breakerService) -> new StringBinaryIndexFieldData(name(), CoreValuesSourceType.KEYWORD);
return (cache, breakerService) -> new StringBinaryIndexFieldData(
name(),
CoreValuesSourceType.KEYWORD,
WildcardDocValuesField::new
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.elasticsearch.xpack.wildcard.WildcardPainlessExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# 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.wildcard.WildcardDocValuesField @dynamic_type {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.wildcard;

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;

/** Runs yaml rest tests */
public class WildcardClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {

public WildcardClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}

@ParametersFactory
public static Iterable<Object[]> parameters() throws Exception {
return ESClientYamlSuiteTestCase.createParameters();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
setup:
- do:
indices.create:
index: test
body:
settings:
number_of_shards: 1
mappings:
properties:
test_wc:
type: wildcard

- do:
bulk:
index: test
refresh: true
body: |
{ "index": {"_id" : "1"} }
{ "test_wc": "abc" }
{ "index": {"_id" : "2"} }
{ "test_wc": ["adc", "aec"] }

---
"Wildcard Fields API":
- do:
search:
index: test
body:
query:
wildcard:
test_wc:
value: "a*c"
script_fields:
constOne:
script:
source: "/* avoid stash */ $('test_wc', 'dne')"
constTwo:
script:
source: "field('test_wc').get(0, 'dne') + field('test_wc').get(1, 'xyz')"
constThree:
script:
source: "String s = ''; for (String z : field('test_wc')) s += z; return s"

- match: { hits.hits.0.fields.constOne.0: "abc" }
- match: { hits.hits.0.fields.constTwo.0: "abcxyz" }
- match: { hits.hits.0.fields.constThree.0: "abc" }
- match: { hits.hits.1.fields.constOne.0: "adc" }
- match: { hits.hits.1.fields.constTwo.0: "adcaec" }
- match: { hits.hits.1.fields.constThree.0: "adcaec" }