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);
}
}
}
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");
}
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());
}
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());
}
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);
}
});
}
Aggregations