-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Store keyword fields that trip ignore_above in binary doc values #137483
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
Changes from all commits
5a48232
9d13387
c2ee79e
9bac251
ab28946
a1d1d2a
ca174e6
0620697
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.mapper; | ||
|
|
||
| import org.apache.lucene.index.BinaryDocValues; | ||
| import org.apache.lucene.index.LeafReader; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.common.io.stream.ByteArrayStreamInput; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public final class BinaryDocValuesSyntheticFieldLoaderLayer implements CompositeSyntheticFieldLoader.DocValuesLayer { | ||
Kubik42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final String fieldName; | ||
|
|
||
| // the binary doc values for a document are all encoded in a single binary array, which this stream knows how to read | ||
| // the doc values in the array take the form of [doc value count][length of value 1][value 1][length of value 2][value 2]... | ||
| private final ByteArrayStreamInput stream; | ||
| private int valueCount; | ||
|
|
||
| public BinaryDocValuesSyntheticFieldLoaderLayer(String fieldName) { | ||
| this.fieldName = fieldName; | ||
| this.stream = new ByteArrayStreamInput(); | ||
| } | ||
|
|
||
| @Override | ||
| public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf) throws IOException { | ||
| BinaryDocValues docValues = leafReader.getBinaryDocValues(fieldName); | ||
|
|
||
| // there are no values associated with this field | ||
| if (docValues == null) { | ||
| valueCount = 0; | ||
| return null; | ||
| } | ||
|
|
||
| return docId -> { | ||
| // there are no more documents to process | ||
| if (docValues.advanceExact(docId) == false) { | ||
| valueCount = 0; | ||
| return false; | ||
| } | ||
|
|
||
| // otherwise, extract the doc values into a stream to later read from | ||
| BytesRef docValuesBytes = docValues.binaryValue(); | ||
| stream.reset(docValuesBytes.bytes, docValuesBytes.offset, docValuesBytes.length); | ||
| valueCount = stream.readVInt(); | ||
|
|
||
| return hasValue(); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(XContentBuilder b) throws IOException { | ||
| for (int i = 0; i < valueCount; i++) { | ||
| // this function already knows how to decode the underlying bytes array, so no need to explicitly call VInt() | ||
| BytesRef valueBytes = stream.readBytesRef(); | ||
| b.value(valueBytes.utf8ToString()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasValue() { | ||
| return valueCount > 0; | ||
| } | ||
|
|
||
| @Override | ||
| public long valueCount() { | ||
| return valueCount; | ||
| } | ||
|
|
||
| @Override | ||
| public String fieldName() { | ||
| return fieldName; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ | |
| import org.apache.lucene.util.automaton.CompiledAutomaton; | ||
| import org.apache.lucene.util.automaton.CompiledAutomaton.AUTOMATON_TYPE; | ||
| import org.apache.lucene.util.automaton.Operations; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.lucene.BytesRefs; | ||
| import org.elasticsearch.common.lucene.Lucene; | ||
| import org.elasticsearch.common.lucene.search.AutomatonQueries; | ||
|
|
@@ -87,6 +89,7 @@ | |
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
@@ -1153,7 +1156,14 @@ private boolean indexValue(DocumentParserContext context, XContentString value) | |
| var utfBytes = value.bytes(); | ||
| var bytesRef = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length()); | ||
| final String fieldName = fieldType().syntheticSourceFallbackFieldName(); | ||
| context.doc().add(new StoredField(fieldName, bytesRef)); | ||
|
|
||
| // store the value in a binary doc values field, create one if it doesn't exist | ||
| MultiValuedBinaryDocValuesField field = (MultiValuedBinaryDocValuesField) context.doc().getByKey(fieldName); | ||
| if (field == null) { | ||
| field = new MultiValuedBinaryDocValuesField(fieldName); | ||
| context.doc().addWithKey(fieldName, field); | ||
| } | ||
| field.add(bytesRef); | ||
|
Comment on lines
-1156
to
+1166
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another BWC issue - old indices will have conflicting fieldinfos for |
||
| } | ||
|
|
||
| return false; | ||
|
|
@@ -1316,15 +1326,56 @@ protected BytesRef preserve(BytesRef value) { | |
| // extra copy of the field for supporting synthetic source. This layer will check that copy. | ||
| if (fieldType().ignoreAbove.valuesPotentiallyIgnored()) { | ||
| final String fieldName = fieldType().syntheticSourceFallbackFieldName(); | ||
| layers.add(new CompositeSyntheticFieldLoader.StoredFieldLayer(fieldName) { | ||
| @Override | ||
| protected void writeValue(Object value, XContentBuilder b) throws IOException { | ||
| BytesRef ref = (BytesRef) value; | ||
| b.utf8Value(ref.bytes, ref.offset, ref.length); | ||
| } | ||
| }); | ||
| layers.add(new BinaryDocValuesSyntheticFieldLoaderLayer(fieldName)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this break BWC? Because old indices will have their ignored values stored in a stored field, and now the mapper can no longer load those values? |
||
| } | ||
|
|
||
| return new CompositeSyntheticFieldLoader(leafFieldName, fullFieldName, layers); | ||
| } | ||
|
|
||
| /** | ||
| * A custom implementation of {@link org.apache.lucene.index.BinaryDocValues} that uses a {@link Set} to maintain a collection of unique | ||
| * binary doc values for fields with multiple values per document. | ||
| */ | ||
| private static final class MultiValuedBinaryDocValuesField extends CustomDocValuesField { | ||
|
|
||
| private final Set<BytesRef> uniqueValues; | ||
Kubik42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private int docValuesByteCount = 0; | ||
|
|
||
| MultiValuedBinaryDocValuesField(String name) { | ||
| super(name); | ||
| // linked hash set to maintain insertion order of elements | ||
| uniqueValues = new LinkedHashSet<>(); | ||
| } | ||
|
|
||
| public void add(final BytesRef value) { | ||
| if (uniqueValues.add(value)) { | ||
| // might as well track these on the go as opposed to having to loop through all entries later | ||
| docValuesByteCount += value.length; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Encodes the collection of binary doc values as a single contiguous binary array, wrapped in {@link BytesRef}. This array takes | ||
| * the form of [doc value count][length of value 1][value 1][length of value 2][value 2]... | ||
| */ | ||
| @Override | ||
| public BytesRef binaryValue() { | ||
| int docValuesCount = uniqueValues.size(); | ||
| // the + 1 is for the total doc values count, which is prefixed at the start of the array | ||
| int streamSize = docValuesByteCount + (docValuesCount + 1) * Integer.BYTES; | ||
|
|
||
| try (BytesStreamOutput out = new BytesStreamOutput(streamSize)) { | ||
| out.writeVInt(docValuesCount); | ||
| for (BytesRef value : uniqueValues) { | ||
| int valueLength = value.length; | ||
| out.writeVInt(valueLength); | ||
| out.writeBytes(value.bytes, value.offset, valueLength); | ||
| } | ||
| return out.bytes().toBytesRef(); | ||
| } catch (IOException e) { | ||
| throw new ElasticsearchException("Failed to get binary value", e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.