2015-04-09 19:14:48 -04:00
|
|
|
[[search-field-stats]]
|
|
|
|
== Field stats API
|
|
|
|
|
|
|
|
experimental[]
|
|
|
|
|
2015-05-24 09:08:27 -04:00
|
|
|
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 based on the min/max range of values.
|
2015-04-09 19:14:48 -04:00
|
|
|
|
2016-05-19 07:45:53 -04:00
|
|
|
For the following examples, lets assume the following indexed data:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
-------------------------------------------------
|
|
|
|
PUT /github/user/1?refresh
|
|
|
|
{
|
|
|
|
"user": "kimchy",
|
|
|
|
"project": "elasticsearch",
|
|
|
|
"rating": "great project"
|
|
|
|
}
|
|
|
|
|
|
|
|
PUT /twitter/tweet/1?refresh
|
|
|
|
{
|
|
|
|
"user": "kimchy",
|
|
|
|
"message": "you know, for search",
|
|
|
|
"rating": 10
|
|
|
|
}
|
|
|
|
------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
|
|
|
|
|
|
|
|
2015-05-24 09:08:27 -04:00
|
|
|
The field stats api by defaults executes on all indices, but can execute on
|
|
|
|
specific indices too.
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
All indices:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
GET /_field_stats?fields=rating
|
2015-04-09 19:14:48 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
Specific indices:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
GET /twitter,github/_field_stats?fields=rating
|
2015-04-09 19:14:48 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
Supported request options:
|
2015-04-23 04:50:01 -04:00
|
|
|
|
2015-04-26 09:51:52 -04:00
|
|
|
[horizontal]
|
2016-04-26 06:12:45 -04:00
|
|
|
`fields`:: A list of fields to compute stats for. The field name supports wildcard notation. For example, using `text_*`
|
|
|
|
will cause all fields that match the expression to be returned.
|
2015-05-24 09:08:27 -04:00
|
|
|
`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` (default).
|
2015-04-09 19:14:48 -04:00
|
|
|
|
2015-05-20 10:53:17 -04:00
|
|
|
Alternatively the `fields` option can also be defined in the request body:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
POST /_field_stats?level=indices
|
|
|
|
{
|
2015-05-20 10:53:17 -04:00
|
|
|
"fields" : ["rating"]
|
2016-05-19 07:45:53 -04:00
|
|
|
}
|
2015-05-20 10:53:17 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-05-20 10:53:17 -04:00
|
|
|
|
|
|
|
This is equivalent to the previous request.
|
|
|
|
|
2015-05-24 09:08:27 -04:00
|
|
|
[float]
|
2015-04-23 03:57:31 -04:00
|
|
|
=== Field statistics
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
The field stats api is supported on string based, number based and date based fields and can return the following statistics per field:
|
|
|
|
|
2015-04-26 09:51:52 -04:00
|
|
|
[horizontal]
|
|
|
|
`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
|
2015-05-24 09:08:27 -04:00
|
|
|
measurement isn't available on one or more shards.
|
|
|
|
Document frequency is the number of documents containing a particular term.
|
2015-04-26 09:51:52 -04:00
|
|
|
|
|
|
|
`sum_total_term_freq`::
|
|
|
|
|
|
|
|
The sum of the term frequencies of all terms in this field across all
|
2015-05-24 09:08:27 -04:00
|
|
|
documents, or -1 if this measurement isn't available on one or more shards.
|
2015-04-26 09:51:52 -04:00
|
|
|
Term frequency is the total number of occurrences of a term in a particular
|
|
|
|
document and field.
|
|
|
|
|
2016-04-26 06:12:45 -04:00
|
|
|
`is_searchable`
|
|
|
|
|
|
|
|
True if any of the instances of the field is searchable, false otherwise.
|
|
|
|
|
|
|
|
`is_aggregatable`
|
|
|
|
|
|
|
|
True if any of the instances of the field is aggregatable, false otherwise.
|
|
|
|
|
2015-04-26 09:51:52 -04:00
|
|
|
`min_value`::
|
|
|
|
|
2015-11-11 01:55:35 -05:00
|
|
|
The lowest value in the field.
|
|
|
|
|
|
|
|
`min_value_as_string`::
|
|
|
|
|
|
|
|
The lowest value in the field represented in a displayable form. All fields,
|
|
|
|
but string fields returns this. (since string fields, represent values already as strings)
|
2015-04-26 09:51:52 -04:00
|
|
|
|
|
|
|
`max_value`::
|
|
|
|
|
2015-11-11 01:55:35 -05:00
|
|
|
The highest value in the field.
|
|
|
|
|
|
|
|
`max_value_as_string`::
|
|
|
|
|
|
|
|
The highest value in the field represented in a displayable form. All fields,
|
|
|
|
but string fields returns this. (since string fields, represent values already as strings)
|
2015-04-26 09:51:52 -04:00
|
|
|
|
2015-05-24 09:08:27 -04:00
|
|
|
NOTE: Documents marked as deleted (but not yet removed by the merge process)
|
|
|
|
still affect all the mentioned statistics.
|
2015-04-09 19:14:48 -04:00
|
|
|
|
2015-07-01 06:05:12 -04:00
|
|
|
[float]
|
|
|
|
=== Cluster level field statistics example
|
2015-05-24 09:08:27 -04:00
|
|
|
|
2015-07-01 06:05:12 -04:00
|
|
|
Request:
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
GET /_field_stats?fields=rating,user,project,message
|
2015-04-09 19:14:48 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-04-09 19:14:48 -04:00
|
|
|
|
2015-07-01 06:05:12 -04:00
|
|
|
Response:
|
|
|
|
|
2015-07-14 12:14:09 -04:00
|
|
|
[source,js]
|
2015-04-09 19:14:48 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"_shards": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"total": 10,
|
|
|
|
"successful": 10,
|
2015-04-09 19:14:48 -04:00
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"indices": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"_all": { <1>
|
2015-04-09 19:14:48 -04:00
|
|
|
"fields": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"project": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 1,
|
|
|
|
"sum_total_term_freq": 1,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "elasticsearch",
|
|
|
|
"max_value": "elasticsearch"
|
2015-04-09 19:14:48 -04:00
|
|
|
},
|
2016-05-19 07:45:53 -04:00
|
|
|
"message": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 4,
|
|
|
|
"sum_total_term_freq": 4,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "for",
|
|
|
|
"max_value": "you"
|
2015-04-09 19:14:48 -04:00
|
|
|
},
|
2016-05-19 07:45:53 -04:00
|
|
|
"user": {
|
|
|
|
"max_doc": 2,
|
|
|
|
"doc_count": 2,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 2,
|
|
|
|
"sum_total_term_freq": 2,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "kimchy",
|
|
|
|
"max_value": "kimchy"
|
2015-04-09 19:14:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 07:45:53 -04:00
|
|
|
},
|
|
|
|
"conflicts": {
|
2016-07-27 08:30:14 -04:00
|
|
|
"rating": "Field [rating] of type [integer] conflicts with existing field of type [string] in other index." <2>
|
2015-04-09 19:14:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// TESTRESPONSE
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
<1> The `_all` key indicates that it contains the field stats of all indices in the cluster.
|
|
|
|
|
2016-05-19 07:45:53 -04:00
|
|
|
<2> When using the cluster level field statistics it is possible to have conflicts if the same field is used in
|
2016-04-26 06:12:45 -04:00
|
|
|
different indices with incompatible types. For instance a field of type `long` is not compatible with a field of
|
|
|
|
type `float` or `string`. A section named `conflicts` is added to the response if one or more conflicts are raised.
|
|
|
|
It contains all the fields with conflicts and the reason of the incompatibility.
|
|
|
|
|
2015-07-01 06:05:12 -04:00
|
|
|
[float]
|
|
|
|
==== Indices level field statistics example
|
|
|
|
|
|
|
|
Request:
|
2015-04-09 19:14:48 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
GET /_field_stats?fields=rating,user,project,message&level=indices
|
2015-04-09 19:14:48 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-04-09 19:14:48 -04:00
|
|
|
|
2015-07-01 06:05:12 -04:00
|
|
|
Response:
|
|
|
|
|
2015-04-09 19:14:48 -04:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"_shards": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"total": 10,
|
|
|
|
"successful": 10,
|
2015-04-09 19:14:48 -04:00
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"indices": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"github": {
|
2015-04-09 19:14:48 -04:00
|
|
|
"fields": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"rating": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 2,
|
|
|
|
"sum_total_term_freq": 2,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "great",
|
|
|
|
"max_value": "project"
|
2015-04-09 19:14:48 -04:00
|
|
|
},
|
2016-05-19 07:45:53 -04:00
|
|
|
"project": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 1,
|
|
|
|
"sum_total_term_freq": 1,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "elasticsearch",
|
|
|
|
"max_value": "elasticsearch"
|
2015-04-09 19:14:48 -04:00
|
|
|
},
|
2016-05-19 07:45:53 -04:00
|
|
|
"user": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 1,
|
|
|
|
"sum_total_term_freq": 1,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "kimchy",
|
|
|
|
"max_value": "kimchy"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"twitter": {
|
|
|
|
"fields": {
|
2015-04-09 19:14:48 -04:00
|
|
|
"rating": {
|
2016-05-19 07:45:53 -04:00
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": -1,
|
|
|
|
"sum_total_term_freq": 1,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "integer",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": true,
|
|
|
|
"min_value": 10,
|
|
|
|
"min_value_as_string": "10",
|
|
|
|
"max_value": 10,
|
|
|
|
"max_value_as_string": "10"
|
|
|
|
},
|
|
|
|
"message": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 4,
|
|
|
|
"sum_total_term_freq": 4,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "for",
|
|
|
|
"max_value": "you"
|
|
|
|
},
|
|
|
|
"user": {
|
|
|
|
"max_doc": 1,
|
|
|
|
"doc_count": 1,
|
|
|
|
"density": 100,
|
|
|
|
"sum_doc_freq": 1,
|
|
|
|
"sum_total_term_freq": 1,
|
2016-07-27 08:30:14 -04:00
|
|
|
"type": "string",
|
2016-05-19 07:45:53 -04:00
|
|
|
"searchable": true,
|
|
|
|
"aggregatable": false,
|
|
|
|
"min_value": "kimchy",
|
|
|
|
"max_value": "kimchy"
|
2015-04-09 19:14:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// TESTRESPONSE
|
2015-05-24 09:08:27 -04:00
|
|
|
<1> The `stack` key means it contains all field stats for the `stack` index.
|
|
|
|
|
2015-05-20 10:53:17 -04:00
|
|
|
[float]
|
|
|
|
=== Field stats index constraints
|
|
|
|
|
|
|
|
Field stats index constraints allows to omit all field stats for indices that don't match with the constraint. An index
|
|
|
|
constraint can exclude indices' field stats based on the `min_value` and `max_value` statistic. This option is only
|
|
|
|
useful if the `level` option is set to `indices`.
|
|
|
|
|
|
|
|
For example index constraints can be useful to find out the min and max value of a particular property of your data in
|
|
|
|
a time based scenario. The following request only returns field stats for the `answer_count` property for indices
|
|
|
|
holding questions created in the year 2014:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
POST /_field_stats?level=indices
|
|
|
|
{
|
|
|
|
"fields" : ["rating"], <1>
|
2015-05-20 10:53:17 -04:00
|
|
|
"index_constraints" : { <2>
|
|
|
|
"creation_date" : { <3>
|
2016-07-07 08:54:50 -04:00
|
|
|
"max_value" : { <4>
|
2015-11-18 02:09:47 -05:00
|
|
|
"gte" : "2014-01-01T00:00:00.000Z"
|
2015-05-20 10:53:17 -04:00
|
|
|
},
|
2016-07-07 08:54:50 -04:00
|
|
|
"min_value" : { <4>
|
2015-05-20 10:53:17 -04:00
|
|
|
"lt" : "2015-01-01T00:00:00.000Z"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 07:45:53 -04:00
|
|
|
}
|
2015-05-20 10:53:17 -04:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2015-05-20 10:53:17 -04:00
|
|
|
|
|
|
|
<1> The fields to compute and return field stats for.
|
|
|
|
<2> The set index constraints. Note that index constrains can be defined for fields that aren't defined in the `fields` option.
|
|
|
|
<3> Index constraints for the field `creation_date`.
|
2016-07-07 08:54:50 -04:00
|
|
|
<4> Index constraints on the `max_value` and `min_value` property of a field statistic.
|
2015-05-20 10:53:17 -04:00
|
|
|
|
|
|
|
For a field, index constraints can be defined on the `min_value` statistic, `max_value` statistic or both.
|
|
|
|
Each index constraint support the following comparisons:
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`gte`:: Greater-than or equal to
|
|
|
|
`gt`:: Greater-than
|
|
|
|
`lte`:: Less-than or equal to
|
|
|
|
`lt`:: Less-than
|
2015-11-18 02:09:47 -05:00
|
|
|
|
|
|
|
Field stats index constraints on date fields optionally accept a `format` option, used to parse the constraint's value.
|
|
|
|
If missing, the format configured in the field's mapping is used.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
POST /_field_stats?level=indices
|
|
|
|
{
|
|
|
|
"fields" : ["rating"],
|
2015-11-18 09:13:17 -05:00
|
|
|
"index_constraints" : {
|
|
|
|
"creation_date" : {
|
2016-07-07 08:54:50 -04:00
|
|
|
"max_value" : {
|
2015-11-18 02:09:47 -05:00
|
|
|
"gte" : "2014-01-01",
|
2015-11-18 09:13:17 -05:00
|
|
|
"format" : "date_optional_time" <1>
|
2015-11-18 02:09:47 -05:00
|
|
|
},
|
2016-07-07 08:54:50 -04:00
|
|
|
"min_value" : {
|
2015-11-18 02:09:47 -05:00
|
|
|
"lt" : "2015-01-01",
|
2015-11-18 09:16:13 -05:00
|
|
|
"format" : "date_optional_time"
|
2015-11-18 02:09:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 07:45:53 -04:00
|
|
|
}
|
2015-11-18 09:13:17 -05:00
|
|
|
--------------------------------------------------
|
2016-05-19 07:45:53 -04:00
|
|
|
// CONSOLE
|
2016-04-26 06:12:45 -04:00
|
|
|
<1> Custom date format
|