Skip to content

Commit

Permalink
Added field stats api
Browse files Browse the repository at this point in the history
The field stats api returns field level statistics such as lowest, highest values and number of documents that have at least one value for a field.

An api like this can be useful to explore a data set you don't know much about. For example you can figure at with the lowest and highest response times are, so that you can create a histogram or range aggregation with sane settings.

This api doesn't run a search to figure this statistics out, but rather use the Lucene index look these statics up (using Terms class in Lucene). So finding out these stats for fields is cheap and quick.

The min/max values are based on the type of the field. So for a numeric field min/max are numbers and date field the min/max date and other fields the min/max are term based.
  • Loading branch information
martijnvg committed Apr 23, 2015
1 parent 7bd4654 commit c07698a
Show file tree
Hide file tree
Showing 28 changed files with 1,938 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/reference/search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ include::search/percolate.asciidoc[]

include::search/more-like-this.asciidoc[]

include::search/field-stats.asciidoc[]

170 changes: 170 additions & 0 deletions docs/reference/search/field-stats.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
[[search-field-stats]]
== Field stats API

experimental[]

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.

All indices:

[source,js]
--------------------------------------------------
curl -XGET "http://localhost:9200/_field_stats?fields=rating"
--------------------------------------------------

Specific indices:

[source,js]
--------------------------------------------------
curl -XGET "http://localhost:9200/index1,index2/_field_stats?fields=rating"
--------------------------------------------------

Supported request options:
* `fields` - A list of fields to compute stats for.
* `level` - Defines if field stats should be returned on a per index level or on a cluster wide level. Valid values are
`indices` and `cluster`. Defaults to `cluster`.

==== Field statistics

The field stats api is supported on string based, number based and date based fields and can return the following statistics per field:

* `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` - 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.
Term frequency is the total number of occurrences of a term in a particular document and field.
* `min_value` - The lowest value in the field represented in a displayable form.
* `max_value` - The highest value in the field represented in a displayable form.

Note that for all the mentioned statistics, documents marked as deleted aren't taken into account. The documents marked
as deleted are are only taken into account when the segments these documents reside on are merged away.

==== Example

[source,js]
--------------------------------------------------
curl -XGET "http://localhost:9200/_field_stats?fields=rating,answer_count,creation_date,display_name"
--------------------------------------------------

[source,js]
--------------------------------------------------
{
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"indices": {
"_all": { <1>
"fields": {
"creation_date": {
"max_doc": 1326564,
"doc_count": 564633,
"density": 42,
"sum_doc_freq": 2258532,
"sum_total_term_freq": -1,
"min_value": "2008-08-01T16:37:51.513Z",
"max_value": "2013-06-02T03:23:11.593Z"
},
"display_name": {
"max_doc": 1326564,
"doc_count": 126741,
"density": 9,
"sum_doc_freq": 166535,
"sum_total_term_freq": 166616,
"min_value": "0",
"max_value": "정혜선"
},
"answer_count": {
"max_doc": 1326564,
"doc_count": 139885,
"density": 10,
"sum_doc_freq": 559540,
"sum_total_term_freq": -1,
"min_value": 0,
"max_value": 160
},
"rating": {
"max_doc": 1326564,
"doc_count": 437892,
"density": 33,
"sum_doc_freq": 1751568,
"sum_total_term_freq": -1,
"min_value": -14,
"max_value": 1277
}
}
}
}
}
--------------------------------------------------

<1> The `_all` key indicates that it contains the field stats of all indices in the cluster.

With level set to `indices`:

[source,js]
--------------------------------------------------
curl -XGET "http://localhost:9200/_field_stats?fields=rating,answer_count,creation_date,display_name&level=indices"
--------------------------------------------------

[source,js]
--------------------------------------------------
{
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"indices": {
"stack": { <1>
"fields": {
"creation_date": {
"max_doc": 1326564,
"doc_count": 564633,
"density": 42,
"sum_doc_freq": 2258532,
"sum_total_term_freq": -1,
"min_value": "2008-08-01T16:37:51.513Z",
"max_value": "2013-06-02T03:23:11.593Z"
},
"display_name": {
"max_doc": 1326564,
"doc_count": 126741,
"density": 9,
"sum_doc_freq": 166535,
"sum_total_term_freq": 166616,
"min_value": "0",
"max_value": "정혜선"
},
"answer_count": {
"max_doc": 1326564,
"doc_count": 139885,
"density": 10,
"sum_doc_freq": 559540,
"sum_total_term_freq": -1,
"min_value": 0,
"max_value": 160
},
"rating": {
"max_doc": 1326564,
"doc_count": 437892,
"density": 33,
"sum_doc_freq": 1751568,
"sum_total_term_freq": -1,
"min_value": -14,
"max_value": 1277
}
}
}
}
}
--------------------------------------------------

<1> The `stack` key means it contains all field stats for the `stack` index.
46 changes: 46 additions & 0 deletions rest-api-spec/api/field_stats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"field_stats": {
"documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/master/search-field-stats.html",
"methods": ["GET", "POST"],
"url": {
"path": "/_field_stats",
"paths": [
"/_field_stats",
"/{index}/_field_stats"
],
"parts": {
"index": {
"type" : "list",
"description" : "A comma-separated list of index names; use `_all` or empty string to perform the operation on all indices"
}
},
"params": {
"fields": {
"type" : "list",
"description" : "A comma-separated list of fields for to get field statistics for (min value, max value, and more)"
},
"level": {
"type" : "enum",
"options" : ["indices", "cluster"],
"default" : "cluster",
"description" : "Defines if field stats should be returned on a per index level or on a cluster wide level"
},
"ignore_unavailable": {
"type" : "boolean",
"description" : "Whether specified concrete indices should be ignored when unavailable (missing or closed)"
},
"allow_no_indices": {
"type" : "boolean",
"description" : "Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)"
},
"expand_wildcards": {
"type" : "enum",
"options" : ["open","closed","none","all"],
"default" : "open",
"description" : "Whether to expand wildcard expression to concrete indices that are open, closed or both."
}
}
},
"body": null
}
}
52 changes: 52 additions & 0 deletions rest-api-spec/test/field_stats/10_basics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
"Basic field stats":
- do:
index:
index: test_1
type: test
id: id_1
body: { foo: "bar", number: 123 }

- do:
indices.refresh: {}

- do:
field_stats:
index: test_1
fields: [foo, number]

- match: { indices._all.fields.foo.max_doc: 1 }
- match: { indices._all.fields.foo.doc_count: 1 }
- match: { indices._all.fields.foo.min_value: "bar" }
- match: { indices._all.fields.foo.max_value: "bar" }
- match: { indices._all.fields.number.max_doc: 1 }
- match: { indices._all.fields.number.doc_count: 1 }
- match: { indices._all.fields.number.min_value: 123 }
- match: { indices._all.fields.number.max_value: 123 }

---
"Basic field stats with level set to indices":
- do:
index:
index: test_1
type: test
id: id_1
body: { foo: "bar", number: 123 }

- do:
indices.refresh: {}

- do:
field_stats:
index: test_1
fields: [foo, number]
level: indices

- match: { indices.test_1.fields.foo.max_doc: 1 }
- match: { indices.test_1.fields.foo.doc_count: 1 }
- match: { indices.test_1.fields.foo.min_value: "bar" }
- match: { indices.test_1.fields.foo.max_value: "bar" }
- match: { indices.test_1.fields.number.max_doc: 1 }
- match: { indices.test_1.fields.number.doc_count: 1 }
- match: { indices.test_1.fields.number.min_value: 123 }
- match: { indices.test_1.fields.number.max_value: 123 }
4 changes: 4 additions & 0 deletions src/main/java/org/elasticsearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
import org.elasticsearch.action.exists.TransportExistsAction;
import org.elasticsearch.action.explain.ExplainAction;
import org.elasticsearch.action.explain.TransportExplainAction;
import org.elasticsearch.action.fieldstats.FieldStatsAction;
import org.elasticsearch.action.fieldstats.TransportFieldStatsTransportAction;
import org.elasticsearch.action.get.*;
import org.elasticsearch.action.index.IndexAction;
import org.elasticsearch.action.index.TransportIndexAction;
Expand Down Expand Up @@ -312,6 +314,8 @@ protected void configure() {
registerAction(GetIndexedScriptAction.INSTANCE, TransportGetIndexedScriptAction.class);
registerAction(DeleteIndexedScriptAction.INSTANCE, TransportDeleteIndexedScriptAction.class);

registerAction(FieldStatsAction.INSTANCE, TransportFieldStatsTransportAction.class);

// register Name -> GenericAction Map that can be injected to instances.
MapBinder<String, GenericAction> actionsBinder
= MapBinder.newMapBinder(binder(), String.class, GenericAction.class);
Expand Down
Loading

0 comments on commit c07698a

Please sign in to comment.