1717import org .elasticsearch .index .query .QueryBuilder ;
1818import org .elasticsearch .index .query .Rewriteable ;
1919import org .elasticsearch .index .query .SearchExecutionContext ;
20- import org .elasticsearch .search .fetch .subphase .highlight .HighlightBuilder ;
20+ import org .elasticsearch .search .fetch .subphase .highlight .HighlightSnippetUtils ;
2121import org .elasticsearch .search .fetch .subphase .highlight .Highlighter ;
2222import org .elasticsearch .search .fetch .subphase .highlight .SearchHighlightContext ;
2323import 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