19
19
20
20
package org .elasticsearch .search ;
21
21
22
+ import java .io .IOException ;
23
+ import java .util .ArrayList ;
24
+ import java .util .Arrays ;
25
+ import java .util .Collections ;
26
+ import java .util .HashMap ;
27
+ import java .util .Iterator ;
28
+ import java .util .List ;
29
+ import java .util .Map ;
30
+ import java .util .Objects ;
31
+
22
32
import org .apache .lucene .search .Explanation ;
23
33
import org .elasticsearch .ElasticsearchParseException ;
24
34
import org .elasticsearch .action .OriginalIndices ;
51
61
import org .elasticsearch .search .suggest .completion .CompletionSuggestion ;
52
62
import org .elasticsearch .transport .RemoteClusterAware ;
53
63
54
- import java .io .IOException ;
55
- import java .util .ArrayList ;
56
- import java .util .Arrays ;
57
- import java .util .Collections ;
58
- import java .util .HashMap ;
59
- import java .util .Iterator ;
60
- import java .util .List ;
61
- import java .util .Map ;
62
- import java .util .Objects ;
63
-
64
64
import static java .util .Collections .emptyMap ;
65
65
import static java .util .Collections .singletonMap ;
66
66
import static java .util .Collections .unmodifiableMap ;
@@ -107,6 +107,9 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
107
107
@ Nullable
108
108
private SearchShardTarget shard ;
109
109
110
+ //These two fields normally get set when setting the shard target, so they hold the same values as the target thus don't get
111
+ //serialized over the wire. When parsing hits back from xcontent though, in most of the cases (whenever explanation is disabled)
112
+ //we can't rebuild the shard target object so we need to set these manually for users retrieval.
110
113
private transient String index ;
111
114
private transient String clusterAlias ;
112
115
@@ -546,7 +549,26 @@ public static SearchHit createFromMap(Map<String, Object> values) {
546
549
Map <String , DocumentField > fields = get (Fields .FIELDS , values , Collections .emptyMap ());
547
550
548
551
SearchHit searchHit = new SearchHit (-1 , id , type , nestedIdentity , fields );
549
- searchHit .index = get (Fields ._INDEX , values , null );
552
+ String index = get (Fields ._INDEX , values , null );
553
+ String clusterAlias = null ;
554
+ if (index != null ) {
555
+ int indexOf = index .indexOf (RemoteClusterAware .REMOTE_CLUSTER_INDEX_SEPARATOR );
556
+ if (indexOf > 0 ) {
557
+ clusterAlias = index .substring (0 , indexOf );
558
+ index = index .substring (indexOf + 1 );
559
+ }
560
+ }
561
+ ShardId shardId = get (Fields ._SHARD , values , null );
562
+ String nodeId = get (Fields ._NODE , values , null );
563
+ if (shardId != null && nodeId != null ) {
564
+ assert shardId .getIndexName ().equals (index );
565
+ searchHit .shard (new SearchShardTarget (nodeId , shardId , clusterAlias , OriginalIndices .NONE ));
566
+ } else {
567
+ //these fields get set anyways when setting the shard target,
568
+ //but we set them explicitly when we don't have enough info to rebuild the shard target
569
+ searchHit .index = index ;
570
+ searchHit .clusterAlias = clusterAlias ;
571
+ }
550
572
searchHit .score (get (Fields ._SCORE , values , DEFAULT_SCORE ));
551
573
searchHit .version (get (Fields ._VERSION , values , -1L ));
552
574
searchHit .sortValues (get (Fields .SORT , values , SearchSortValues .EMPTY ));
@@ -556,12 +578,7 @@ public static SearchHit createFromMap(Map<String, Object> values) {
556
578
searchHit .setInnerHits (get (Fields .INNER_HITS , values , null ));
557
579
List <String > matchedQueries = get (Fields .MATCHED_QUERIES , values , null );
558
580
if (matchedQueries != null ) {
559
- searchHit .matchedQueries (matchedQueries .toArray (new String [matchedQueries .size ()]));
560
- }
561
- ShardId shardId = get (Fields ._SHARD , values , null );
562
- String nodeId = get (Fields ._NODE , values , null );
563
- if (shardId != null && nodeId != null ) {
564
- searchHit .shard (new SearchShardTarget (nodeId , shardId , null , OriginalIndices .NONE ));
581
+ searchHit .matchedQueries (matchedQueries .toArray (new String [0 ]));
565
582
}
566
583
return searchHit ;
567
584
}
@@ -598,15 +615,12 @@ private static void declareMetaDataFields(ObjectParser<Map<String, Object>, Void
598
615
if (metadatafield .equals (Fields ._ID ) == false && metadatafield .equals (Fields ._INDEX ) == false
599
616
&& metadatafield .equals (Fields ._TYPE ) == false ) {
600
617
parser .declareField ((map , field ) -> {
601
- @ SuppressWarnings ("unchecked" )
602
- Map <String , DocumentField > fieldMap = (Map <String , DocumentField >) map .computeIfAbsent (Fields .FIELDS ,
618
+ @ SuppressWarnings ("unchecked" )
619
+ Map <String , DocumentField > fieldMap = (Map <String , DocumentField >) map .computeIfAbsent (Fields .FIELDS ,
603
620
v -> new HashMap <String , DocumentField >());
604
- fieldMap .put (field .getName (), field );
605
- }, (p , c ) -> {
606
- List <Object > values = new ArrayList <>();
607
- values .add (parseFieldsValue (p ));
608
- return new DocumentField (metadatafield , values );
609
- }, new ParseField (metadatafield ), ValueType .VALUE );
621
+ fieldMap .put (field .getName (), field );
622
+ }, (p , c ) -> new DocumentField (metadatafield , Collections .singletonList (parseFieldsValue (p ))),
623
+ new ParseField (metadatafield ), ValueType .VALUE );
610
624
}
611
625
}
612
626
}
@@ -829,13 +843,15 @@ public boolean equals(Object obj) {
829
843
&& Arrays .equals (matchedQueries , other .matchedQueries )
830
844
&& Objects .equals (explanation , other .explanation )
831
845
&& Objects .equals (shard , other .shard )
832
- && Objects .equals (innerHits , other .innerHits );
846
+ && Objects .equals (innerHits , other .innerHits )
847
+ && Objects .equals (index , other .index )
848
+ && Objects .equals (clusterAlias , other .clusterAlias );
833
849
}
834
850
835
851
@ Override
836
852
public int hashCode () {
837
853
return Objects .hash (id , type , nestedIdentity , version , source , fields , getHighlightFields (), Arrays .hashCode (matchedQueries ),
838
- explanation , shard , innerHits );
854
+ explanation , shard , innerHits , index , clusterAlias );
839
855
}
840
856
841
857
/**
@@ -953,4 +969,9 @@ public int hashCode() {
953
969
return Objects .hash (field , offset , child );
954
970
}
955
971
}
972
+
973
+ @ Override
974
+ public String toString () {
975
+ return Strings .toString (this , true , true );
976
+ }
956
977
}
0 commit comments