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 .lookup .SourceLookup ;
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
@@ -551,7 +554,26 @@ public static SearchHit createFromMap(Map<String, Object> values) {
551
554
Map <String , DocumentField > fields = get (Fields .FIELDS , values , Collections .emptyMap ());
552
555
553
556
SearchHit searchHit = new SearchHit (-1 , id , type , nestedIdentity , fields );
554
- searchHit .index = get (Fields ._INDEX , values , null );
557
+ String index = get (Fields ._INDEX , values , null );
558
+ String clusterAlias = null ;
559
+ if (index != null ) {
560
+ int indexOf = index .indexOf (RemoteClusterAware .REMOTE_CLUSTER_INDEX_SEPARATOR );
561
+ if (indexOf > 0 ) {
562
+ clusterAlias = index .substring (0 , indexOf );
563
+ index = index .substring (indexOf + 1 );
564
+ }
565
+ }
566
+ ShardId shardId = get (Fields ._SHARD , values , null );
567
+ String nodeId = get (Fields ._NODE , values , null );
568
+ if (shardId != null && nodeId != null ) {
569
+ assert shardId .getIndexName ().equals (index );
570
+ searchHit .shard (new SearchShardTarget (nodeId , shardId , clusterAlias , OriginalIndices .NONE ));
571
+ } else {
572
+ //these fields get set anyways when setting the shard target,
573
+ //but we set them explicitly when we don't have enough info to rebuild the shard target
574
+ searchHit .index = index ;
575
+ searchHit .clusterAlias = clusterAlias ;
576
+ }
555
577
searchHit .score (get (Fields ._SCORE , values , DEFAULT_SCORE ));
556
578
searchHit .version (get (Fields ._VERSION , values , -1L ));
557
579
searchHit .sortValues (get (Fields .SORT , values , SearchSortValues .EMPTY ));
@@ -561,12 +583,7 @@ public static SearchHit createFromMap(Map<String, Object> values) {
561
583
searchHit .setInnerHits (get (Fields .INNER_HITS , values , null ));
562
584
List <String > matchedQueries = get (Fields .MATCHED_QUERIES , values , null );
563
585
if (matchedQueries != null ) {
564
- searchHit .matchedQueries (matchedQueries .toArray (new String [matchedQueries .size ()]));
565
- }
566
- ShardId shardId = get (Fields ._SHARD , values , null );
567
- String nodeId = get (Fields ._NODE , values , null );
568
- if (shardId != null && nodeId != null ) {
569
- searchHit .shard (new SearchShardTarget (nodeId , shardId , null , OriginalIndices .NONE ));
586
+ searchHit .matchedQueries (matchedQueries .toArray (new String [0 ]));
570
587
}
571
588
return searchHit ;
572
589
}
@@ -842,13 +859,15 @@ public boolean equals(Object obj) {
842
859
&& Arrays .equals (matchedQueries , other .matchedQueries )
843
860
&& Objects .equals (explanation , other .explanation )
844
861
&& Objects .equals (shard , other .shard )
845
- && Objects .equals (innerHits , other .innerHits );
862
+ && Objects .equals (innerHits , other .innerHits )
863
+ && Objects .equals (index , other .index )
864
+ && Objects .equals (clusterAlias , other .clusterAlias );
846
865
}
847
866
848
867
@ Override
849
868
public int hashCode () {
850
869
return Objects .hash (id , type , nestedIdentity , version , source , fields , getHighlightFields (), Arrays .hashCode (matchedQueries ),
851
- explanation , shard , innerHits );
870
+ explanation , shard , innerHits , index , clusterAlias );
852
871
}
853
872
854
873
/**
0 commit comments