34
34
35
35
import java .io .IOException ;
36
36
import java .util .Collections ;
37
+ import java .util .Map ;
37
38
38
39
import static org .mockito .Mockito .mock ;
39
40
import static org .mockito .Mockito .when ;
@@ -78,6 +79,29 @@ public void testMultipleFiltering() throws IOException {
78
79
assertEquals (Collections .singletonMap ("field" ,"value" ), hitContext .hit ().getSourceAsMap ());
79
80
}
80
81
82
+ public void testNestedSource () throws IOException {
83
+ Map <String , Object > expectedNested = Collections .singletonMap ("nested2" , Collections .singletonMap ("field" , "value0" ));
84
+ XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
85
+ .field ("field" , "value" )
86
+ .field ("field2" , "value2" )
87
+ .field ("nested1" , expectedNested )
88
+ .endObject ();
89
+ FetchSubPhase .HitContext hitContext = hitExecuteMultiple (source , true , null , null ,
90
+ new SearchHit .NestedIdentity ("nested1" , 0 ,null ));
91
+ assertEquals (expectedNested , hitContext .hit ().getSourceAsMap ());
92
+ hitContext = hitExecuteMultiple (source , true , new String []{"invalid" }, null ,
93
+ new SearchHit .NestedIdentity ("nested1" , 0 ,null ));
94
+ assertEquals (Collections .emptyMap (), hitContext .hit ().getSourceAsMap ());
95
+
96
+ hitContext = hitExecuteMultiple (source , true , null , null ,
97
+ new SearchHit .NestedIdentity ("nested1" , 0 , new SearchHit .NestedIdentity ("nested2" , 0 , null )));
98
+ assertEquals (Collections .singletonMap ("field" , "value0" ), hitContext .hit ().getSourceAsMap ());
99
+
100
+ hitContext = hitExecuteMultiple (source , true , new String []{"invalid" }, null ,
101
+ new SearchHit .NestedIdentity ("nested1" , 0 , new SearchHit .NestedIdentity ("nested2" , 0 , null )));
102
+ assertEquals (Collections .emptyMap (), hitContext .hit ().getSourceAsMap ());
103
+ }
104
+
81
105
public void testSourceDisabled () throws IOException {
82
106
FetchSubPhase .HitContext hitContext = hitExecute (null , true , null , null );
83
107
assertNull (hitContext .hit ().getSourceAsMap ());
@@ -96,17 +120,29 @@ public void testSourceDisabled() throws IOException {
96
120
}
97
121
98
122
private FetchSubPhase .HitContext hitExecute (XContentBuilder source , boolean fetchSource , String include , String exclude ) {
123
+ return hitExecute (source , fetchSource , include , exclude , null );
124
+ }
125
+
126
+
127
+ private FetchSubPhase .HitContext hitExecute (XContentBuilder source , boolean fetchSource , String include , String exclude ,
128
+ SearchHit .NestedIdentity nestedIdentity ) {
99
129
return hitExecuteMultiple (source , fetchSource ,
100
130
include == null ? Strings .EMPTY_ARRAY : new String []{include },
101
- exclude == null ? Strings .EMPTY_ARRAY : new String []{exclude });
131
+ exclude == null ? Strings .EMPTY_ARRAY : new String []{exclude }, nestedIdentity );
102
132
}
103
133
104
134
private FetchSubPhase .HitContext hitExecuteMultiple (XContentBuilder source , boolean fetchSource , String [] includes , String [] excludes ) {
135
+ return hitExecuteMultiple (source , fetchSource , includes , excludes , null );
136
+ }
137
+
138
+ private FetchSubPhase .HitContext hitExecuteMultiple (XContentBuilder source , boolean fetchSource , String [] includes , String [] excludes ,
139
+ SearchHit .NestedIdentity nestedIdentity ) {
105
140
FetchSourceContext fetchSourceContext = new FetchSourceContext (fetchSource , includes , excludes );
106
141
SearchContext searchContext = new FetchSourceSubPhaseTestSearchContext (fetchSourceContext ,
107
142
source == null ? null : BytesReference .bytes (source ));
108
143
FetchSubPhase .HitContext hitContext = new FetchSubPhase .HitContext ();
109
- hitContext .reset (new SearchHit (1 , null , null , null ), null , 1 , null );
144
+ final SearchHit searchHit = new SearchHit (1 , null , null , nestedIdentity , null );
145
+ hitContext .reset (searchHit , null , 1 , null );
110
146
FetchSourceSubPhase phase = new FetchSourceSubPhase ();
111
147
phase .hitExecute (searchContext , hitContext );
112
148
return hitContext ;
0 commit comments