4040import org .elasticsearch .index .IndexVersions ;
4141import org .elasticsearch .index .analysis .IndexAnalyzers ;
4242import org .elasticsearch .index .analysis .NamedAnalyzer ;
43- import org .elasticsearch .index .fielddata .AbstractBinaryDocValues ;
4443import org .elasticsearch .index .fielddata .FieldDataContext ;
4544import org .elasticsearch .index .fielddata .IndexFieldData ;
4645import org .elasticsearch .index .fielddata .SortedBinaryDocValues ;
@@ -302,11 +301,17 @@ private IOFunction<LeafReaderContext, CheckedIntFunction<List<Object>, IOExcepti
302301
303302 if (parent instanceof KeywordFieldMapper .KeywordFieldType keywordParent
304303 && keywordParent .ignoreAbove ().valuesPotentiallyIgnored ()) {
305- var ifd = searchExecutionContext .getForField (parent , MappedFieldType .FielddataOperation .SEARCH );
306304 if (parent .isStored ()) {
307- return combineFieldFetchers (storedFieldFetcher (parentFieldName ), docValuesFieldFetcher (ifd ));
305+ return combineFieldFetchers (
306+ storedFieldFetcher (parentFieldName ),
307+ ignoredValuesDocValuesFieldFetcher (keywordParent .syntheticSourceFallbackFieldName ())
308+ );
308309 } else if (parent .hasDocValues ()) {
309- return docValuesFieldFetcher (ifd );
310+ var ifd = searchExecutionContext .getForField (parent , MappedFieldType .FielddataOperation .SEARCH );
311+ return combineFieldFetchers (
312+ docValuesFieldFetcher (ifd ),
313+ ignoredValuesDocValuesFieldFetcher (keywordParent .syntheticSourceFallbackFieldName ())
314+ );
310315 }
311316 }
312317
@@ -356,30 +361,31 @@ private IOFunction<LeafReaderContext, CheckedIntFunction<List<Object>, IOExcepti
356361 private IOFunction <LeafReaderContext , CheckedIntFunction <List <Object >, IOException >> docValuesFieldFetcher (IndexFieldData <?> ifd ) {
357362 return context -> {
358363 SortedBinaryDocValues indexedValuesDocValues = ifd .load (context ).getBytesValues ();
359- CustomBinaryDocValues ignoredValuesDocValues = new CustomBinaryDocValues (
360- DocValues .getBinary (context .reader (), ifd .getFieldName () + TextFamilyFieldType .FALLBACK_FIELD_NAME_SUFFIX )
361- );
362-
363- return docId -> {
364- int indexedValueCount = indexedValuesDocValues .advanceExact (docId ) ? indexedValuesDocValues .docValueCount () : 0 ;
365- int ignoredValueCount = ignoredValuesDocValues .advanceExact (docId ) ? ignoredValuesDocValues .docValueCount () : 0 ;
366- var values = new ArrayList <>(indexedValueCount + ignoredValueCount );
367-
368- // extract indexed values from doc values
369- for (int i = 0 ; i < indexedValueCount ; i ++) {
370- values .add (indexedValuesDocValues .nextValue ().utf8ToString ());
371- }
372-
373- // extract ignored values from doc values
374- for (int i = 0 ; i < ignoredValueCount ; i ++) {
375- values .add (ignoredValuesDocValues .nextValue ().utf8ToString ());
376- }
364+ return docId -> getValuesFromDocValues (indexedValuesDocValues , docId );
365+ };
366+ }
377367
378- return values ;
379- };
368+ private IOFunction <LeafReaderContext , CheckedIntFunction <List <Object >, IOException >> ignoredValuesDocValuesFieldFetcher (
369+ String fieldName
370+ ) {
371+ return context -> {
372+ CustomBinaryDocValues ignoredValuesDocValues = new CustomBinaryDocValues (DocValues .getBinary (context .reader (), fieldName ));
373+ return docId -> getValuesFromDocValues (ignoredValuesDocValues , docId );
380374 };
381375 }
382376
377+ private List <Object > getValuesFromDocValues (SortedBinaryDocValues docValues , int docId ) throws IOException {
378+ if (docValues .advanceExact (docId )) {
379+ var values = new ArrayList <>(docValues .docValueCount ());
380+ for (int i = 0 ; i < docValues .docValueCount (); i ++) {
381+ values .add (docValues .nextValue ().utf8ToString ());
382+ }
383+ return values ;
384+ } else {
385+ return List .of ();
386+ }
387+ }
388+
383389 private static IOFunction <LeafReaderContext , CheckedIntFunction <List <Object >, IOException >> storedFieldFetcher (String ... names ) {
384390 var loader = StoredFieldLoader .create (false , Set .of (names ));
385391 return context -> {
@@ -787,9 +793,9 @@ protected void writeValue(Object value, XContentBuilder b) throws IOException {
787793 }
788794
789795 /**
790- * A wrapper around {@link BinaryDocValues} that exposes some quality of life functions.
796+ * A wrapper around {@link BinaryDocValues} that exposes some quality of life functions. Note, these values are not sorted.
791797 */
792- private static class CustomBinaryDocValues extends AbstractBinaryDocValues {
798+ private static class CustomBinaryDocValues extends SortedBinaryDocValues {
793799
794800 private final BinaryDocValues binaryDocValues ;
795801 private final ByteArrayStreamInput stream ;
@@ -801,16 +807,12 @@ private static class CustomBinaryDocValues extends AbstractBinaryDocValues {
801807 this .stream = new ByteArrayStreamInput ();
802808 }
803809
810+ @ Override
804811 public BytesRef nextValue () throws IOException {
805812 // this function already knows how to decode the underlying bytes array, so no need to explicitly call VInt()
806813 return stream .readBytesRef ();
807814 }
808815
809- @ Override
810- public BytesRef binaryValue () throws IOException {
811- return binaryDocValues .binaryValue ();
812- }
813-
814816 @ Override
815817 public boolean advanceExact (int docId ) throws IOException {
816818 // if document has a value, read underlying bytes
@@ -826,6 +828,7 @@ public boolean advanceExact(int docId) throws IOException {
826828 return false ;
827829 }
828830
831+ @ Override
829832 public int docValueCount () {
830833 return docValueCount ;
831834 }
0 commit comments