32
32
33
33
package org .opensearch .lucene .search .uhighlight ;
34
34
35
- import org .apache .lucene .analysis .Analyzer ;
36
35
import org .apache .lucene .index .LeafReader ;
37
36
import org .apache .lucene .index .Term ;
38
37
import org .apache .lucene .queries .spans .SpanMultiTermQueryWrapper ;
39
38
import org .apache .lucene .queries .spans .SpanNearQuery ;
40
39
import org .apache .lucene .queries .spans .SpanOrQuery ;
41
40
import org .apache .lucene .queries .spans .SpanQuery ;
42
41
import org .apache .lucene .queries .spans .SpanTermQuery ;
43
- import org .apache .lucene .search .IndexSearcher ;
44
42
import org .apache .lucene .search .PrefixQuery ;
45
43
import org .apache .lucene .search .Query ;
44
+ import org .apache .lucene .search .uhighlight .FieldHighlighter ;
46
45
import org .apache .lucene .search .uhighlight .FieldOffsetStrategy ;
47
- import org .apache .lucene .search .uhighlight .LabelledCharArrayMatcher ;
48
46
import org .apache .lucene .search .uhighlight .NoOpOffsetStrategy ;
47
+ import org .apache .lucene .search .uhighlight .Passage ;
49
48
import org .apache .lucene .search .uhighlight .PassageFormatter ;
50
- import org .apache .lucene .search .uhighlight .PhraseHelper ;
51
- import org .apache .lucene .search .uhighlight .SplittingBreakIterator ;
52
- import org .apache .lucene .search .uhighlight .UHComponents ;
49
+ import org .apache .lucene .search .uhighlight .PassageScorer ;
53
50
import org .apache .lucene .search .uhighlight .UnifiedHighlighter ;
54
- import org .apache .lucene .util .BytesRef ;
55
51
import org .opensearch .common .CheckedSupplier ;
56
52
import org .opensearch .common .Nullable ;
57
53
import org .opensearch .common .lucene .search .MultiPhrasePrefixQuery ;
61
57
import java .text .BreakIterator ;
62
58
import java .util .Collection ;
63
59
import java .util .Collections ;
60
+ import java .util .Comparator ;
64
61
import java .util .Locale ;
65
62
import java .util .Set ;
66
- import java .util .function .Predicate ;
67
63
68
64
/**
69
65
* Subclass of the {@link UnifiedHighlighter} that works for a single field in a single document.
@@ -91,7 +87,7 @@ public class CustomUnifiedHighlighter extends UnifiedHighlighter {
91
87
/**
92
88
* Creates a new instance of {@link CustomUnifiedHighlighter}
93
89
*
94
- * @param analyzer the analyzer used for the field at index time, used for multi term queries internally.
90
+ * @param builder the unified highlighter builder
95
91
* @param offsetSource the {@link OffsetSource} to used for offsets retrieval.
96
92
* @param passageFormatter our own {@link CustomPassageFormatter}
97
93
* which generates snippets in forms of {@link Snippet} objects.
@@ -104,14 +100,12 @@ public class CustomUnifiedHighlighter extends UnifiedHighlighter {
104
100
* @param query the query we're highlighting
105
101
* @param noMatchSize The size of the text that should be returned when no highlighting can be performed.
106
102
* @param maxPassages the maximum number of passes to highlight
107
- * @param fieldMatcher decides which terms should be highlighted
108
103
* @param maxAnalyzedOffset if the field is more than this long we'll refuse to use the ANALYZED
109
104
* offset source for it because it'd be super slow
110
105
* @param fieldMaxAnalyzedOffset this is used to limit the length of input that will be ANALYZED, this allows bigger fields to be partially highligthed
111
106
*/
112
107
public CustomUnifiedHighlighter (
113
- IndexSearcher searcher ,
114
- Analyzer analyzer ,
108
+ UnifiedHighlighter .Builder builder ,
115
109
OffsetSource offsetSource ,
116
110
PassageFormatter passageFormatter ,
117
111
@ Nullable Locale breakIteratorLocale ,
@@ -121,21 +115,19 @@ public CustomUnifiedHighlighter(
121
115
Query query ,
122
116
int noMatchSize ,
123
117
int maxPassages ,
124
- Predicate <String > fieldMatcher ,
125
118
int maxAnalyzedOffset ,
126
119
Integer fieldMaxAnalyzedOffset
127
120
) throws IOException {
128
- super (searcher , analyzer );
121
+ super (builder );
129
122
this .offsetSource = offsetSource ;
130
123
this .breakIterator = breakIterator ;
131
124
this .breakIteratorLocale = breakIteratorLocale == null ? Locale .ROOT : breakIteratorLocale ;
132
125
this .passageFormatter = passageFormatter ;
133
126
this .index = index ;
134
127
this .field = field ;
135
128
this .noMatchSize = noMatchSize ;
136
- this .setFieldMatcher (fieldMatcher );
137
129
this .maxAnalyzedOffset = maxAnalyzedOffset ;
138
- fieldHighlighter = getFieldHighlighter (field , query , extractTerms (query ), maxPassages );
130
+ fieldHighlighter = ( CustomFieldHighlighter ) getFieldHighlighter (field , query , extractTerms (query ), maxPassages );
139
131
this .fieldMaxAnalyzedOffset = fieldMaxAnalyzedOffset ;
140
132
}
141
133
@@ -203,26 +195,27 @@ protected PassageFormatter getFormatter(String field) {
203
195
}
204
196
205
197
@ Override
206
- protected CustomFieldHighlighter getFieldHighlighter ( String field , Query query , Set < Term > allTerms , int maxPassages ) {
207
- Predicate < String > fieldMatcher = getFieldMatcher ( field );
208
- BytesRef [] terms = filterExtractedTerms ( fieldMatcher , allTerms );
209
- Set < HighlightFlag > highlightFlags = getFlags ( field );
210
- PhraseHelper phraseHelper = getPhraseHelper ( field , query , highlightFlags );
211
- LabelledCharArrayMatcher [] automata = getAutomata ( field , query , highlightFlags );
212
- UHComponents components = new UHComponents ( field , fieldMatcher , query , terms , phraseHelper , automata , false , highlightFlags );
213
- OffsetSource offsetSource = getOptimizedOffsetSource ( components );
214
- BreakIterator breakIterator = new SplittingBreakIterator ( getBreakIterator ( field ), UnifiedHighlighter . MULTIVAL_SEP_CHAR );
215
- FieldOffsetStrategy strategy = getOffsetStrategy ( offsetSource , components );
198
+ protected FieldHighlighter newFieldHighlighter (
199
+ String field ,
200
+ FieldOffsetStrategy fieldOffsetStrategy ,
201
+ BreakIterator breakIterator ,
202
+ PassageScorer passageScorer ,
203
+ int maxPassages ,
204
+ int maxNoHighlightPassages ,
205
+ PassageFormatter passageFormatter ,
206
+ Comparator < Passage > passageSortComparator
207
+ ) {
216
208
return new CustomFieldHighlighter (
217
209
field ,
218
- strategy ,
210
+ fieldOffsetStrategy ,
219
211
breakIteratorLocale ,
220
212
breakIterator ,
221
213
getScorer (field ),
222
214
maxPassages ,
223
215
(noMatchSize > 0 ? 1 : 0 ),
224
216
getFormatter (field ),
225
- noMatchSize
217
+ noMatchSize ,
218
+ passageSortComparator
226
219
);
227
220
}
228
221
0 commit comments