Skip to content

Commit 179147b

Browse files
ioanatiachrisparrinello
authored andcommitted
ES|QL: Improve value loading for match_only_text mapping (elastic#137026)
1 parent 92901d7 commit 179147b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docs/changelog/137026.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 137026
2+
summary: Improve value loading for `match_only_text` mapping
3+
area: ES|QL
4+
type: enhancement
5+
issues: []

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldMapper.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,28 @@ protected String delegatingTo() {
602602
}
603603
}
604604

605+
/*
606+
* TODO: This duplicates code from TextFieldMapper
607+
* If this is a sub-text field try and return the parent's loader. Text
608+
* fields will always be slow to load and if the parent is exact then we
609+
* should use that instead.
610+
*/
611+
String parentField = blContext.parentField(name());
612+
if (parentField != null) {
613+
MappedFieldType parent = blContext.lookup().fieldType(parentField);
614+
if (parent.typeName().equals(KeywordFieldMapper.CONTENT_TYPE)) {
615+
KeywordFieldMapper.KeywordFieldType kwd = (KeywordFieldMapper.KeywordFieldType) parent;
616+
if (kwd.hasNormalizer() == false && (kwd.hasDocValues() || kwd.isStored())) {
617+
return new BlockLoader.Delegating(kwd.blockLoader(blContext)) {
618+
@Override
619+
protected String delegatingTo() {
620+
return kwd.name();
621+
}
622+
};
623+
}
624+
}
625+
}
626+
605627
// fallback to _source (synthetic or not)
606628
SourceValueFetcher fetcher = SourceValueFetcher.toString(blContext.sourcePaths(name()), blContext.indexSettings());
607629
// MatchOnlyText never has norms, so we have to use the field names field

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/MatchOnlyTextFieldTypeTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.index.mapper.TextSearchInfo;
5050
import org.elasticsearch.index.mapper.extras.MatchOnlyTextFieldMapper.MatchOnlyTextFieldType;
5151
import org.elasticsearch.script.ScriptCompiler;
52+
import org.elasticsearch.search.lookup.SearchLookup;
5253
import org.hamcrest.Matchers;
5354

5455
import java.io.IOException;
@@ -372,4 +373,36 @@ public void testBlockLoaderDoesNotUseSyntheticSourceDelegateWhenIgnoreAboveIsSet
372373
// verify that we don't delegate anything
373374
assertThat(blockLoader, Matchers.not(Matchers.instanceOf(BlockLoader.Delegating.class)));
374375
}
376+
377+
public void testBlockLoaderDelegateToKeywordFieldWhenSyntheticSourceIsDisabled() {
378+
String parentFieldName = "foo";
379+
String childFieldName = "foo.bar";
380+
// given
381+
KeywordFieldMapper.KeywordFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType(
382+
parentFieldName,
383+
true,
384+
true,
385+
Collections.emptyMap()
386+
);
387+
388+
MatchOnlyTextFieldMapper.MatchOnlyTextFieldType ft = new MatchOnlyTextFieldMapper.MatchOnlyTextFieldType(
389+
childFieldName,
390+
new TextSearchInfo(TextFieldMapper.Defaults.FIELD_TYPE, null, Lucene.STANDARD_ANALYZER, Lucene.STANDARD_ANALYZER),
391+
mock(NamedAnalyzer.class),
392+
false,
393+
Collections.emptyMap(),
394+
true,
395+
false,
396+
keywordFieldType
397+
);
398+
399+
var mockedSearchLookup = mock(SearchLookup.class);
400+
when(mockedSearchLookup.fieldType(parentFieldName)).thenReturn(keywordFieldType);
401+
402+
var mockedBlockLoaderContext = mock(MappedFieldType.BlockLoaderContext.class);
403+
when(mockedBlockLoaderContext.parentField(childFieldName)).thenReturn(parentFieldName);
404+
when(mockedBlockLoaderContext.lookup()).thenReturn(mockedSearchLookup);
405+
BlockLoader blockLoader = ft.blockLoader(mockedBlockLoaderContext);
406+
assertThat(blockLoader, Matchers.instanceOf(BlockLoader.Delegating.class));
407+
}
375408
}

0 commit comments

Comments
 (0)