@@ -312,18 +312,15 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
312
312
} else if (type == 1 ) {
313
313
TotalHits totalHits = readTotalHits (in );
314
314
float maxScore = in .readFloat ();
315
- SortField [] fields = in .readArray (Lucene ::readSortField , SortField []::new );
316
- FieldDoc [] fieldDocs = new FieldDoc [in .readVInt ()];
317
- for (int i = 0 ; i < fieldDocs .length ; i ++) {
318
- fieldDocs [i ] = readFieldDoc (in );
319
- }
315
+ SortField [] fields = readSortFieldArray (in );
316
+ FieldDoc [] fieldDocs = in .readArray (Lucene ::readFieldDoc , FieldDoc []::new );
320
317
return new TopDocsAndMaxScore (new TopFieldDocs (totalHits , fieldDocs , fields ), maxScore );
321
318
} else if (type == 2 ) {
322
319
TotalHits totalHits = readTotalHits (in );
323
320
float maxScore = in .readFloat ();
324
321
325
322
String field = in .readString ();
326
- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
323
+ SortField [] fields = readSortFieldArray ( in );
327
324
int size = in .readVInt ();
328
325
Object [] collapseValues = new Object [size ];
329
326
FieldDoc [] fieldDocs = new FieldDoc [size ];
@@ -338,65 +335,30 @@ public static TopDocsAndMaxScore readTopDocs(StreamInput in) throws IOException
338
335
}
339
336
340
337
public static FieldDoc readFieldDoc (StreamInput in ) throws IOException {
341
- Comparable <?>[] cFields = new Comparable <?>[in .readVInt ()];
342
- for (int j = 0 ; j < cFields .length ; j ++) {
343
- byte type = in .readByte ();
344
- if (type == 0 ) {
345
- cFields [j ] = null ;
346
- } else if (type == 1 ) {
347
- cFields [j ] = in .readString ();
348
- } else if (type == 2 ) {
349
- cFields [j ] = in .readInt ();
350
- } else if (type == 3 ) {
351
- cFields [j ] = in .readLong ();
352
- } else if (type == 4 ) {
353
- cFields [j ] = in .readFloat ();
354
- } else if (type == 5 ) {
355
- cFields [j ] = in .readDouble ();
356
- } else if (type == 6 ) {
357
- cFields [j ] = in .readByte ();
358
- } else if (type == 7 ) {
359
- cFields [j ] = in .readShort ();
360
- } else if (type == 8 ) {
361
- cFields [j ] = in .readBoolean ();
362
- } else if (type == 9 ) {
363
- cFields [j ] = in .readBytesRef ();
364
- } else if (type == 10 ) {
365
- cFields [j ] = new BigInteger (in .readString ());
366
- } else {
367
- throw new IOException ("Can't match type [" + type + "]" );
368
- }
369
- }
370
- return new FieldDoc (in .readVInt (), in .readFloat (), cFields );
338
+ var sortValues = readSortValues (in );
339
+ return new FieldDoc (in .readVInt (), in .readFloat (), sortValues );
340
+ }
341
+
342
+ public static Object [] readSortValues (StreamInput in ) throws IOException {
343
+ return in .readArray (Lucene ::readSortValue , Object []::new );
371
344
}
372
345
373
346
public static Comparable <?> readSortValue (StreamInput in ) throws IOException {
374
347
byte type = in .readByte ();
375
- if (type == 0 ) {
376
- return null ;
377
- } else if (type == 1 ) {
378
- return in .readString ();
379
- } else if (type == 2 ) {
380
- return in .readInt ();
381
- } else if (type == 3 ) {
382
- return in .readLong ();
383
- } else if (type == 4 ) {
384
- return in .readFloat ();
385
- } else if (type == 5 ) {
386
- return in .readDouble ();
387
- } else if (type == 6 ) {
388
- return in .readByte ();
389
- } else if (type == 7 ) {
390
- return in .readShort ();
391
- } else if (type == 8 ) {
392
- return in .readBoolean ();
393
- } else if (type == 9 ) {
394
- return in .readBytesRef ();
395
- } else if (type == 10 ) {
396
- return new BigInteger (in .readString ());
397
- } else {
398
- throw new IOException ("Can't match type [" + type + "]" );
399
- }
348
+ return switch (type ) {
349
+ case 0 -> null ;
350
+ case 1 -> in .readString ();
351
+ case 2 -> in .readInt ();
352
+ case 3 -> in .readLong ();
353
+ case 4 -> in .readFloat ();
354
+ case 5 -> in .readDouble ();
355
+ case 6 -> in .readByte ();
356
+ case 7 -> in .readShort ();
357
+ case 8 -> in .readBoolean ();
358
+ case 9 -> in .readBytesRef ();
359
+ case 10 -> new BigInteger (in .readString ());
360
+ default -> throw new IOException ("Can't match type [" + type + "]" );
361
+ };
400
362
}
401
363
402
364
public static ScoreDoc readScoreDoc (StreamInput in ) throws IOException {
@@ -425,7 +387,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
425
387
out .writeByte ((byte ) 2 );
426
388
writeTotalHits (out , topDocs .totalHits );
427
389
out .writeString (topFieldGroups .field );
428
- out . writeArray ( Lucene :: writeSortField , topFieldGroups .fields );
390
+ writeSortFieldArray ( out , topFieldGroups .fields );
429
391
out .writeVInt (topDocs .scoreDocs .length );
430
392
for (int i = 0 ; i < topDocs .scoreDocs .length ; i ++) {
431
393
ScoreDoc doc = topFieldGroups .scoreDocs [i ];
@@ -436,7 +398,7 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
436
398
} else if (topDocs instanceof TopFieldDocs topFieldDocs ) {
437
399
out .writeByte ((byte ) 1 );
438
400
writeTotalHits (out , topDocs .totalHits );
439
- out . writeArray ( Lucene :: writeSortField , topFieldDocs .fields );
401
+ writeSortFieldArray ( out , topFieldDocs .fields );
440
402
out .writeArray ((o , doc ) -> {
441
403
writeFieldDoc (o , (FieldDoc ) doc );
442
404
o .writeVInt (doc .shardIndex );
@@ -451,6 +413,10 @@ public static void writeTopDocsIncludingShardIndex(StreamOutput out, TopDocs top
451
413
}
452
414
}
453
415
416
+ public static void writeSortFieldArray (StreamOutput out , SortField [] sortFields ) throws IOException {
417
+ out .writeArray (Lucene ::writeSortField , sortFields );
418
+ }
419
+
454
420
/**
455
421
* Read side counterpart to {@link #writeTopDocsIncludingShardIndex} and the same as {@link #readTopDocs(StreamInput)} but for the
456
422
* added shard index values that are read.
@@ -473,7 +439,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
473
439
return new TopDocs (totalHits , scoreDocs );
474
440
} else if (type == 1 ) {
475
441
TotalHits totalHits = readTotalHits (in );
476
- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
442
+ SortField [] fields = readSortFieldArray ( in );
477
443
FieldDoc [] fieldDocs = new FieldDoc [in .readVInt ()];
478
444
for (int i = 0 ; i < fieldDocs .length ; i ++) {
479
445
var fieldDoc = readFieldDoc (in );
@@ -484,7 +450,7 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
484
450
} else if (type == 2 ) {
485
451
TotalHits totalHits = readTotalHits (in );
486
452
String field = in .readString ();
487
- SortField [] fields = in . readArray ( Lucene :: readSortField , SortField []:: new );
453
+ SortField [] fields = readSortFieldArray ( in );
488
454
int size = in .readVInt ();
489
455
Object [] collapseValues = new Object [size ];
490
456
FieldDoc [] fieldDocs = new FieldDoc [size ];
@@ -500,6 +466,10 @@ public static TopDocs readTopDocsIncludingShardIndex(StreamInput in) throws IOEx
500
466
}
501
467
}
502
468
469
+ public static SortField [] readSortFieldArray (StreamInput in ) throws IOException {
470
+ return in .readArray (Lucene ::readSortField , SortField []::new );
471
+ }
472
+
503
473
public static void writeTopDocs (StreamOutput out , TopDocsAndMaxScore topDocs ) throws IOException {
504
474
if (topDocs .topDocs instanceof TopFieldGroups topFieldGroups ) {
505
475
out .writeByte ((byte ) 2 );
@@ -508,7 +478,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
508
478
out .writeFloat (topDocs .maxScore );
509
479
510
480
out .writeString (topFieldGroups .field );
511
- out . writeArray ( Lucene :: writeSortField , topFieldGroups .fields );
481
+ writeSortFieldArray ( out , topFieldGroups .fields );
512
482
513
483
out .writeVInt (topFieldGroups .scoreDocs .length );
514
484
for (int i = 0 ; i < topFieldGroups .scoreDocs .length ; i ++) {
@@ -522,7 +492,7 @@ public static void writeTopDocs(StreamOutput out, TopDocsAndMaxScore topDocs) th
522
492
writeTotalHits (out , topFieldDocs .totalHits );
523
493
out .writeFloat (topDocs .maxScore );
524
494
525
- out . writeArray ( Lucene :: writeSortField , topFieldDocs .fields );
495
+ writeSortFieldArray ( out , topFieldDocs .fields );
526
496
out .writeArray ((o , doc ) -> writeFieldDoc (o , (FieldDoc ) doc ), topFieldDocs .scoreDocs );
527
497
} else {
528
498
out .writeByte ((byte ) 0 );
@@ -597,14 +567,17 @@ public static void writeSortValue(StreamOutput out, Object field) throws IOExcep
597
567
598
568
public static void writeFieldDoc (StreamOutput out , FieldDoc fieldDoc ) throws IOException {
599
569
out .writeArray (Lucene ::writeSortValue , fieldDoc .fields );
600
- out .writeVInt (fieldDoc .doc );
601
- out .writeFloat (fieldDoc .score );
570
+ doWriteScoreDoc (out , fieldDoc );
602
571
}
603
572
604
573
public static void writeScoreDoc (StreamOutput out , ScoreDoc scoreDoc ) throws IOException {
605
574
if (scoreDoc .getClass ().equals (ScoreDoc .class ) == false ) {
606
575
throw new IllegalArgumentException ("This method can only be used to serialize a ScoreDoc, not a " + scoreDoc .getClass ());
607
576
}
577
+ doWriteScoreDoc (out , scoreDoc );
578
+ }
579
+
580
+ private static void doWriteScoreDoc (StreamOutput out , ScoreDoc scoreDoc ) throws IOException {
608
581
out .writeVInt (scoreDoc .doc );
609
582
out .writeFloat (scoreDoc .score );
610
583
}
@@ -661,17 +634,12 @@ private static SortField rewriteMergeSortField(SortField sortField) {
661
634
}
662
635
}
663
636
664
- public static void writeSortField (StreamOutput out , SortField sortField ) throws IOException {
637
+ static void writeSortField (StreamOutput out , SortField sortField ) throws IOException {
665
638
sortField = rewriteMergeSortField (sortField );
666
639
if (sortField .getClass () != SortField .class ) {
667
640
throw new IllegalArgumentException ("Cannot serialize SortField impl [" + sortField + "]" );
668
641
}
669
- if (sortField .getField () == null ) {
670
- out .writeBoolean (false );
671
- } else {
672
- out .writeBoolean (true );
673
- out .writeString (sortField .getField ());
674
- }
642
+ out .writeOptionalString (sortField .getField ());
675
643
if (sortField .getComparatorSource () != null ) {
676
644
IndexFieldData .XFieldComparatorSource comparatorSource = (IndexFieldData .XFieldComparatorSource ) sortField
677
645
.getComparatorSource ();
0 commit comments