1919
2020package org .elasticsearch .action .admin .indices .analyze ;
2121
22- import org .elasticsearch .action . ActionType ;
22+ import org .elasticsearch .Version ;
2323import org .elasticsearch .action .ActionRequestValidationException ;
2424import org .elasticsearch .action .ActionResponse ;
25+ import org .elasticsearch .action .ActionType ;
2526import org .elasticsearch .action .support .single .shard .SingleShardRequest ;
2627import org .elasticsearch .common .ParseField ;
2728import org .elasticsearch .common .Strings ;
@@ -292,22 +293,29 @@ public static class Response extends ActionResponse implements ToXContentObject
292293 private final List <AnalyzeToken > tokens ;
293294
294295 public Response (List <AnalyzeToken > tokens , DetailAnalyzeResponse detail ) {
296+ if (tokens == null && detail == null ) {
297+ throw new IllegalArgumentException ("Neither token nor detail set on AnalysisAction.Response" );
298+ }
295299 this .tokens = tokens ;
296300 this .detail = detail ;
297301 }
298302
299303 public Response (StreamInput in ) throws IOException {
300304 super .readFrom (in );
301- int size = in .readVInt ();
302- if (size > 0 ) {
303- tokens = new ArrayList <>(size );
304- for (int i = 0 ; i < size ; i ++) {
305- tokens .add (new AnalyzeToken (in ));
305+ if (in .getVersion ().onOrAfter (Version .V_7_3_0 )) {
306+ AnalyzeToken [] tokenArray = in .readOptionalArray (AnalyzeToken ::new , AnalyzeToken []::new );
307+ tokens = tokenArray != null ? Arrays .asList (tokenArray ) : null ;
308+ } else {
309+ int size = in .readVInt ();
310+ if (size > 0 ) {
311+ tokens = new ArrayList <>(size );
312+ for (int i = 0 ; i < size ; i ++) {
313+ tokens .add (new AnalyzeToken (in ));
314+ }
315+ } else {
316+ tokens = null ;
306317 }
307318 }
308- else {
309- tokens = null ;
310- }
311319 detail = in .readOptionalWriteable (DetailAnalyzeResponse ::new );
312320 }
313321
@@ -347,21 +355,33 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
347355 @ Override
348356 public void writeTo (StreamOutput out ) throws IOException {
349357 super .writeTo (out );
350- if (tokens != null ) {
351- out . writeVInt ( tokens . size ()) ;
352- for ( AnalyzeToken token : tokens ) {
353- token . writeTo ( out );
358+ if (out . getVersion (). onOrAfter ( Version . V_7_3_0 ) ) {
359+ AnalyzeToken [] tokenArray = null ;
360+ if ( tokens != null ) {
361+ tokenArray = tokens . toArray ( new AnalyzeToken [ 0 ] );
354362 }
363+ out .writeOptionalArray (tokenArray );
355364 } else {
356- out .writeVInt (0 );
365+ if (tokens != null ) {
366+ out .writeVInt (tokens .size ());
367+ for (AnalyzeToken token : tokens ) {
368+ token .writeTo (out );
369+ }
370+ } else {
371+ out .writeVInt (0 );
372+ }
357373 }
358374 out .writeOptionalWriteable (detail );
359375 }
360376
361377 @ Override
362378 public boolean equals (Object o ) {
363- if (this == o ) return true ;
364- if (o == null || getClass () != o .getClass ()) return false ;
379+ if (this == o ) {
380+ return true ;
381+ }
382+ if (o == null || getClass () != o .getClass ()) {
383+ return false ;
384+ }
365385 Response that = (Response ) o ;
366386 return Objects .equals (detail , that .detail ) &&
367387 Objects .equals (tokens , that .tokens );
@@ -402,8 +422,12 @@ public static class AnalyzeToken implements Writeable, ToXContentObject {
402422
403423 @ Override
404424 public boolean equals (Object o ) {
405- if (this == o ) return true ;
406- if (o == null || getClass () != o .getClass ()) return false ;
425+ if (this == o ) {
426+ return true ;
427+ }
428+ if (o == null || getClass () != o .getClass ()) {
429+ return false ;
430+ }
407431 AnalyzeToken that = (AnalyzeToken ) o ;
408432 return startOffset == that .startOffset &&
409433 endOffset == that .endOffset &&
@@ -583,8 +607,12 @@ public AnalyzeTokenList[] tokenfilters() {
583607
584608 @ Override
585609 public boolean equals (Object o ) {
586- if (this == o ) return true ;
587- if (o == null || getClass () != o .getClass ()) return false ;
610+ if (this == o ) {
611+ return true ;
612+ }
613+ if (o == null || getClass () != o .getClass ()) {
614+ return false ;
615+ }
588616 DetailAnalyzeResponse that = (DetailAnalyzeResponse ) o ;
589617 return customAnalyzer == that .customAnalyzer &&
590618 Objects .equals (analyzer , that .analyzer ) &&
@@ -670,8 +698,12 @@ public static class AnalyzeTokenList implements Writeable, ToXContentObject {
670698
671699 @ Override
672700 public boolean equals (Object o ) {
673- if (this == o ) return true ;
674- if (o == null || getClass () != o .getClass ()) return false ;
701+ if (this == o ) {
702+ return true ;
703+ }
704+ if (o == null || getClass () != o .getClass ()) {
705+ return false ;
706+ }
675707 AnalyzeTokenList that = (AnalyzeTokenList ) o ;
676708 return Objects .equals (name , that .name ) &&
677709 Arrays .equals (tokens , that .tokens );
@@ -691,16 +723,19 @@ public AnalyzeTokenList(String name, AnalyzeToken[] tokens) {
691723
692724 AnalyzeTokenList (StreamInput in ) throws IOException {
693725 name = in .readString ();
694- int size = in .readVInt ();
695- if (size > 0 ) {
696- tokens = new AnalyzeToken [size ];
697- for (int i = 0 ; i < size ; i ++) {
698- tokens [i ] = new AnalyzeToken (in );
726+ if (in .getVersion ().onOrAfter (Version .V_7_3_0 )) {
727+ tokens = in .readOptionalArray (AnalyzeToken ::new , AnalyzeToken []::new );
728+ } else {
729+ int size = in .readVInt ();
730+ if (size > 0 ) {
731+ tokens = new AnalyzeToken [size ];
732+ for (int i = 0 ; i < size ; i ++) {
733+ tokens [i ] = new AnalyzeToken (in );
734+ }
735+ } else {
736+ tokens = null ;
699737 }
700738 }
701- else {
702- tokens = null ;
703- }
704739 }
705740
706741 public String getName () {
@@ -733,13 +768,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
733768 @ Override
734769 public void writeTo (StreamOutput out ) throws IOException {
735770 out .writeString (name );
736- if (tokens != null ) {
737- out .writeVInt (tokens .length );
738- for (AnalyzeToken token : tokens ) {
739- token .writeTo (out );
740- }
771+ if (out .getVersion ().onOrAfter (Version .V_7_3_0 )) {
772+ out .writeOptionalArray (tokens );
741773 } else {
742- out .writeVInt (0 );
774+ if (tokens != null ) {
775+ out .writeVInt (tokens .length );
776+ for (AnalyzeToken token : tokens ) {
777+ token .writeTo (out );
778+ }
779+ } else {
780+ out .writeVInt (0 );
781+ }
743782 }
744783 }
745784 }
@@ -790,8 +829,12 @@ public void writeTo(StreamOutput out) throws IOException {
790829
791830 @ Override
792831 public boolean equals (Object o ) {
793- if (this == o ) return true ;
794- if (o == null || getClass () != o .getClass ()) return false ;
832+ if (this == o ) {
833+ return true ;
834+ }
835+ if (o == null || getClass () != o .getClass ()) {
836+ return false ;
837+ }
795838 CharFilteredText that = (CharFilteredText ) o ;
796839 return Objects .equals (name , that .name ) &&
797840 Arrays .equals (texts , that .texts );
0 commit comments