Search in sources :

Example 1 with FieldStats

use of org.elasticsearch.action.fieldstats.FieldStats in project elasticsearch by elastic.

the class FieldStatsTests method testHalfFloat.

public void testHalfFloat() {
    createIndex("test", Settings.EMPTY, "test", "field_index", makeType("half_float", true, false, false), "field_dv", makeType("half_float", false, true, false), "field_stored", makeType("half_float", false, true, true), "field_source", makeType("half_float", false, false, false));
    for (float value = -1; value <= 9; value++) {
        client().prepareIndex("test", "test").setSource("field_index", value, "field_dv", value, "field_stored", value, "field_source", value).get();
    }
    client().admin().indices().prepareRefresh().get();
    FieldStatsResponse result = client().prepareFieldStats().setFields("field_index", "field_dv", "field_stored", "field_source").get();
    for (String field : new String[] { "field_index", "field_dv", "field_stored" }) {
        FieldStats stats = result.getAllFieldStats().get(field);
        assertEquals(stats.getMaxDoc(), 11L);
        assertEquals(stats.getDisplayType(), "float");
        if (field.equals("field_index")) {
            assertEquals(stats.getDocCount(), 11L);
            assertEquals(stats.getDensity(), 100);
            assertEquals(stats.getMinValue(), -1d);
            assertEquals(stats.getMaxValue(), 9d);
            assertEquals(stats.getMinValueAsString(), Float.toString(-1));
            assertEquals(stats.getMaxValueAsString(), Float.toString(9));
        } else {
            assertEquals(stats.getDocCount(), 0L);
            assertNull(stats.getMinValue());
            assertNull(stats.getMaxValue());
            assertEquals(stats.getDensity(), 0);
        }
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) FieldStatsResponse(org.elasticsearch.action.fieldstats.FieldStatsResponse) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 2 with FieldStats

use of org.elasticsearch.action.fieldstats.FieldStats in project elasticsearch by elastic.

the class FieldStatsTests method testMerge.

public void testMerge() {
    List<FieldStats> stats = new ArrayList<>();
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, false, 1L, 1L));
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, false, 1L, 1L));
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, false, 1L, 1L));
    stats.add(new FieldStats.Long(0, 0, 0, 0, false, false));
    FieldStats stat = new FieldStats.Long(1, 1L, 1L, 1L, true, false, 1L, 1L);
    for (FieldStats otherStat : stats) {
        stat.accumulate(otherStat);
    }
    assertEquals(stat.getMaxDoc(), 4L);
    assertEquals(stat.getDocCount(), 4L);
    assertEquals(stat.getSumDocFreq(), 4L);
    assertEquals(stat.getSumTotalTermFreq(), 4L);
    assertEquals(stat.isSearchable(), true);
    assertEquals(stat.isAggregatable(), false);
    assertEquals(stat.getDisplayType(), "integer");
}
Also used : ArrayList(java.util.ArrayList) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 3 with FieldStats

use of org.elasticsearch.action.fieldstats.FieldStats in project elasticsearch by elastic.

the class FieldStatsTests method assertSerialization.

private void assertSerialization(FieldStats stats, Version version) throws IOException {
    BytesStreamOutput output = new BytesStreamOutput();
    output.setVersion(version);
    stats.writeTo(output);
    output.flush();
    StreamInput input = output.bytes().streamInput();
    input.setVersion(version);
    FieldStats deserializedStats = FieldStats.readFrom(input);
    assertEquals(stats, deserializedStats);
    assertEquals(stats.hashCode(), deserializedStats.hashCode());
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 4 with FieldStats

use of org.elasticsearch.action.fieldstats.FieldStats in project elasticsearch by elastic.

the class FieldStatsTests method testMerge_notAvailable.

public void testMerge_notAvailable() {
    List<FieldStats> stats = new ArrayList<>();
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, true, 1L, 1L));
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, true, 1L, 1L));
    stats.add(new FieldStats.Long(1, 1L, 1L, 1L, true, false, 1L, 1L));
    FieldStats stat = new FieldStats.Long(1, -1L, -1L, -1L, false, true, 1L, 1L);
    for (FieldStats otherStat : stats) {
        stat.accumulate(otherStat);
    }
    assertEquals(stat.getMaxDoc(), 4L);
    assertEquals(stat.getDocCount(), -1L);
    assertEquals(stat.getSumDocFreq(), -1L);
    assertEquals(stat.getSumTotalTermFreq(), -1L);
    assertEquals(stat.isSearchable(), true);
    assertEquals(stat.isAggregatable(), true);
    assertEquals(stat.getDisplayType(), "integer");
    stats.add(new FieldStats.Long(1, -1L, -1L, -1L, false, true));
    stat = stats.remove(0);
    for (FieldStats otherStat : stats) {
        stat.accumulate(otherStat);
    }
    assertEquals(stat.getMaxDoc(), 4L);
    assertEquals(stat.getDocCount(), -1L);
    assertEquals(stat.getSumDocFreq(), -1L);
    assertEquals(stat.getSumTotalTermFreq(), -1L);
    assertEquals(stat.isSearchable(), true);
    assertEquals(stat.isAggregatable(), true);
    assertEquals(stat.getDisplayType(), "integer");
    assertNull(stat.getMaxValue());
    assertNull(stat.getMinValue());
}
Also used : ArrayList(java.util.ArrayList) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 5 with FieldStats

use of org.elasticsearch.action.fieldstats.FieldStats 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)

Aggregations

FieldStats (org.elasticsearch.action.fieldstats.FieldStats)11 FieldStatsResponse (org.elasticsearch.action.fieldstats.FieldStatsResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 UncheckedIOException (java.io.UncheckedIOException)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 LongPoint (org.apache.lucene.document.LongPoint)1 FieldInfo (org.apache.lucene.index.FieldInfo)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 IndexReader (org.apache.lucene.index.IndexReader)1 SnapshotDeletionPolicy (org.apache.lucene.index.SnapshotDeletionPolicy)1 Terms (org.apache.lucene.index.Terms)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 BytesRef (org.apache.lucene.util.BytesRef)1 FixedBitSet (org.apache.lucene.util.FixedBitSet)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1