Skip to content

Commit

Permalink
applied feedback 1
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvg committed Apr 16, 2015
1 parent d380969 commit cebac06
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 41 deletions.
8 changes: 4 additions & 4 deletions docs/reference/search/field-stats.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

experimental[]

The field stats api allows to easily figure certain statistical properties without a field without executing a search, but
looking up measurements that are natively available in the Lucene index. This can be useful to explore a data set,
that you don't know much about in order to for example create histogram aggregation with a meaningful interval.
The field stats api allows one to find statistical properties of a field without executing a search, but
looking up measurements that are natively available in the Lucene index. This can be useful to explore a dataset which
you don't know much about. For example, this allows creating a histogram aggregation with meaningful intervals.

The field stats api by defaults executes on all indices, but can execute on specific indices too.

Expand All @@ -32,7 +32,7 @@ The field stats api is supported on string based, number based and date based fi

* `max_doc` - The total number of documents.
* `doc_count` - The number of documents that have at least one term for this field, or -1 if this measurement isn't available on one or more shards.
* `density` - A ratio as a percentage between all the documents that have at least one value for this field and all documents.
* `density` - The percentage of documents that have at least one value for this field. This is a derived statistic and is based on the `max_doc` and `doc_count`.
* `sum_doc_freq` - The sum of each term's document frequency in this field, or -1 if this measurement isn't available on one or more shards.
Document frequency is the number of documents containing a particular term.
* `sum_total_term_freq` - The sum of the term frequencies of all terms in this field across all documents, or -1 if this measurement isn't available on one or more shards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

import java.io.IOException;

/**
*/
public abstract class FieldStats<T> implements Streamable, ToXContent {

private byte type;
Expand Down Expand Up @@ -73,7 +71,9 @@ public long getDocCount() {
}

/**
* @return A ratio as percentage of between all the documents that have at least one value for this field and all documents. (doc_count / max_doc)
* @return The percentage of documents that have at least one value for this field.
*
* This is a derived statistic and is based on: 'doc_count / max_doc'
*/
public int getDensity() {
if (docCount < 0 || maxDoc <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,23 @@ public class FieldStatsRequest extends BroadcastOperationRequest<FieldStatsReque

private String[] fields;

public String[] fields() {
public String[] getFields() {
return fields;
}

public void fields(String[] fields) {
public void setFields(String[] fields) {
this.fields = fields;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
fields = in.readStringArray();
}
fields = in.readStringArray();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (fields != null) {
out.writeBoolean(true);
out.writeStringArray(fields);
} else {
out.writeBoolean(false);
}
out.writeStringArrayNullable(fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FieldStatsRequestBuilder(Client client) {
}

public FieldStatsRequestBuilder fields(String... fields) {
request().fields(fields);
request().setFields(fields);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,23 @@ public FieldStatsShardRequest() {

public FieldStatsShardRequest(ShardId shardId, FieldStatsRequest request) {
super(shardId, request);
this.fields = request.fields();
this.fields = request.getFields();
}

public String[] fields() {
public String[] getFields() {
return fields;
}

public void fields(String[] fields) {
this.fields = fields;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
fields = in.readStringArray();
}
fields = in.readStringArray();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (fields != null) {
out.writeBoolean(true);
out.writeStringArray(fields);
} else {
out.writeBoolean(false);
}
out.writeStringArrayNullable(fields);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,13 @@ protected FieldStatsResponse newResponse(FieldStatsRequest request, AtomicRefere
int successfulShards = 0;
int failedShards = 0;
Map<String, FieldStats> mergedFieldStats = new HashMap<>();
List<ShardOperationFailedException> shardFailures = null;
List<ShardOperationFailedException> shardFailures = new ArrayList<>();
for (int i = 0; i < shardsResponses.length(); i++) {
Object shardResponse = shardsResponses.get(i);
if (shardResponse == null) {
// simply ignore non active shards
} else if (shardResponse instanceof BroadcastShardOperationFailedException) {
failedShards++;
if (shardFailures == null) {
shardFailures = new ArrayList<>();
}
shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse));
} else {
successfulShards++;
Expand Down Expand Up @@ -132,7 +129,7 @@ protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request)
IndexShard shard = indexServices.shardSafe(shardId.id());
shard.readAllowed();
try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
for (String field : request.fields()) {
for (String field : request.getFields()) {
FieldMappers fieldMappers = mapperService.fullName(field);
if (fieldMappers != null) {
IndexReader reader = searcher.reader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
final FieldStatsRequest fieldStatsRequest = new FieldStatsRequest();
fieldStatsRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
fieldStatsRequest.indicesOptions(IndicesOptions.fromRequest(request, fieldStatsRequest.indicesOptions()));
fieldStatsRequest.fields(Strings.splitStringByCommaToArray(request.param("fields")));
fieldStatsRequest.setFields(Strings.splitStringByCommaToArray(request.param("fields")));
fieldStatsRequest.indicesOptions(IndicesOptions.fromRequest(request, fieldStatsRequest.indicesOptions()));
fieldStatsRequest.listenerThreaded(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/**
*/
public class FieldStatsTests extends ElasticsearchIntegrationTest {
public class FieldStatsIntegrationTests extends ElasticsearchIntegrationTest {

@Test
public void testRandom() throws Exception {
Expand Down

0 comments on commit cebac06

Please sign in to comment.