Skip to content

Commit b923a2e

Browse files
committed
Workaround for rewrite
1 parent 02cebe7 commit b923a2e

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ public boolean canHighlight(MappedFieldType fieldType) {
6060
@Override
6161
public HighlightField highlight(FieldHighlightContext fieldContext) throws IOException {
6262
@SuppressWarnings("unchecked")
63-
// Map<String, CustomUnifiedHighlighter> cache = (Map<String, CustomUnifiedHighlighter>) fieldContext.cache.computeIfAbsent(
64-
// UnifiedHighlighter.class.getName(),
65-
// k -> new HashMap<>()
66-
// );
6763
Map<String, CustomUnifiedHighlighter> cache = (Map<String, CustomUnifiedHighlighter>) fieldContext.cache.getOrDefault(
6864
UnifiedHighlighter.class.getName(),
6965
new HashMap<>()

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/lucene/HighlighterExpressionEvaluator.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ protected void appendMatch(BytesRefBlock.Builder builder, Scorable scorer, int d
8686
throws IOException {
8787

8888
// TODO: Can we build a custom highlighter directly here, so we don't have to rely on fetch phase classes?
89+
90+
// Create a source loader for highlighter use
91+
SourceLoader sourceLoader = searchContext.newSourceLoader(null);
92+
FetchContext fetchContext = new FetchContext(searchContext, sourceLoader);
93+
MappedFieldType fieldType = searchContext.getSearchExecutionContext().getFieldType(fieldName);
94+
SearchHit searchHit = new SearchHit(docId);
95+
Source source = Source.lazy(lazyStoredSourceLoader(leafReaderContext, docId));
96+
String defaultHighlighter = fieldType.getDefaultHighlighter();
97+
98+
Highlighter highlighter;
99+
// if (SemanticTextHighlighter.NAME.equals(defaultHighlighter)) {
100+
// highlighter = new SemanticTextHighlighter();
101+
// } else {
102+
highlighter = new DefaultHighlighter();
103+
// }
104+
89105
SearchHighlightContext.FieldOptions.Builder optionsBuilder = new SearchHighlightContext.FieldOptions.Builder();
90106
optionsBuilder.numberOfFragments(numFragments != null ? numFragments : HighlightBuilder.DEFAULT_NUMBER_OF_FRAGMENTS);
91107
optionsBuilder.fragmentCharSize(fragmentLength != null ? fragmentLength : HighlightBuilder.DEFAULT_FRAGMENT_CHAR_SIZE);
@@ -94,12 +110,6 @@ protected void appendMatch(BytesRefBlock.Builder builder, Scorable scorer, int d
94110
optionsBuilder.requireFieldMatch(false);
95111
optionsBuilder.scoreOrdered(true);
96112
SearchHighlightContext.Field field = new SearchHighlightContext.Field(fieldName, optionsBuilder.build());
97-
// Create a source loader for highlighter use
98-
SourceLoader sourceLoader = searchContext.newSourceLoader(null);
99-
FetchContext fetchContext = new FetchContext(searchContext, sourceLoader);
100-
MappedFieldType fieldType = searchContext.getSearchExecutionContext().getFieldType(fieldName);
101-
SearchHit searchHit = new SearchHit(docId);
102-
Source source = Source.lazy(lazyStoredSourceLoader(leafReaderContext, docId));
103113

104114
FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext(searchHit, leafReaderContext, docId, Map.of(), source, null);
105115
FieldHighlightContext highlightContext = new FieldHighlightContext(
@@ -111,18 +121,19 @@ protected void appendMatch(BytesRefBlock.Builder builder, Scorable scorer, int d
111121
query,
112122
new HashMap<>()
113123
);
114-
Highlighter highlighter = new DefaultHighlighter();
115124
HighlightField highlight = highlighter.highlight(highlightContext);
116125

117-
boolean multivalued = highlight.fragments().length > 1;
118-
if (multivalued) {
119-
builder.beginPositionEntry();
120-
}
121-
for (Text highlightText : highlight.fragments()) {
122-
builder.appendBytesRef(new BytesRef(highlightText.bytes().bytes()));
123-
}
124-
if (multivalued) {
125-
builder.endPositionEntry();
126+
if (highlight != null) {
127+
boolean multivalued = highlight.fragments().length > 1;
128+
if (multivalued) {
129+
builder.beginPositionEntry();
130+
}
131+
for (Text highlightText : highlight.fragments()) {
132+
builder.appendBytesRef(new BytesRef(highlightText.bytes().bytes()));
133+
}
134+
if (multivalued) {
135+
builder.endPositionEntry();
136+
}
126137
}
127138
}
128139

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/QueryBuilderResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public FunctionsRewritable rewrite(QueryRewriteContext ctx) throws IOException {
9090
? translationAware.asQuery(LucenePushdownPredicates.DEFAULT, TranslatorHandler.TRANSLATOR_HANDLER).toQueryBuilder()
9191
: builder;
9292
try {
93-
builder = builder.rewrite(ctx);
93+
// builder = builder.rewrite(ctx);
94+
builder = Rewriteable.rewrite(builder, ctx);
9495
} catch (IOException e) {
9596
exceptionHolder.setIfAbsent(e);
9697
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/ExtractSnippets.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
1616
import org.elasticsearch.index.query.MatchQueryBuilder;
1717
import org.elasticsearch.index.query.QueryBuilder;
18+
import org.elasticsearch.index.query.Rewriteable;
1819
import org.elasticsearch.index.query.SearchExecutionContext;
1920
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
2021
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext;
@@ -217,7 +218,9 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
217218
// TODO: Reduce duplication between this method and TextSimilarityRerankingRankFeaturePhaseRankShardContext#prepareForFetch
218219
HighlightBuilder highlightBuilder = new HighlightBuilder();
219220
if (queryBuilder != null) {
220-
highlightBuilder.highlightQuery(queryBuilder);
221+
// TODO validate this works and determine why this is not working in query builder resolver
222+
QueryBuilder rewritten = Rewriteable.rewrite(queryBuilder, searchExecutionContext);
223+
highlightBuilder.highlightQuery(rewritten);
221224
}
222225
// Stripping pre/post tags as they're not useful for snippet creation
223226
highlightBuilder.field(field.sourceText()).preTags("").postTags("");

0 commit comments

Comments
 (0)