use of org.elasticsearch.action.fieldstats.FieldStatsResponse in project elasticsearch by elastic.
the class FieldStatsTests method testEmptyIndex.
public void testEmptyIndex() {
createIndex("test1", Settings.EMPTY, "type", "value", "type=date");
FieldStatsResponse response = client().prepareFieldStats().setFields("*").setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 1);
assertEquals(response.getIndicesMergedFieldStats().get("test1").size(), 0);
}
use of org.elasticsearch.action.fieldstats.FieldStatsResponse in project elasticsearch by elastic.
the class FieldStatsTests method testDateFiltering_optionalFormat.
public void testDateFiltering_optionalFormat() {
createIndex("test1", Settings.EMPTY, "type", "value", "type=date,format=strict_date_optional_time");
client().prepareIndex("test1", "type").setSource("value", "2014-01-01T00:00:00.000Z").get();
createIndex("test2", Settings.EMPTY, "type", "value", "type=date,format=strict_date_optional_time");
client().prepareIndex("test2", "type").setSource("value", "2014-01-02T00:00:00.000Z").get();
client().admin().indices().prepareRefresh().get();
DateTime dateTime1 = new DateTime(2014, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
DateTime dateTime2 = new DateTime(2014, 1, 2, 0, 0, 0, 0, DateTimeZone.UTC);
FieldStatsResponse response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GT, String.valueOf(dateTime1.getMillis()), "epoch_millis"), new IndexConstraint("value", MAX, LTE, String.valueOf(dateTime2.getMillis()), "epoch_millis")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 1);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), "2014-01-02T00:00:00.000Z");
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getDisplayType(), "date");
try {
client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GT, String.valueOf(dateTime1.getMillis()), "xyz")).setLevel("indices").get();
fail("IllegalArgumentException should have been thrown");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("Invalid format"));
}
}
use of org.elasticsearch.action.fieldstats.FieldStatsResponse in project elasticsearch by elastic.
the class FieldStatsTests method testDateFiltering.
public void testDateFiltering() {
DateTime dateTime1 = new DateTime(2014, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
String dateTime1Str = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().print(dateTime1);
DateTime dateTime2 = new DateTime(2014, 1, 2, 0, 0, 0, 0, DateTimeZone.UTC);
String dateTime2Str = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().print(dateTime2);
createIndex("test1", Settings.EMPTY, "type", "value", "type=date", "value2", "type=date,index=false");
client().prepareIndex("test1", "test").setSource("value", dateTime1Str, "value2", dateTime1Str).get();
createIndex("test2", Settings.EMPTY, "type", "value", "type=date");
client().prepareIndex("test2", "test").setSource("value", dateTime2Str).get();
client().admin().indices().prepareRefresh().get();
FieldStatsResponse response = client().prepareFieldStats().setFields("value").setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 2);
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValue(), dateTime1.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValue(), dateTime2.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValueAsString(), dateTime1Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), dateTime2Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getDisplayType(), "date");
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GTE, "2013-12-30T00:00:00.000Z"), new IndexConstraint("value", MAX, LTE, "2013-12-31T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 0);
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GTE, "2013-12-31T00:00:00.000Z"), new IndexConstraint("value", MAX, LTE, "2014-01-01T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 1);
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValue(), dateTime1.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValueAsString(), dateTime1Str);
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getDisplayType(), "date");
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GT, "2014-01-01T00:00:00.000Z"), new IndexConstraint("value", MAX, LTE, "2014-01-02T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 1);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValue(), dateTime2.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), dateTime2Str);
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GT, "2014-01-02T00:00:00.000Z"), new IndexConstraint("value", MAX, LTE, "2014-01-03T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 0);
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GTE, "2014-01-01T23:00:00.000Z"), new IndexConstraint("value", MAX, LTE, "2014-01-02T01:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 1);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValue(), dateTime2.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), dateTime2Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getDisplayType(), "date");
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MIN, GTE, "2014-01-01T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 2);
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValue(), dateTime1.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValue(), dateTime2.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValueAsString(), dateTime1Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), dateTime2Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getDisplayType(), "date");
response = client().prepareFieldStats().setFields("value").setIndexContraints(new IndexConstraint("value", MAX, LTE, "2014-01-02T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 2);
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValue(), dateTime1.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValue(), dateTime2.getMillis());
assertEquals(response.getIndicesMergedFieldStats().get("test1").get("value").getMinValueAsString(), dateTime1Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getMinValueAsString(), dateTime2Str);
assertEquals(response.getIndicesMergedFieldStats().get("test2").get("value").getDisplayType(), "date");
response = client().prepareFieldStats().setFields("value2").setIndexContraints(new IndexConstraint("value2", MAX, LTE, "2014-01-02T00:00:00.000Z")).setLevel("indices").get();
assertEquals(response.getIndicesMergedFieldStats().size(), 0);
}
use of org.elasticsearch.action.fieldstats.FieldStatsResponse in project elasticsearch by elastic.
the class RestFieldStatsAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
if (request.hasContentOrSourceParam() && request.hasParam("fields")) {
throw new IllegalArgumentException("can't specify a request body and [fields] request parameter, " + "either specify a request body or the [fields] request parameter");
}
final FieldStatsRequest fieldStatsRequest = new FieldStatsRequest();
fieldStatsRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
fieldStatsRequest.indicesOptions(IndicesOptions.fromRequest(request, fieldStatsRequest.indicesOptions()));
fieldStatsRequest.level(request.param("level", FieldStatsRequest.DEFAULT_LEVEL));
if (request.hasContentOrSourceParam()) {
try (XContentParser parser = request.contentOrSourceParamParser()) {
fieldStatsRequest.source(parser);
}
} else {
fieldStatsRequest.setFields(Strings.splitStringByCommaToArray(request.param("fields")));
}
return channel -> client.fieldStats(fieldStatsRequest, new RestBuilderListener<FieldStatsResponse>(channel) {
@Override
public RestResponse buildResponse(FieldStatsResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
buildBroadcastShardsHeader(builder, request, response);
builder.startObject("indices");
for (Map.Entry<String, Map<String, FieldStats>> entry1 : response.getIndicesMergedFieldStats().entrySet()) {
builder.startObject(entry1.getKey());
builder.startObject("fields");
for (Map.Entry<String, FieldStats> entry2 : entry1.getValue().entrySet()) {
builder.field(entry2.getKey());
entry2.getValue().toXContent(builder, request);
}
builder.endObject();
builder.endObject();
}
builder.endObject();
if (response.getConflicts().size() > 0) {
builder.startObject("conflicts");
for (Map.Entry<String, String> entry : response.getConflicts().entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
}
builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder);
}
});
}
use of org.elasticsearch.action.fieldstats.FieldStatsResponse in project elasticsearch by elastic.
the class FieldStatsIntegrationIT method testWildcardFields.
public void testWildcardFields() throws Exception {
assertAcked(prepareCreate("test1").addMapping("test", "foo", "type=long", "foobar", "type=long", "barfoo", "type=long"));
assertAcked(prepareCreate("test2").addMapping("test", "foobar", "type=long", "barfoo", "type=long"));
ensureGreen("test1", "test2");
FieldStatsResponse response = client().prepareFieldStats().setFields("foo*").get();
assertAllSuccessful(response);
assertThat(response.getAllFieldStats().size(), equalTo(0));
indexRange("test1", "foo", -100, 0);
indexRange("test2", "foo", -10, 100);
indexRange("test1", "foobar", -10, 100);
indexRange("test2", "foobar", -100, 0);
response = client().prepareFieldStats().setFields("foo*").get();
assertAllSuccessful(response);
assertThat(response.getAllFieldStats().size(), equalTo(2));
assertThat(response.getAllFieldStats().get("foo").getMinValue(), equalTo(-100L));
assertThat(response.getAllFieldStats().get("foo").getMaxValue(), equalTo(100L));
assertThat(response.getAllFieldStats().get("foobar").getMinValue(), equalTo(-100L));
assertThat(response.getAllFieldStats().get("foobar").getMaxValue(), equalTo(100L));
response = client().prepareFieldStats().setFields("foo*").setLevel("indices").get();
assertAllSuccessful(response);
assertThat(response.getIndicesMergedFieldStats().size(), equalTo(2));
assertThat(response.getIndicesMergedFieldStats().get("test1").size(), equalTo(2));
assertThat(response.getIndicesMergedFieldStats().get("test1").get("foo").getMinValue(), equalTo(-100L));
assertThat(response.getIndicesMergedFieldStats().get("test1").get("foo").getMaxValue(), equalTo(0L));
assertThat(response.getIndicesMergedFieldStats().get("test1").get("foobar").getMinValue(), equalTo(-10L));
assertThat(response.getIndicesMergedFieldStats().get("test1").get("foobar").getMaxValue(), equalTo(100L));
assertThat(response.getIndicesMergedFieldStats().get("test2").size(), equalTo(2));
assertThat(response.getIndicesMergedFieldStats().get("test2").get("foobar").getMinValue(), equalTo(-100L));
assertThat(response.getIndicesMergedFieldStats().get("test2").get("foobar").getMaxValue(), equalTo(0L));
assertThat(response.getIndicesMergedFieldStats().get("test2").get("foo").getMinValue(), equalTo(-10L));
assertThat(response.getIndicesMergedFieldStats().get("test2").get("foo").getMaxValue(), equalTo(100L));
}
Aggregations