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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.fielddata.FieldDataContext;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fieldvisitor.LeafStoredFieldLoader;
import org.elasticsearch.index.fieldvisitor.StoredFieldLoader;
import org.elasticsearch.index.mapper.BlockLoader;
import org.elasticsearch.index.mapper.BlockStoredFieldsReader;
Expand Down Expand Up @@ -119,6 +120,9 @@ public String familyTypeName() {

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
if (disableTemplating) {
return storedFieldValueFetcher();
}
return new ValueFetcher() {
BinaryDocValues docValues;

Expand All @@ -133,15 +137,47 @@ public void setNextReader(LeafReaderContext context) {

@Override
public List<Object> fetchValues(Source source, int doc, List<Object> ignoredValues) throws IOException {
if (false == docValues.advanceExact(doc)) {
if (docValues == null || false == docValues.advanceExact(doc)) {
return List.of();
}
return List.of(docValues.binaryValue().utf8ToString());
}

@Override
public StoredFieldsSpec storedFieldsSpec() {
// PatternedTextCompositeValues may require a stored field, but it handles loading this field internally.
// PatternTextCompositeValues may require a stored field, but it handles loading this field internally.
return StoredFieldsSpec.NO_REQUIREMENTS;
}
};
}

private ValueFetcher storedFieldValueFetcher() {
var loader = StoredFieldLoader.create(false, Set.of(storedNamed()));
return new ValueFetcher() {
LeafStoredFieldLoader leafLoader;

@Override
public void setNextReader(LeafReaderContext context) {
try {
this.leafLoader = loader.getLoader(context, null);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public List<Object> fetchValues(Source source, int doc, List<Object> ignoredValues) throws IOException {
leafLoader.advanceTo(doc);
var storedFields = leafLoader.storedFields();
var values = storedFields.get(storedNamed());
if (values == null || values.isEmpty()) {
return List.of();
}
return List.of(((BytesRef) values.getFirst()).utf8ToString());
}

@Override
public StoredFieldsSpec storedFieldsSpec() {
return StoredFieldsSpec.NO_REQUIREMENTS;
}
};
Expand Down Expand Up @@ -174,7 +210,11 @@ private static IOFunction<LeafReaderContext, CheckedIntFunction<List<Object>, IO
return docId -> {
leafLoader.advanceTo(docId);
var storedFields = leafLoader.storedFields();
return storedFields.get(name);
var values = storedFields.get(name);
if (values == null || values.isEmpty()) {
return List.of();
}
return List.of(((BytesRef) values.getFirst()).utf8ToString());
};
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package org.elasticsearch.xpack.logsdb.patterntext;

import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.SortField;
import org.apache.lucene.util.BytesRef;
Expand All @@ -16,6 +16,7 @@
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
import org.elasticsearch.index.fielddata.LeafFieldData;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fieldvisitor.StoredFieldLoader;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.field.DocValuesScriptFieldFactory;
import org.elasticsearch.script.field.KeywordDocValuesField;
Expand All @@ -28,6 +29,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Set;

public class PatternTextIndexFieldData implements IndexFieldData<LeafFieldData> {

Expand Down Expand Up @@ -71,8 +73,12 @@ public LeafFieldData load(LeafReaderContext context) {

@Override
public LeafFieldData loadDirect(LeafReaderContext context) throws IOException {
LeafReader leafReader = context.reader();
var values = PatternTextCompositeValues.from(leafReader, fieldType);
final BinaryDocValues values;
if (fieldType.disableTemplating()) {
values = loadStoredFieldDocValues(context);
} else {
values = PatternTextCompositeValues.from(context.reader(), fieldType);
}
return new LeafFieldData() {

final ToScriptFieldFactory<SortedBinaryDocValues> factory = KeywordDocValuesField::new;
Expand All @@ -87,7 +93,7 @@ public SortedBinaryDocValues getBytesValues() {
return new SortedBinaryDocValues() {
@Override
public boolean advanceExact(int doc) throws IOException {
return values.advanceExact(doc);
return values != null && values.advanceExact(doc);
}

@Override
Expand All @@ -109,6 +115,52 @@ public long ramBytesUsed() {
};
}

private BinaryDocValues loadStoredFieldDocValues(LeafReaderContext context) throws IOException {
var loader = StoredFieldLoader.create(false, Set.of(fieldType.storedNamed()));
var leafLoader = loader.getLoader(context, null);
return new BinaryDocValues() {
BytesRef currentValue;

@Override
public boolean advanceExact(int target) throws IOException {
leafLoader.advanceTo(target);
var storedFields = leafLoader.storedFields();
var fieldValues = storedFields.get(fieldType.storedNamed());
if (fieldValues != null && fieldValues.isEmpty() == false) {
currentValue = (BytesRef) fieldValues.getFirst();
return true;
}
currentValue = null;
return false;
}

@Override
public BytesRef binaryValue() {
return currentValue;
}

@Override
public int docID() {
throw new UnsupportedOperationException();
}

@Override
public int nextDoc() {
throw new UnsupportedOperationException();
}

@Override
public int advance(int target) {
throw new UnsupportedOperationException();
}

@Override
public long cost() {
return 0;
}
};
}

@Override
public SortField sortField(Object missingValue, MultiValueMode sortMode, XFieldComparatorSource.Nested nested, boolean reverse) {
throw new IllegalArgumentException("not supported for source pattern_text field type");
Expand Down
Loading