-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[WIP] Support extract_snippets function in ES|QL #132549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 45 commits
ee56018
eb0a876
8c0f312
86dc82a
4f4f157
d68c2e8
0571100
9fe7654
8adea56
6be55b4
60e3ce6
b6fb4f3
f6a8079
1ca0b58
34c10f5
02cebe7
b923a2e
5b9347c
44b1bc4
82412d8
1bc3d16
d4ba21d
932864a
632df21
838b054
0b0487e
eee88be
77b44d5
5ab3c56
a6a0f11
9c7609c
4a37634
675e78b
d5c9d91
bd369f7
ccda43d
35120e6
de46fef
ff3f3c1
5f20480
ae92c83
48c2825
80d1056
ec3ac7a
694bf6a
0ef8fce
e15f824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.fetch.subphase.highlight; | ||
|
|
||
| import org.apache.lucene.search.Query; | ||
| import org.elasticsearch.index.query.QueryBuilder; | ||
| import org.elasticsearch.index.query.SearchExecutionContext; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Utility class for building highlighting queries for the purpose of extracting snippets. | ||
| */ | ||
| public class HighlightSnippetUtils { | ||
|
|
||
| public static SearchHighlightContext buildSearchHighlightContextForSnippets( | ||
| SearchExecutionContext searchExecutionContext, | ||
| String field, | ||
| int numSnippets, | ||
| int snippetCharLength, | ||
| QueryBuilder queryBuilder | ||
| ) throws IOException { | ||
| SearchHighlightContext.Field highlightField = buildFieldHighlightContextForSnippets( | ||
| searchExecutionContext, | ||
| field, | ||
| numSnippets, | ||
| snippetCharLength, | ||
| queryBuilder.toQuery(searchExecutionContext) | ||
| ); | ||
| return new SearchHighlightContext(List.of(highlightField)); | ||
| } | ||
|
|
||
| public static SearchHighlightContext.Field buildFieldHighlightContextForSnippets( | ||
| SearchExecutionContext searchExecutionContext, | ||
| String fieldName, | ||
| int numSnippets, | ||
| int snippetCharLength, | ||
| Query query | ||
| ) { | ||
| SearchHighlightContext.FieldOptions.Builder optionsBuilder = new SearchHighlightContext.FieldOptions.Builder(); | ||
| optionsBuilder.numberOfFragments(numSnippets); | ||
| optionsBuilder.fragmentCharSize(snippetCharLength); | ||
| optionsBuilder.noMatchSize(snippetCharLength); | ||
| optionsBuilder.preTags(new String[] { "" }); | ||
| optionsBuilder.postTags(new String[] { "" }); | ||
| optionsBuilder.requireFieldMatch(false); | ||
| optionsBuilder.scoreOrdered(true); | ||
| optionsBuilder.highlightQuery(query); | ||
| return new SearchHighlightContext.Field(fieldName, optionsBuilder.build()); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's tough that the highlighter code is so deeply ingrained into the fetch phase 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - this was the best/cleanest solution that I could come up with, if you have better suggestions I'd be happy to talk about them!