3030import java .util .HashMap ;
3131import java .util .HashSet ;
3232import java .util .LinkedHashMap ;
33- import java .util .List ;
3433import java .util .Map ;
3534import java .util .Set ;
3635import java .util .function .Function ;
@@ -147,7 +146,7 @@ private FieldContext contextBuilders(
147146 sourceRequired = true ;
148147 }
149148
150- Query highlightQuery = getHighlighterQuery (context , field , fieldType );
149+ Query highlightQuery = getHighlightQuery (context , field , fieldType );
151150
152151 builders .put (
153152 fieldName ,
@@ -167,33 +166,25 @@ private FieldContext contextBuilders(
167166 return new FieldContext (storedFieldsSpec , builders );
168167 }
169168
170- private Query getHighlighterQuery (FetchContext context , SearchHighlightContext .Field field , MappedFieldType fieldType ) {
171- if (fieldType instanceof ConstantFieldType ) {
172- return getHighlighterQueryForConstantFieldType (context , fieldType );
169+ private Query getHighlightQuery (FetchContext context , SearchHighlightContext .Field field , MappedFieldType fieldType ) {
170+ if (fieldType instanceof ConstantFieldType constantFieldType ) {
171+ return getHighlightQueryForConstantFieldType (context , constantFieldType );
173172 }
174173 return field .fieldOptions ().highlightQuery ();
175174 }
176175
177- private Query getHighlighterQueryForConstantFieldType (FetchContext context , MappedFieldType fieldType ) {
176+ private Query getHighlightQueryForConstantFieldType (FetchContext context , ConstantFieldType fieldType ) {
178177 if (context .query () instanceof MatchAllDocumentQueryWrapper matchAllDocumentQueryWrapper ) {
179178 return new TermQuery (new Term (fieldType .name (), matchAllDocumentQueryWrapper .getPattern ()));
180179 } else if (context .query () instanceof DisjunctionMaxQuery unionOfQueries ) {
181180 for (Query query : unionOfQueries .getDisjuncts ()) {
182181 if (query instanceof MatchAllDocumentQueryWrapper matchAllDocumentQueryWrapper ) {
183- if (Regex .simpleMatch (matchAllDocumentQueryWrapper .getPattern (), getValueForConstantFieldType (context , fieldType ))) {
184- return new TermQuery (new Term (fieldType .name (), getValueForConstantFieldType (context , fieldType )));
182+ if (Regex .simpleMatch (matchAllDocumentQueryWrapper .getPattern (), fieldType . getConstantValue (context ))) {
183+ return new TermQuery (new Term (fieldType .name (), fieldType . getConstantValue (context )));
185184 }
186185 }
187186 }
188187 }
189188 return new MatchNoDocsQuery ();
190189 }
191-
192- private String getValueForConstantFieldType (FetchContext context , MappedFieldType fieldType ) {
193- try {
194- return (String ) fieldType .valueFetcher (context .getSearchExecutionContext (), null ).fetchValues (null , 0 , List .of ()).get (0 );
195- } catch (IOException e ) {
196- throw new RuntimeException (e );
197- }
198- }
199190}
0 commit comments