Search in sources :

Example 6 with FieldStatsResponse

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);
}
Also used : FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse)

Example 7 with FieldStatsResponse

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"));
    }
}
Also used : IndexConstraint(org.elasticsearch.action.fieldstats.IndexConstraint) DateTime(org.joda.time.DateTime) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse)

Example 8 with FieldStatsResponse

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);
}
Also used : IndexConstraint(org.elasticsearch.action.fieldstats.IndexConstraint) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse)

Example 9 with FieldStatsResponse

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);
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) FieldStatsRequest(org.elasticsearch.action.fieldstats.FieldStatsRequest) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) RestStatus(org.elasticsearch.rest.RestStatus) Map(java.util.Map) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) FieldStats(org.elasticsearch.action.fieldstats.FieldStats) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) RestResponse(org.elasticsearch.rest.RestResponse) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) IOException(java.io.IOException) FieldStats(org.elasticsearch.action.fieldstats.FieldStats) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) XContentParser(org.elasticsearch.common.xcontent.XContentParser) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) FieldStatsRequest(org.elasticsearch.action.fieldstats.FieldStatsRequest)

Example 10 with FieldStatsResponse

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));
}
Also used : FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse)

Aggregations

FieldStatsResponse (org.elasticsearch.action.fieldstats.FieldStatsResponse)19 IndexConstraint (org.elasticsearch.action.fieldstats.IndexConstraint)6 FieldStats (org.elasticsearch.action.fieldstats.FieldStats)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 BytesRef (org.apache.lucene.util.BytesRef)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 DateTime (org.joda.time.DateTime)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HalfFloatPoint (org.apache.lucene.document.HalfFloatPoint)1 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)1 FieldStatsRequest (org.elasticsearch.action.fieldstats.FieldStatsRequest)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 Strings (org.elasticsearch.common.Strings)1 Settings (org.elasticsearch.common.settings.Settings)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1