@@ -313,18 +313,15 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
313313 } else if (type == 1 ) {
314314 TotalHits totalHits = readTotalHits (in );
315315 float maxScore = in .readFloat ();
316- SortField [] fields = in .readArray (Lucene ::readSortField , SortField []::new );
317- FieldDoc [] fieldDocs = new FieldDoc [in .readVInt ()];
318- for (int i = 0 ; i < fieldDocs .length ; i ++) {
319- fieldDocs [i ] = readFieldDoc (in );
320- }
316+ SortField [] fields = readSortFieldArray (in );
317+ FieldDoc [] fieldDocs = in .readArray (Lucene ::readFieldDoc , FieldDoc []::new );
321318 return new TopDocsAndMaxScore (new TopFieldDocs (totalHits , fieldDocs , fields ), maxScore );
322319 } else if (type == 2 ) {
323320 TotalHits totalHits = readTotalHits (in );
324321 float maxScore = in .readFloat ();
325322
326323 String field = in .readString ();
327- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
324+ SortField [] fields = readSortFieldArray ( in );
328325 int size = in .readVInt ();
329326 Object [] collapseValues = new Object [size ];
330327 FieldDoc [] fieldDocs = new FieldDoc [size ];
@@ -339,65 +336,30 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
339336 }
340337
341338 public static FieldDoc readFieldDoc (StreamInput in ) throws IOException {
342- Comparable <?>[] cFields = new Comparable <?>[in .readVInt ()];
343- for (int j = 0 ; j < cFields .length ; j ++) {
344- byte type = in .readByte ();
345- if (type == 0 ) {
346- cFields [j ] = null ;
347- } else if (type == 1 ) {
348- cFields [j ] = in .readString ();
349- } else if (type == 2 ) {
350- cFields [j ] = in .readInt ();
351- } else if (type == 3 ) {
352- cFields [j ] = in .readLong ();
353- } else if (type == 4 ) {
354- cFields [j ] = in .readFloat ();
355- } else if (type == 5 ) {
356- cFields [j ] = in .readDouble ();
357- } else if (type == 6 ) {
358- cFields [j ] = in .readByte ();
359- } else if (type == 7 ) {
360- cFields [j ] = in .readShort ();
361- } else if (type == 8 ) {
362- cFields [j ] = in .readBoolean ();
363- } else if (type == 9 ) {
364- cFields [j ] = in .readBytesRef ();
365- } else if (type == 10 ) {
366- cFields [j ] = new BigInteger (in .readString ());
367- } else {
368- throw new IOException ("Can't match type [" + type + "]" );
369- }
370- }
371- return new FieldDoc (in .readVInt (), in .readFloat (), cFields );
339+ var sortValues = readSortValues (in );
340+ return new FieldDoc (in .readVInt (), in .readFloat (), sortValues );
341+ }
342+
343+ public static Object [] readSortValues (StreamInput in ) throws IOException {
344+ return in .readArray (Lucene ::readSortValue , Object []::new );
372345 }
373346
374347 public static Comparable <?> readSortValue (StreamInput in ) throws IOException {
375348 byte type = in .readByte ();
376- if (type == 0 ) {
377- return null ;
378- } else if (type == 1 ) {
379- return in .readString ();
380- } else if (type == 2 ) {
381- return in .readInt ();
382- } else if (type == 3 ) {
383- return in .readLong ();
384- } else if (type == 4 ) {
385- return in .readFloat ();
386- } else if (type == 5 ) {
387- return in .readDouble ();
388- } else if (type == 6 ) {
389- return in .readByte ();
390- } else if (type == 7 ) {
391- return in .readShort ();
392- } else if (type == 8 ) {
393- return in .readBoolean ();
394- } else if (type == 9 ) {
395- return in .readBytesRef ();
396- } else if (type == 10 ) {
397- return new BigInteger (in .readString ());
398- } else {
399- throw new IOException ("Can't match type [" + type + "]" );
400- }
349+ return switch (type ) {
350+ case 0 -> null ;
351+ case 1 -> in .readString ();
352+ case 2 -> in .readInt ();
353+ case 3 -> in .readLong ();
354+ case 4 -> in .readFloat ();
355+ case 5 -> in .readDouble ();
356+ case 6 -> in .readByte ();
357+ case 7 -> in .readShort ();
358+ case 8 -> in .readBoolean ();
359+ case 9 -> in .readBytesRef ();
360+ case 10 -> new BigInteger (in .readString ());
361+ default -> throw new IOException ("Can't match type [" + type + "]" );
362+ };
401363 }
402364
403365 public static ScoreDoc readScoreDoc (StreamInput in ) throws IOException {
@@ -426,7 +388,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
426388 out .writeByte ((byte ) 2 );
427389 writeTotalHits (out , topDocs .totalHits );
428390 out .writeString (topFieldGroups .field );
429- out . writeArray ( Lucene :: writeSortField , topFieldGroups .fields );
391+ writeSortFieldArray ( out , topFieldGroups .fields );
430392 out .writeVInt (topDocs .scoreDocs .length );
431393 for (int i = 0 ; i < topDocs .scoreDocs .length ; i ++) {
432394 ScoreDoc doc = topFieldGroups .scoreDocs [i ];
@@ -437,7 +399,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
437399 } else if (topDocs instanceof TopFieldDocs topFieldDocs ) {
438400 out .writeByte ((byte ) 1 );
439401 writeTotalHits (out , topDocs .totalHits );
440- out . writeArray ( Lucene :: writeSortField , topFieldDocs .fields );
402+ writeSortFieldArray ( out , topFieldDocs .fields );
441403 out .writeArray ((o , doc ) -> {
442404 writeFieldDoc (o , (FieldDoc ) doc );
443405 o .writeVInt (doc .shardIndex );
@@ -452,6 +414,10 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
452414 }
453415 }
454416
417+ public static void writeSortFieldArray (StreamOutput out , SortField [] sortFields ) throws IOException {
418+ out .writeArray (Lucene ::writeSortField , sortFields );
419+ }
420+
455421 /**
456422 * Read side counterpart to {@link #writeTopDocsIncludingShardIndex} and the same as {@link #readTopDocs(StreamInput)} but for the
457423 * added shard index values that are read.
@@ -474,7 +440,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
474440 return new TopDocs (totalHits , scoreDocs );
475441 } else if (type == 1 ) {
476442 TotalHits totalHits = readTotalHits (in );
477- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
443+ SortField [] fields = readSortFieldArray ( in );
478444 FieldDoc [] fieldDocs = new FieldDoc [in .readVInt ()];
479445 for (int i = 0 ; i < fieldDocs .length ; i ++) {
480446 var fieldDoc = readFieldDoc (in );
@@ -485,7 +451,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
485451 } else if (type == 2 ) {
486452 TotalHits totalHits = readTotalHits (in );
487453 String field = in .readString ();
488- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
454+ SortField [] fields = readSortFieldArray ( in );
489455 int size = in .readVInt ();
490456 Object [] collapseValues = new Object [size ];
491457 FieldDoc [] fieldDocs = new FieldDoc [size ];
@@ -501,6 +467,10 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
501467 }
502468 }
503469
470+ public static SortField [] readSortFieldArray (StreamInput in ) throws IOException {
471+ return in .readArray (Lucene ::readSortField , SortField []::new );
472+ }
473+
504474 public static void writeTopDocs (StreamOutput out , TopDocsAndMaxScore topDocs ) throws IOException {
505475 if (topDocs .topDocs instanceof TopFieldGroups topFieldGroups ) {
506476 out .writeByte ((byte ) 2 );
@@ -509,7 +479,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
509479 out .writeFloat (topDocs .maxScore );
510480
511481 out .writeString (topFieldGroups .field );
512- out . writeArray ( Lucene :: writeSortField , topFieldGroups .fields );
482+ writeSortFieldArray ( out , topFieldGroups .fields );
513483
514484 out .writeVInt (topFieldGroups .scoreDocs .length );
515485 for (int i = 0 ; i < topFieldGroups .scoreDocs .length ; i ++) {
@@ -523,7 +493,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
523493 writeTotalHits (out , topFieldDocs .totalHits );
524494 out .writeFloat (topDocs .maxScore );
525495
526- out . writeArray ( Lucene :: writeSortField , topFieldDocs .fields );
496+ writeSortFieldArray ( out , topFieldDocs .fields );
527497 out .writeArray ((o , doc ) -> writeFieldDoc (o , (FieldDoc ) doc ), topFieldDocs .scoreDocs );
528498 } else {
529499 out .writeByte ((byte ) 0 );
@@ -598,14 +568,17 @@ public static void writeSortValue(StreamOutput out, Object field) throws IOExcep
598568
599569 public static void writeFieldDoc (StreamOutput out , FieldDoc fieldDoc ) throws IOException {
600570 out .writeArray (Lucene ::writeSortValue , fieldDoc .fields );
601- out .writeVInt (fieldDoc .doc );
602- out .writeFloat (fieldDoc .score );
571+ doWriteScoreDoc (out , fieldDoc );
603572 }
604573
605574 public static void writeScoreDoc (StreamOutput out , ScoreDoc scoreDoc ) throws IOException {
606575 if (scoreDoc .getClass ().equals (ScoreDoc .class ) == false ) {
607576 throw new IllegalArgumentException ("This method can only be used to serialize a ScoreDoc, not a " + scoreDoc .getClass ());
608577 }
578+ doWriteScoreDoc (out , scoreDoc );
579+ }
580+
581+ private static void doWriteScoreDoc (StreamOutput out , ScoreDoc scoreDoc ) throws IOException {
609582 out .writeVInt (scoreDoc .doc );
610583 out .writeFloat (scoreDoc .score );
611584 }
@@ -662,17 +635,12 @@ private static SortField rewriteMergeSortField(SortField sortField) {
662635 }
663636 }
664637
665- public static void writeSortField (StreamOutput out , SortField sortField ) throws IOException {
638+ static void writeSortField (StreamOutput out , SortField sortField ) throws IOException {
666639 sortField = rewriteMergeSortField (sortField );
667640 if (sortField .getClass () != SortField .class ) {
668641 throw new IllegalArgumentException ("Cannot serialize SortField impl [" + sortField + "]" );
669642 }
670- if (sortField .getField () == null ) {
671- out .writeBoolean (false );
672- } else {
673- out .writeBoolean (true );
674- out .writeString (sortField .getField ());
675- }
643+ out .writeOptionalString (sortField .getField ());
676644 if (sortField .getComparatorSource () != null ) {
677645 IndexFieldData .XFieldComparatorSource comparatorSource = (IndexFieldData .XFieldComparatorSource ) sortField
678646 .getComparatorSource ();
0 commit comments