19
19
20
20
package org .elasticsearch .search .fetch .subphase ;
21
21
22
- import org .elasticsearch .common .bytes .BytesReference ;
22
+ import org .elasticsearch .action .search .SearchPhaseExecutionException ;
23
+ import org .elasticsearch .action .search .SearchRequestBuilder ;
24
+ import org .elasticsearch .action .search .SearchResponse ;
25
+ import org .elasticsearch .action .support .WriteRequest ;
23
26
import org .elasticsearch .common .document .DocumentField ;
24
- import org .elasticsearch .common .settings .Settings ;
25
27
import org .elasticsearch .common .xcontent .XContentBuilder ;
26
28
import org .elasticsearch .common .xcontent .XContentFactory ;
27
- import org .elasticsearch .index .IndexService ;
28
29
import org .elasticsearch .search .SearchHit ;
29
- import org .elasticsearch .search .fetch .FetchSubPhase ;
30
- import org .elasticsearch .search .internal .SearchContext ;
31
- import org .elasticsearch .search .lookup .SearchLookup ;
32
30
import org .elasticsearch .test .ESSingleNodeTestCase ;
33
- import org .elasticsearch .test .TestSearchContext ;
34
31
import org .junit .Before ;
35
32
36
33
import java .io .IOException ;
37
- import java .util .List ;
38
34
import java .util .Map ;
39
35
40
36
import static org .hamcrest .Matchers .containsString ;
43
39
44
40
public class FetchFieldsPhaseTests extends ESSingleNodeTestCase {
45
41
46
- private IndexService indexService ;
47
-
48
42
@ Before
49
- public void setupIndex () throws IOException {
43
+ public void createIndex () throws IOException {
50
44
XContentBuilder mapping = XContentFactory .jsonBuilder ().startObject ()
51
45
.startObject ("properties" )
52
46
.startObject ("field" ).field ("type" , "keyword" ).endObject ()
@@ -60,71 +54,72 @@ public void setupIndex() throws IOException {
60
54
.startObject ("field_that_does_not_match" ).field ("type" , "keyword" ).endObject ()
61
55
.endObject ()
62
56
.endObject ();
63
- indexService = createIndex ("index" , Settings .EMPTY , mapping );
57
+
58
+ client ().admin ().indices ().prepareCreate ("index" ).setMapping (mapping ).get ();
64
59
}
65
60
66
61
public void testLeafValues () throws IOException {
67
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
68
- .array ("field" , "first" , "second" )
69
- .startObject ("object" )
70
- .field ("field" , "third" )
71
- .endObject ()
72
- .endObject ();
62
+ indexDocument ( XContentFactory .jsonBuilder ().startObject ()
63
+ .array ("field" , "first" , "second" )
64
+ .startObject ("object" )
65
+ .field ("field" , "third" )
66
+ .endObject ()
67
+ .endObject () );
73
68
74
- FetchSubPhase . HitContext hitContext = hitExecute ( indexService , source , List . of ( "field" , "object.field" ) );
75
- assertThat (hitContext . hit () .getFields ().size (), equalTo (2 ));
69
+ SearchHit hit = fetchFields ( "field" , "object.field" );
70
+ assertThat (hit .getFields ().size (), equalTo (2 ));
76
71
77
- DocumentField field = hitContext . hit () .getFields ().get ("field" );
72
+ DocumentField field = hit .getFields ().get ("field" );
78
73
assertNotNull (field );
79
74
assertThat (field .getValues ().size (), equalTo (2 ));
80
75
assertThat (field .getValues (), hasItems ("first" , "second" ));
81
76
82
- DocumentField objectField = hitContext . hit () .getFields ().get ("object.field" );
77
+ DocumentField objectField = hit .getFields ().get ("object.field" );
83
78
assertNotNull (objectField );
84
79
assertThat (objectField .getValues ().size (), equalTo (1 ));
85
80
assertThat (objectField .getValues (), hasItems ("third" ));
86
81
}
87
82
88
83
public void testObjectValues () throws IOException {
89
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
90
- .startObject ("float_range" )
91
- .field ("gte" , 0.0 )
92
- .field ("lte" , 2.718 )
93
- .endObject ()
94
- .endObject ();
84
+ indexDocument ( XContentFactory .jsonBuilder ().startObject ()
85
+ .startObject ("float_range" )
86
+ .field ("gte" , 0.0 )
87
+ .field ("lte" , 2.718 )
88
+ .endObject ()
89
+ .endObject () );
95
90
96
- FetchSubPhase . HitContext hitContext = hitExecute ( indexService , source , "float_range" );
97
- assertThat (hitContext . hit () .getFields ().size (), equalTo (1 ));
91
+ SearchHit hit = fetchFields ( "float_range" );
92
+ assertThat (hit .getFields ().size (), equalTo (1 ));
98
93
99
- DocumentField rangeField = hitContext . hit () .getFields ().get ("float_range" );
94
+ DocumentField rangeField = hit .getFields ().get ("float_range" );
100
95
assertNotNull (rangeField );
101
96
assertThat (rangeField .getValues ().size (), equalTo (1 ));
102
97
assertThat (rangeField .getValue (), equalTo (Map .of ("gte" , 0.0 , "lte" , 2.718 )));
103
98
}
104
99
105
100
public void testFieldNamesWithWildcard () throws IOException {
106
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
107
- .array ("field" , "first" , "second" )
108
- .field ("integer_field" , "third" )
109
- .startObject ("object" )
110
- .field ("field" , "fourth" )
111
- .endObject ()
112
- .endObject ();
101
+ indexDocument ( XContentFactory .jsonBuilder ().startObject ()
102
+ .array ("field" , "first" , "second" )
103
+ .field ("integer_field" , 42 )
104
+ .startObject ("object" )
105
+ .field ("field" , "fourth" )
106
+ .endObject ()
107
+ .endObject () );
113
108
114
- FetchSubPhase . HitContext hitContext = hitExecute ( indexService , source , "*field" );
115
- assertThat (hitContext . hit () .getFields ().size (), equalTo (3 ));
109
+ SearchHit hit = fetchFields ( "*field" );
110
+ assertThat (hit .getFields ().size (), equalTo (3 ));
116
111
117
- DocumentField field = hitContext . hit () .getFields ().get ("field" );
112
+ DocumentField field = hit .getFields ().get ("field" );
118
113
assertNotNull (field );
119
114
assertThat (field .getValues ().size (), equalTo (2 ));
120
115
assertThat (field .getValues (), hasItems ("first" , "second" ));
121
116
122
- DocumentField otherField = hitContext . hit () .getFields ().get ("integer_field" );
117
+ DocumentField otherField = hit .getFields ().get ("integer_field" );
123
118
assertNotNull (otherField );
124
119
assertThat (otherField .getValues ().size (), equalTo (1 ));
125
- assertThat (otherField .getValues (), hasItems ("third" ));
120
+ assertThat (otherField .getValues (), hasItems (42 ));
126
121
127
- DocumentField objectField = hitContext . hit () .getFields ().get ("object.field" );
122
+ DocumentField objectField = hit .getFields ().get ("object.field" );
128
123
assertNotNull (objectField );
129
124
assertThat (objectField .getValues ().size (), equalTo (1 ));
130
125
assertThat (objectField .getValues (), hasItems ("fourth" ));
@@ -138,77 +133,43 @@ public void testSourceDisabled() throws IOException {
138
133
.endObject ()
139
134
.endObject ();
140
135
141
- IndexService sourceDisabledIndexService = createIndex ("disabled-index" , Settings .EMPTY , mapping );
142
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
143
- .field ("field" , "value" )
144
- .endObject ();
145
-
146
- IllegalArgumentException e = expectThrows (IllegalArgumentException .class ,
147
- () -> hitExecute (sourceDisabledIndexService , source , "field" ));
148
- assertThat (e .getMessage (), containsString ("Unable to retrieve the requested [fields] since _source is disabled in the mapping" ));
149
- }
150
-
151
- public void testNonExistentFields () throws IOException {
152
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
153
- .field ("field" , "value" )
154
- .endObject ();
136
+ client ().admin ().indices ().prepareCreate ("source-disabled" )
137
+ .setMapping (mapping )
138
+ .get ();
155
139
156
- FetchSubPhase .HitContext hitContext = hitExecute (indexService , source , "non_existent" );
157
- assertFalse (hitContext .hit ().getFields ().containsKey ("non_existent" ));
140
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () ->
141
+ client ().prepareSearch ("source-disabled" ).addFetchField ("field" ).get ());
142
+ assertThat (e .getCause ().getMessage (), containsString (
143
+ "Unable to retrieve the requested [fields] since _source is disabled in the mapping" ));
158
144
}
159
145
160
146
public void testObjectFields () throws IOException {
161
- XContentBuilder source = XContentFactory .jsonBuilder ().startObject ()
147
+ indexDocument ( XContentFactory .jsonBuilder ().startObject ()
162
148
.array ("field" , "first" , "second" )
163
149
.startObject ("object" )
164
150
.field ("field" , "third" )
165
151
.endObject ()
166
- .endObject ();
152
+ .endObject ()) ;
167
153
168
- FetchSubPhase . HitContext hitContext = hitExecute ( indexService , source , "object" );
169
- assertFalse (hitContext . hit () .getFields ().containsKey ("object" ));
154
+ SearchHit hit = fetchFields ( "object" );
155
+ assertFalse (hit .getFields ().containsKey ("object" ));
170
156
}
171
157
172
- private FetchSubPhase .HitContext hitExecute (IndexService indexService , XContentBuilder source , String field ) {
173
- return hitExecute (indexService , source , List .of (field ));
158
+ private void indexDocument (XContentBuilder source ) {
159
+ client ().prepareIndex ("index" )
160
+ .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
161
+ .setSource (source )
162
+ .get ();
174
163
}
175
164
176
- private FetchSubPhase .HitContext hitExecute (IndexService indexService , XContentBuilder source , List <String > fields ) {
177
- FetchFieldsContext fetchFieldsContext = new FetchFieldsContext (fields );
178
- BytesReference sourceAsBytes = source != null ? BytesReference .bytes (source ) : null ;
179
- SearchContext searchContext = new FetchFieldsPhaseTestSearchContext (indexService , fetchFieldsContext , sourceAsBytes );
180
-
181
- FetchSubPhase .HitContext hitContext = new FetchSubPhase .HitContext ();
182
- SearchHit searchHit = new SearchHit (1 , null , null , null , null );
183
- hitContext .reset (searchHit , null , 1 , null );
184
-
185
- FetchFieldsPhase phase = new FetchFieldsPhase ();
186
- phase .hitExecute (searchContext , hitContext );
187
- return hitContext ;
188
- }
189
-
190
- private static class FetchFieldsPhaseTestSearchContext extends TestSearchContext {
191
- private final FetchFieldsContext context ;
192
- private final BytesReference source ;
193
-
194
- FetchFieldsPhaseTestSearchContext (IndexService indexService ,
195
- FetchFieldsContext context ,
196
- BytesReference source ) {
197
- super (indexService .getBigArrays (), indexService );
198
- this .context = context ;
199
- this .source = source ;
165
+ private SearchHit fetchFields (String ... fields ) {
166
+ SearchRequestBuilder builder = client ().prepareSearch ("index" );
167
+ for (String field : fields ) {
168
+ builder .addFetchField (field );
200
169
}
170
+ SearchResponse response = builder .get ();
201
171
202
- @ Override
203
- public FetchFieldsContext fetchFieldsContext () {
204
- return context ;
205
- }
206
-
207
- @ Override
208
- public SearchLookup lookup () {
209
- SearchLookup lookup = new SearchLookup (this .mapperService (), this ::getForField );
210
- lookup .source ().setSource (source );
211
- return lookup ;
212
- }
172
+ assertEquals (1 , response .getHits ().getHits ().length );
173
+ return response .getHits ().getHits ()[0 ];
213
174
}
214
175
}
0 commit comments