Skip to content

Commit 8b5a9cc

Browse files
committed
Highlighting: constant_keyword
constant_keyword are now treated like standard keyword and can be highlighted in kibana. closes #85596
1 parent f379b04 commit 8b5a9cc

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import org.elasticsearch.core.Nullable;
1919
import org.elasticsearch.index.query.QueryRewriteContext;
2020
import org.elasticsearch.index.query.SearchExecutionContext;
21+
import org.elasticsearch.search.fetch.FetchContext;
2122

23+
import java.io.IOException;
2224
import java.util.Collection;
25+
import java.util.List;
2326
import java.util.Map;
2427

2528
/**
@@ -119,4 +122,12 @@ public final Query wildcardQuery(
119122
return new MatchNoDocsQuery();
120123
}
121124
}
125+
126+
public final String getConstantValue(FetchContext context) {
127+
try {
128+
return (String) valueFetcher(context.getSearchExecutionContext(), null).fetchValues(null, 0, List.of()).get(0);
129+
} catch (IOException e) {
130+
throw new RuntimeException(e);
131+
}
132+
}
122133
}

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.HashMap;
3131
import java.util.HashSet;
3232
import java.util.LinkedHashMap;
33-
import java.util.List;
3433
import java.util.Map;
3534
import java.util.Set;
3635
import java.util.function.Function;
@@ -147,7 +146,7 @@ private FieldContext contextBuilders(
147146
sourceRequired = true;
148147
}
149148

150-
Query highlightQuery = getHighlighterQuery(context, field, fieldType);
149+
Query highlightQuery = getHighlightQuery(context, field, fieldType);
151150

152151
builders.put(
153152
fieldName,
@@ -167,33 +166,25 @@ private FieldContext contextBuilders(
167166
return new FieldContext(storedFieldsSpec, builders);
168167
}
169168

170-
private Query getHighlighterQuery(FetchContext context, SearchHighlightContext.Field field, MappedFieldType fieldType) {
171-
if (fieldType instanceof ConstantFieldType) {
172-
return getHighlighterQueryForConstantFieldType(context, fieldType);
169+
private Query getHighlightQuery(FetchContext context, SearchHighlightContext.Field field, MappedFieldType fieldType) {
170+
if (fieldType instanceof ConstantFieldType constantFieldType) {
171+
return getHighlightQueryForConstantFieldType(context, constantFieldType);
173172
}
174173
return field.fieldOptions().highlightQuery();
175174
}
176175

177-
private Query getHighlighterQueryForConstantFieldType(FetchContext context, MappedFieldType fieldType) {
176+
private Query getHighlightQueryForConstantFieldType(FetchContext context, ConstantFieldType fieldType) {
178177
if (context.query() instanceof MatchAllDocumentQueryWrapper matchAllDocumentQueryWrapper) {
179178
return new TermQuery(new Term(fieldType.name(), matchAllDocumentQueryWrapper.getPattern()));
180179
} else if (context.query() instanceof DisjunctionMaxQuery unionOfQueries) {
181180
for (Query query : unionOfQueries.getDisjuncts()) {
182181
if (query instanceof MatchAllDocumentQueryWrapper matchAllDocumentQueryWrapper) {
183-
if (Regex.simpleMatch(matchAllDocumentQueryWrapper.getPattern(), getValueForConstantFieldType(context, fieldType))) {
184-
return new TermQuery(new Term(fieldType.name(), getValueForConstantFieldType(context, fieldType)));
182+
if (Regex.simpleMatch(matchAllDocumentQueryWrapper.getPattern(), fieldType.getConstantValue(context))) {
183+
return new TermQuery(new Term(fieldType.name(), fieldType.getConstantValue(context)));
185184
}
186185
}
187186
}
188187
}
189188
return new MatchNoDocsQuery();
190189
}
191-
192-
private String getValueForConstantFieldType(FetchContext context, MappedFieldType fieldType) {
193-
try {
194-
return (String) fieldType.valueFetcher(context.getSearchExecutionContext(), null).fetchValues(null, 0, List.of()).get(0);
195-
} catch (IOException e) {
196-
throw new RuntimeException(e);
197-
}
198-
}
199190
}

0 commit comments

Comments
 (0)