Skip to content

Commit 89b7937

Browse files
committed
Add tests for queries, aggregations, sorting, and fetching doc values.
1 parent 5924fe0 commit 89b7937

File tree

8 files changed

+437
-6
lines changed

8 files changed

+437
-6
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeIT.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ public void setupSuiteScopeCluster() throws Exception {
116116
.field(SINGLE_VALUED_FIELD_NAME, i * 2 - 1)
117117
.endObject()));
118118
}
119+
120+
// Create two indices and add the field 'route_length_miles' as an alias in
121+
// one, and a concrete field in the other.
122+
prepareCreate("old_index")
123+
.addMapping("_doc", "distance", "type=double", "route_length_miles", "type=alias,path=distance")
124+
.get();
125+
prepareCreate("new_index")
126+
.addMapping("_doc", "route_length_miles", "type=double")
127+
.get();
128+
129+
builders.add(client().prepareIndex("old_index", "_doc").setSource("distance", 42.0));
130+
builders.add(client().prepareIndex("old_index", "_doc").setSource("distance", 50.5));
131+
builders.add(client().prepareIndex("new_index", "_doc").setSource("route_length_miles", 100.2));
132+
builders.add(client().prepareIndex("new_index", "_doc").setSource(Collections.emptyMap()));
133+
119134
indexRandom(true, builders);
120135
ensureSearchable();
121136
}
@@ -972,4 +987,72 @@ public void testDontCacheScripts() throws Exception {
972987
assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
973988
.getMissCount(), equalTo(1L));
974989
}
990+
991+
public void testFieldAlias() {
992+
SearchResponse response = client().prepareSearch("old_index", "new_index")
993+
.addAggregation(range("range")
994+
.field("route_length_miles")
995+
.addUnboundedTo(50.0)
996+
.addRange(50.0, 150.0)
997+
.addUnboundedFrom(150.0))
998+
.execute().actionGet();
999+
1000+
assertSearchResponse(response);
1001+
1002+
Range range = response.getAggregations().get("range");
1003+
assertThat(range, notNullValue());
1004+
assertThat(range.getName(), equalTo("range"));
1005+
List<? extends Range.Bucket> buckets = range.getBuckets();
1006+
assertThat(buckets.size(), equalTo(3));
1007+
1008+
Range.Bucket bucket = buckets.get(0);
1009+
assertThat(bucket, notNullValue());
1010+
assertThat(bucket.getKey(), equalTo("*-50.0"));
1011+
assertThat(bucket.getDocCount(), equalTo(1L));
1012+
1013+
bucket = buckets.get(1);
1014+
assertThat(bucket, notNullValue());
1015+
assertThat(bucket.getKey(), equalTo("50.0-150.0"));
1016+
assertThat(bucket.getDocCount(), equalTo(2L));
1017+
1018+
bucket = buckets.get(2);
1019+
assertThat(bucket, notNullValue());
1020+
assertThat(bucket.getKey(), equalTo("150.0-*"));
1021+
assertThat(bucket.getDocCount(), equalTo(0L));
1022+
}
1023+
1024+
1025+
public void testFieldAliasWithMissingValue() {
1026+
SearchResponse response = client().prepareSearch("old_index", "new_index")
1027+
.addAggregation(range("range")
1028+
.field("route_length_miles")
1029+
.missing(0.0)
1030+
.addUnboundedTo(50.0)
1031+
.addRange(50.0, 150.0)
1032+
.addUnboundedFrom(150.0))
1033+
.execute().actionGet();
1034+
1035+
assertSearchResponse(response);
1036+
1037+
Range range = response.getAggregations().get("range");
1038+
assertThat(range, notNullValue());
1039+
assertThat(range.getName(), equalTo("range"));
1040+
List<? extends Range.Bucket> buckets = range.getBuckets();
1041+
assertThat(buckets.size(), equalTo(3));
1042+
1043+
Range.Bucket bucket = buckets.get(0);
1044+
assertThat(bucket, notNullValue());
1045+
assertThat(bucket.getKey(), equalTo("*-50.0"));
1046+
assertThat(bucket.getDocCount(), equalTo(2L));
1047+
1048+
bucket = buckets.get(1);
1049+
assertThat(bucket, notNullValue());
1050+
assertThat(bucket.getKey(), equalTo("50.0-150.0"));
1051+
assertThat(bucket.getDocCount(), equalTo(2L));
1052+
1053+
bucket = buckets.get(2);
1054+
assertThat(bucket, notNullValue());
1055+
assertThat(bucket.getKey(), equalTo("150.0-*"));
1056+
assertThat(bucket.getDocCount(), equalTo(0L));
1057+
}
9751058
}

server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumIT.java

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.metrics;
2020

21-
import java.util.Collection;
22-
import java.util.Collections;
23-
import java.util.HashMap;
24-
import java.util.List;
25-
import java.util.Map;
26-
21+
import org.elasticsearch.action.index.IndexRequestBuilder;
2722
import org.elasticsearch.action.search.SearchResponse;
2823
import org.elasticsearch.common.settings.Settings;
2924
import org.elasticsearch.plugins.Plugin;
@@ -36,6 +31,14 @@
3631
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
3732
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
3833
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
34+
import org.hamcrest.core.IsNull;
35+
36+
import java.util.ArrayList;
37+
import java.util.Collection;
38+
import java.util.Collections;
39+
import java.util.HashMap;
40+
import java.util.List;
41+
import java.util.Map;
3942

4043
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
4144
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
@@ -61,6 +64,33 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
6164
return Collections.singleton(MetricAggScriptPlugin.class);
6265
}
6366

67+
@Override
68+
public void setupSuiteScopeCluster() throws Exception {
69+
super.setupSuiteScopeCluster();
70+
71+
// Create two indices and add the field 'route_length_miles' as an alias in
72+
// one, and a concrete field in the other.
73+
prepareCreate("old_index")
74+
.addMapping("_doc",
75+
"transit_mode", "type=keyword",
76+
"distance", "type=double",
77+
"route_length_miles", "type=alias,path=distance")
78+
.get();
79+
prepareCreate("new_index")
80+
.addMapping("_doc",
81+
"transit_mode", "type=keyword",
82+
"route_length_miles", "type=double")
83+
.get();
84+
85+
List<IndexRequestBuilder> builders = new ArrayList<>();
86+
builders.add(client().prepareIndex("old_index", "_doc").setSource("transit_mode", "train", "distance", 42.0));
87+
builders.add(client().prepareIndex("old_index", "_doc").setSource("transit_mode", "bus", "distance", 50.5));
88+
builders.add(client().prepareIndex("new_index", "_doc").setSource("transit_mode", "train", "route_length_miles", 100.2));
89+
90+
indexRandom(true, builders);
91+
ensureSearchable();
92+
}
93+
6494
@Override
6595
public void testEmptyAggregation() throws Exception {
6696

@@ -382,4 +412,54 @@ public void testDontCacheScripts() throws Exception {
382412
assertThat(client().admin().indices().prepareStats("cache_test_idx").setRequestCache(true).get().getTotal().getRequestCache()
383413
.getMissCount(), equalTo(1L));
384414
}
415+
416+
public void testFieldAlias() {
417+
SearchResponse response = client().prepareSearch("old_index", "new_index")
418+
.addAggregation(sum("sum")
419+
.field("route_length_miles"))
420+
.execute().actionGet();
421+
422+
assertSearchResponse(response);
423+
424+
Sum sum = response.getAggregations().get("sum");
425+
assertThat(sum, IsNull.notNullValue());
426+
assertThat(sum.getName(), equalTo("sum"));
427+
assertThat(sum.getValue(), equalTo(192.7));
428+
}
429+
430+
public void testFieldAliasInSubAggregation() {
431+
SearchResponse response = client().prepareSearch("old_index", "new_index")
432+
.addAggregation(terms("terms")
433+
.field("transit_mode")
434+
.subAggregation(sum("sum")
435+
.field("route_length_miles")))
436+
.execute().actionGet();
437+
438+
assertSearchResponse(response);
439+
440+
Terms terms = response.getAggregations().get("terms");
441+
assertThat(terms, notNullValue());
442+
assertThat(terms.getName(), equalTo("terms"));
443+
444+
List<? extends Terms.Bucket> buckets = terms.getBuckets();
445+
assertThat(buckets.size(), equalTo(2));
446+
447+
Terms.Bucket bucket = buckets.get(0);
448+
assertThat(bucket, notNullValue());
449+
assertThat(bucket.getKey(), equalTo("train"));
450+
assertThat(bucket.getDocCount(), equalTo(2L));
451+
452+
Sum sum = bucket.getAggregations().get("sum");
453+
assertThat(sum, notNullValue());
454+
assertThat(sum.getValue(), equalTo(142.2));
455+
456+
bucket = buckets.get(1);
457+
assertThat(bucket, notNullValue());
458+
assertThat(bucket.getKey(), equalTo("bus"));
459+
assertThat(bucket.getDocCount(), equalTo(1L));
460+
461+
sum = bucket.getAggregations().get("sum");
462+
assertThat(sum, notNullValue());
463+
assertThat(sum.getValue(), equalTo(50.5));
464+
}
385465
}

server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.elasticsearch.common.document.DocumentField;
3131
import org.elasticsearch.common.joda.Joda;
3232
import org.elasticsearch.common.settings.Settings;
33+
import org.elasticsearch.common.xcontent.XContentBuilder;
3334
import org.elasticsearch.common.xcontent.XContentFactory;
3435
import org.elasticsearch.common.xcontent.XContentType;
3536
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@@ -49,6 +50,8 @@
4950
import org.joda.time.DateTime;
5051
import org.joda.time.DateTimeZone;
5152
import org.joda.time.ReadableDateTime;
53+
import org.joda.time.format.DateTimeFormat;
54+
import org.joda.time.format.DateTimeFormatter;
5255

5356
import java.util.ArrayList;
5457
import java.util.Arrays;
@@ -912,6 +915,71 @@ public void testScriptFields() throws Exception {
912915
}
913916
}
914917

918+
public void testDocValueFieldsWithFieldAlias() throws Exception {
919+
XContentBuilder mapping = XContentFactory.jsonBuilder()
920+
.startObject()
921+
.startObject("type")
922+
.startObject("_source")
923+
.field("enabled", false)
924+
.endObject()
925+
.startObject("properties")
926+
.startObject("text_field")
927+
.field("type", "text")
928+
.field("fielddata", true)
929+
.endObject()
930+
.startObject("date_field")
931+
.field("type", "date")
932+
.field("format", "yyyy-MM-dd")
933+
.endObject()
934+
.startObject("text_field_alias")
935+
.field("type", "alias")
936+
.field("path", "text_field")
937+
.endObject()
938+
.startObject("date_field_alias")
939+
.field("type", "alias")
940+
.field("path", "date_field")
941+
.endObject()
942+
.endObject()
943+
.endObject()
944+
.endObject();
945+
assertAcked(prepareCreate("test").addMapping("type", mapping));
946+
ensureGreen("test");
947+
948+
DateTime date = new DateTime(1990, 12, 29, 0, 0, DateTimeZone.UTC);
949+
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
950+
951+
index("test", "type", "1", "text_field", "foo", "date_field", formatter.print(date));
952+
refresh("test");
953+
954+
SearchRequestBuilder builder = client().prepareSearch().setQuery(matchAllQuery())
955+
.addDocValueField("text_field_alias")
956+
.addDocValueField("date_field_alias", "use_field_mapping")
957+
.addDocValueField("date_field");
958+
SearchResponse searchResponse = builder.execute().actionGet();
959+
960+
assertNoFailures(searchResponse);
961+
assertHitCount(searchResponse, 1);
962+
SearchHit hit = searchResponse.getHits().getAt(0);
963+
964+
Map<String, DocumentField> fields = hit.getFields();
965+
assertThat(fields.keySet(), equalTo(newHashSet("text_field_alias", "date_field_alias", "date_field")));
966+
967+
DocumentField textFieldAlias = fields.get("text_field_alias");
968+
assertThat(textFieldAlias.getName(), equalTo("text_field_alias"));
969+
assertThat(textFieldAlias.getValue(), equalTo("foo"));
970+
971+
DocumentField dateFieldAlias = fields.get("date_field_alias");
972+
assertThat(dateFieldAlias.getName(), equalTo("date_field_alias"));
973+
assertThat(dateFieldAlias.getValue(),
974+
equalTo("1990-12-29"));
975+
976+
DocumentField dateField = fields.get("date_field");
977+
assertThat(dateField.getName(), equalTo("date_field"));
978+
979+
ReadableDateTime fetchedDate = dateField.getValue();
980+
assertThat(fetchedDate, equalTo(date));
981+
}
982+
915983
public void testLoadMetadata() throws Exception {
916984
assertAcked(prepareCreate("test"));
917985

server/src/test/java/org/elasticsearch/search/query/ExistsIT.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.search.SearchResponse;
2525
import org.elasticsearch.common.Strings;
2626
import org.elasticsearch.common.xcontent.XContentBuilder;
27+
import org.elasticsearch.common.xcontent.XContentFactory;
2728
import org.elasticsearch.common.xcontent.json.JsonXContent;
2829
import org.elasticsearch.index.query.QueryBuilders;
2930
import org.elasticsearch.search.SearchHit;
@@ -141,4 +142,89 @@ public void testExists() throws Exception {
141142
}
142143
}
143144
}
145+
146+
public void testFieldAlias() throws Exception {
147+
XContentBuilder mapping = XContentFactory.jsonBuilder()
148+
.startObject()
149+
.startObject("type")
150+
.startObject("properties")
151+
.startObject("bar")
152+
.field("type", "long")
153+
.endObject()
154+
.startObject("foo")
155+
.field("type", "object")
156+
.startObject("properties")
157+
.startObject("bar")
158+
.field("type", "double")
159+
.endObject()
160+
.endObject()
161+
.endObject()
162+
.startObject("foo-bar")
163+
.field("type", "alias")
164+
.field("path", "foo.bar")
165+
.endObject()
166+
.endObject()
167+
.endObject()
168+
.endObject();
169+
assertAcked(prepareCreate("idx").addMapping("type", mapping));
170+
ensureGreen("idx");
171+
172+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
173+
indexRequests.add(client().prepareIndex("idx", "type").setSource(emptyMap()));
174+
indexRequests.add(client().prepareIndex("idx", "type").setSource(emptyMap()));
175+
indexRequests.add(client().prepareIndex("idx", "type").setSource("bar", 3));
176+
indexRequests.add(client().prepareIndex("idx", "type").setSource("foo", singletonMap("bar", 2.718)));
177+
indexRequests.add(client().prepareIndex("idx", "type").setSource("foo", singletonMap("bar", 6.283)));
178+
indexRandom(true, false, indexRequests);
179+
180+
Map<String, Integer> expected = new LinkedHashMap<>();
181+
expected.put("foo.bar", 2);
182+
expected.put("foo-bar", 2);
183+
expected.put("foo*", 2);
184+
expected.put("*bar", 3);
185+
186+
for (Map.Entry<String, Integer> entry : expected.entrySet()) {
187+
String fieldName = entry.getKey();
188+
int expectedCount = entry.getValue();
189+
190+
SearchResponse response = client().prepareSearch("idx")
191+
.setQuery(QueryBuilders.existsQuery(fieldName))
192+
.get();
193+
assertSearchResponse(response);
194+
assertHitCount(response, expectedCount);
195+
}
196+
}
197+
198+
public void testFieldAliasWithNoDocValues() throws Exception {
199+
XContentBuilder mapping = XContentFactory.jsonBuilder()
200+
.startObject()
201+
.startObject("type")
202+
.startObject("properties")
203+
.startObject("foo")
204+
.field("type", "long")
205+
.field("doc_values", false)
206+
.endObject()
207+
.startObject("foo-alias")
208+
.field("type", "alias")
209+
.field("path", "foo")
210+
.endObject()
211+
.endObject()
212+
.endObject()
213+
.endObject();
214+
assertAcked(prepareCreate("idx").addMapping("type", mapping));
215+
ensureGreen("idx");
216+
217+
List<IndexRequestBuilder> indexRequests = new ArrayList<>();
218+
indexRequests.add(client().prepareIndex("idx", "type").setSource(emptyMap()));
219+
indexRequests.add(client().prepareIndex("idx", "type").setSource(emptyMap()));
220+
indexRequests.add(client().prepareIndex("idx", "type").setSource("foo", 3));
221+
indexRequests.add(client().prepareIndex("idx", "type").setSource("foo", 43));
222+
indexRandom(true, false, indexRequests);
223+
224+
SearchResponse response = client().prepareSearch("idx")
225+
.setQuery(QueryBuilders.existsQuery("foo-alias"))
226+
.get();
227+
assertSearchResponse(response);
228+
assertHitCount(response, 2);
229+
}
144230
}

0 commit comments

Comments
 (0)