Skip to content

Commit ccda43d

Browse files
committed
Clean up duplication when creating highlighter
1 parent bd369f7 commit ccda43d

File tree

1 file changed

+15
-22
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string

1 file changed

+15
-22
lines changed

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.index.query.QueryBuilder;
1818
import org.elasticsearch.index.query.Rewriteable;
1919
import org.elasticsearch.index.query.SearchExecutionContext;
20-
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
20+
import org.elasticsearch.search.fetch.subphase.highlight.HighlightSnippetUtils;
2121
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
2222
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext;
2323
import org.elasticsearch.search.internal.SearchContext;
@@ -68,10 +68,9 @@ public class ExtractSnippets extends EsqlScalarFunction implements OptionalArgum
6868
);
6969

7070
private static final int DEFAULT_NUM_SNIPPETS = 1;
71-
// TODO: This default should be in line with the text similarity reranker. Set artificially low here for POC purposes.
71+
// TODO: Determine good default, set artificially low for POC purposes
7272
private static final int DEFAULT_SNIPPET_LENGTH = 10;
7373

74-
// TODO: better names?
7574
private final Expression field, str, numSnippets, snippetLength;
7675
private final QueryBuilder queryBuilder;
7776

@@ -206,8 +205,8 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
206205
List<EsPhysicalOperationProviders.ShardContext> shardContexts = toEvaluator.shardContexts();
207206
LuceneQueryEvaluator.ShardConfig[] shardConfigs = new LuceneQueryEvaluator.ShardConfig[shardContexts.size()];
208207

209-
Integer numSnippets = this.numSnippets == null ? DEFAULT_NUM_SNIPPETS : (Integer) this.numSnippets.fold(FoldContext.small());
210-
Integer snippedSize = this.snippetLength == null ? DEFAULT_SNIPPET_LENGTH : (Integer) this.snippetLength.fold(FoldContext.small());
208+
int numSnippets = this.numSnippets == null ? DEFAULT_NUM_SNIPPETS : (Integer) this.numSnippets.fold(FoldContext.small());
209+
int snippetSize = this.snippetLength == null ? DEFAULT_SNIPPET_LENGTH : (Integer) this.snippetLength.fold(FoldContext.small());
211210

212211
int i = 0;
213212
for (EsPhysicalOperationProviders.ShardContext shardContext : shardContexts) {
@@ -218,22 +217,16 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
218217
}
219218

220219
try {
221-
// TODO: Reduce duplication between this method and TextSimilarityRerankingRankFeaturePhaseRankShardContext#prepareForFetch
222-
HighlightBuilder highlightBuilder = new HighlightBuilder();
223-
if (queryBuilder != null) {
224-
// TODO: Ideally we'd only need to rewrite in the QueryBuilderResolver, but we need semantic rewrites to happen
225-
// on both coordinator and data nodes.
226-
QueryBuilder rewritten = Rewriteable.rewrite(queryBuilder, searchExecutionContext);
227-
highlightBuilder.highlightQuery(rewritten);
228-
}
229-
highlightBuilder.field(fieldName()).preTags("").postTags("");
230-
highlightBuilder.order(HighlightBuilder.Order.SCORE);
231-
232-
highlightBuilder.numOfFragments(numSnippets);
233-
highlightBuilder.fragmentSize(snippedSize);
234-
highlightBuilder.noMatchSize(snippedSize);
235-
236-
SearchHighlightContext highlightContext = highlightBuilder.build(searchExecutionContext);
220+
// We need to call rewrite here, to ensure we rewrite on both coordinator and data nodes.
221+
assert queryBuilder != null : "ExtractSnippets missing required state";
222+
QueryBuilder rewritten = Rewriteable.rewrite(queryBuilder, searchExecutionContext);
223+
SearchHighlightContext highlightContext = HighlightSnippetUtils.buildSearchHighlightContextForSnippets(
224+
searchExecutionContext,
225+
fieldName(),
226+
numSnippets,
227+
snippetSize,
228+
rewritten
229+
);
237230
searchContext.highlight(highlightContext);
238231

239232
} catch (IOException e) {
@@ -260,7 +253,7 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
260253
shardConfigs,
261254
fieldName(),
262255
numSnippets,
263-
snippedSize,
256+
snippetSize,
264257
firstSearchContext,
265258
highlighters
266259
);

0 commit comments

Comments
 (0)