Skip to content

Commit 511b032

Browse files
committed
Fix clickbench queries with wildcard queries to work work with binary doc values.
1 parent d2b30bd commit 511b032

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@
5353
import org.elasticsearch.index.fielddata.FieldData;
5454
import org.elasticsearch.index.fielddata.FieldDataContext;
5555
import org.elasticsearch.index.fielddata.IndexFieldData;
56+
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
5657
import org.elasticsearch.index.fielddata.SourceValueFetcherSortedBinaryIndexFieldData;
5758
import org.elasticsearch.index.fielddata.StoredFieldSortedBinaryIndexFieldData;
59+
import org.elasticsearch.index.fielddata.plain.BinaryIndexFieldData;
5860
import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData;
5961
import org.elasticsearch.index.query.AutomatonQueryWithDescription;
6062
import org.elasticsearch.index.query.SearchExecutionContext;
6163
import org.elasticsearch.index.similarity.SimilarityProvider;
64+
import org.elasticsearch.indices.breaker.CircuitBreakerService;
65+
import org.elasticsearch.script.BinaryDocValuesStringFieldScript;
6266
import org.elasticsearch.script.Script;
6367
import org.elasticsearch.script.ScriptCompiler;
6468
import org.elasticsearch.script.SortedSetDocValuesStringFieldScript;
@@ -956,7 +960,10 @@ protected BytesRef storedToBytesRef(Object stored) {
956960
);
957961
}
958962

959-
private SortedSetOrdinalsIndexFieldData.Builder fieldDataFromDocValues() {
963+
private IndexFieldData.Builder fieldDataFromDocValues() {
964+
if (useBinaryDocValues) {
965+
return new BinaryIndexFieldData.Builder(name(), CoreValuesSourceType.KEYWORD);
966+
}
960967
return new SortedSetOrdinalsIndexFieldData.Builder(
961968
name(),
962969
CoreValuesSourceType.KEYWORD,
@@ -1041,7 +1048,9 @@ public Query wildcardQuery(
10411048
}
10421049
return new StringScriptFieldWildcardQuery(
10431050
new Script(""),
1044-
ctx -> new SortedSetDocValuesStringFieldScript(name(), context.lookup(), ctx),
1051+
ctx -> useBinaryDocValues
1052+
? new BinaryDocValuesStringFieldScript(name(), context.lookup(), ctx)
1053+
: new SortedSetDocValuesStringFieldScript(name(), context.lookup(), ctx),
10451054
name(),
10461055
value,
10471056
caseInsensitive
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.script;
11+
12+
import org.apache.lucene.index.BinaryDocValues;
13+
import org.apache.lucene.index.DocValues;
14+
import org.apache.lucene.index.LeafReaderContext;
15+
import org.apache.lucene.util.BytesRef;
16+
import org.elasticsearch.index.mapper.OnScriptError;
17+
import org.elasticsearch.search.lookup.SearchLookup;
18+
19+
import java.io.IOException;
20+
import java.util.Map;
21+
22+
public class BinaryDocValuesStringFieldScript extends StringFieldScript {
23+
private final BinaryDocValues binaryDocValues;
24+
25+
boolean hasValue = false;
26+
27+
public BinaryDocValuesStringFieldScript(String fieldName, SearchLookup searchLookup, LeafReaderContext ctx) {
28+
super(fieldName, Map.of(), searchLookup, OnScriptError.FAIL, ctx);
29+
try {
30+
binaryDocValues = DocValues.getBinary(ctx.reader(), fieldName);
31+
} catch (IOException e) {
32+
throw new IllegalStateException("Cannot load doc values", e);
33+
}
34+
}
35+
36+
@Override
37+
public void setDocument(int docID) {
38+
try {
39+
hasValue = binaryDocValues.advanceExact(docID);
40+
} catch (IOException e) {
41+
throw new IllegalStateException("Cannot load doc values", e);
42+
}
43+
}
44+
45+
@Override
46+
public void execute() {
47+
try {
48+
if (hasValue) {
49+
BytesRef bytesRef = binaryDocValues.binaryValue();
50+
emit(bytesRef.utf8ToString());
51+
}
52+
} catch (IOException e) {
53+
throw new IllegalStateException("Cannot load doc values", e);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)