field-stats.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. [[search-field-stats]]
  2. == Field stats API
  3. experimental[]
  4. The field stats api allows one to find statistical properties of a field
  5. without executing a search, but looking up measurements that are natively
  6. available in the Lucene index. This can be useful to explore a dataset which
  7. you don't know much about. For example, this allows creating a histogram
  8. aggregation with meaningful intervals based on the min/max range of values.
  9. The field stats api by defaults executes on all indices, but can execute on
  10. specific indices too.
  11. All indices:
  12. [source,js]
  13. --------------------------------------------------
  14. curl -XGET "http://localhost:9200/_field_stats?fields=rating"
  15. --------------------------------------------------
  16. Specific indices:
  17. [source,js]
  18. --------------------------------------------------
  19. curl -XGET "http://localhost:9200/index1,index2/_field_stats?fields=rating"
  20. --------------------------------------------------
  21. Supported request options:
  22. [horizontal]
  23. `fields`:: A list of fields to compute stats for. The field name supports wildcard notation. For example, using `text_*`
  24. will cause all fields that match the expression to be returned.
  25. `level`:: Defines if field stats should be returned on a per index level or on a
  26. cluster wide level. Valid values are `indices` and `cluster` (default).
  27. Alternatively the `fields` option can also be defined in the request body:
  28. [source,js]
  29. --------------------------------------------------
  30. curl -XPOST "http://localhost:9200/_field_stats?level=indices" -d '{
  31. "fields" : ["rating"]
  32. }'
  33. --------------------------------------------------
  34. This is equivalent to the previous request.
  35. [float]
  36. === Field statistics
  37. The field stats api is supported on string based, number based and date based fields and can return the following statistics per field:
  38. [horizontal]
  39. `max_doc`::
  40. The total number of documents.
  41. `doc_count`::
  42. The number of documents that have at least one term for this field, or -1 if
  43. this measurement isn't available on one or more shards.
  44. `density`::
  45. The percentage of documents that have at least one value for this field. This
  46. is a derived statistic and is based on the `max_doc` and `doc_count`.
  47. `sum_doc_freq`::
  48. The sum of each term's document frequency in this field, or -1 if this
  49. measurement isn't available on one or more shards.
  50. Document frequency is the number of documents containing a particular term.
  51. `sum_total_term_freq`::
  52. The sum of the term frequencies of all terms in this field across all
  53. documents, or -1 if this measurement isn't available on one or more shards.
  54. Term frequency is the total number of occurrences of a term in a particular
  55. document and field.
  56. `is_searchable`
  57. True if any of the instances of the field is searchable, false otherwise.
  58. `is_aggregatable`
  59. True if any of the instances of the field is aggregatable, false otherwise.
  60. `min_value`::
  61. The lowest value in the field.
  62. `min_value_as_string`::
  63. The lowest value in the field represented in a displayable form. All fields,
  64. but string fields returns this. (since string fields, represent values already as strings)
  65. `max_value`::
  66. The highest value in the field.
  67. `max_value_as_string`::
  68. The highest value in the field represented in a displayable form. All fields,
  69. but string fields returns this. (since string fields, represent values already as strings)
  70. NOTE: Documents marked as deleted (but not yet removed by the merge process)
  71. still affect all the mentioned statistics.
  72. [float]
  73. === Cluster level field statistics example
  74. Request:
  75. [source,js]
  76. --------------------------------------------------
  77. curl -XGET "http://localhost:9200/_field_stats?fields=rating,answer_count,creation_date,display_name"
  78. --------------------------------------------------
  79. Response:
  80. [source,js]
  81. --------------------------------------------------
  82. {
  83. "_shards": {
  84. "total": 1,
  85. "successful": 1,
  86. "failed": 0
  87. },
  88. "indices": {
  89. "_all": { <1>
  90. "fields": {
  91. "creation_date": {
  92. "max_doc": 1326564,
  93. "doc_count": 564633,
  94. "density": 42,
  95. "sum_doc_freq": 2258532,
  96. "sum_total_term_freq": -1,
  97. "min_value": "2008-08-01T16:37:51.513Z",
  98. "max_value": "2013-06-02T03:23:11.593Z",
  99. "is_searchable": "true",
  100. "is_aggregatable": "true"
  101. },
  102. "display_name": {
  103. "max_doc": 1326564,
  104. "doc_count": 126741,
  105. "density": 9,
  106. "sum_doc_freq": 166535,
  107. "sum_total_term_freq": 166616,
  108. "min_value": "0",
  109. "max_value": "정혜선",
  110. "is_searchable": "true",
  111. "is_aggregatable": "false"
  112. },
  113. "answer_count": {
  114. "max_doc": 1326564,
  115. "doc_count": 139885,
  116. "density": 10,
  117. "sum_doc_freq": 559540,
  118. "sum_total_term_freq": -1,
  119. "min_value": 0,
  120. "max_value": 160,
  121. "is_searchable": "true",
  122. "is_aggregatable": "true"
  123. },
  124. "rating": {
  125. "max_doc": 1326564,
  126. "doc_count": 437892,
  127. "density": 33,
  128. "sum_doc_freq": 1751568,
  129. "sum_total_term_freq": -1,
  130. "min_value": -14,
  131. "max_value": 1277,
  132. "is_searchable": "true",
  133. "is_aggregatable": "true"
  134. }
  135. }
  136. }
  137. }
  138. }
  139. --------------------------------------------------
  140. <1> The `_all` key indicates that it contains the field stats of all indices in the cluster.
  141. NOTE: When using the cluster level field statistics it is possible to have conflicts if the same field is used in
  142. different indices with incompatible types. For instance a field of type `long` is not compatible with a field of
  143. type `float` or `string`. A section named `conflicts` is added to the response if one or more conflicts are raised.
  144. It contains all the fields with conflicts and the reason of the incompatibility.
  145. [source,js]
  146. --------------------------------------------------
  147. {
  148. "_shards": {
  149. "total": 1,
  150. "successful": 1,
  151. "failed": 0
  152. },
  153. "indices": {
  154. "_all": {
  155. "fields": {
  156. "creation_date": {
  157. "max_doc": 1326564,
  158. "doc_count": 564633,
  159. "density": 42,
  160. "sum_doc_freq": 2258532,
  161. "sum_total_term_freq": -1,
  162. "min_value": "2008-08-01T16:37:51.513Z",
  163. "max_value": "2013-06-02T03:23:11.593Z",
  164. "is_searchable": "true",
  165. "is_aggregatable": "true"
  166. }
  167. }
  168. }
  169. },
  170. "conflicts": {
  171. "field_name_in_conflict1": "reason1",
  172. "field_name_in_conflict2": "reason2"
  173. }
  174. }
  175. --------------------------------------------------
  176. [float]
  177. ==== Indices level field statistics example
  178. Request:
  179. [source,js]
  180. --------------------------------------------------
  181. curl -XGET "http://localhost:9200/_field_stats?fields=rating,answer_count,creation_date,display_name&level=indices"
  182. --------------------------------------------------
  183. Response:
  184. [source,js]
  185. --------------------------------------------------
  186. {
  187. "_shards": {
  188. "total": 1,
  189. "successful": 1,
  190. "failed": 0
  191. },
  192. "indices": {
  193. "stack": { <1>
  194. "fields": {
  195. "creation_date": {
  196. "max_doc": 1326564,
  197. "doc_count": 564633,
  198. "density": 42,
  199. "sum_doc_freq": 2258532,
  200. "sum_total_term_freq": -1,
  201. "min_value": "2008-08-01T16:37:51.513Z",
  202. "max_value": "2013-06-02T03:23:11.593Z",
  203. "is_searchable": "true",
  204. "is_aggregatable": "true"
  205. },
  206. "display_name": {
  207. "max_doc": 1326564,
  208. "doc_count": 126741,
  209. "density": 9,
  210. "sum_doc_freq": 166535,
  211. "sum_total_term_freq": 166616,
  212. "min_value": "0",
  213. "max_value": "정혜선",
  214. "is_searchable": "true",
  215. "is_aggregatable": "false"
  216. },
  217. "answer_count": {
  218. "max_doc": 1326564,
  219. "doc_count": 139885,
  220. "density": 10,
  221. "sum_doc_freq": 559540,
  222. "sum_total_term_freq": -1,
  223. "min_value": 0,
  224. "max_value": 160,
  225. "is_searchable": "true",
  226. "is_aggregatable": "true"
  227. },
  228. "rating": {
  229. "max_doc": 1326564,
  230. "doc_count": 437892,
  231. "density": 33,
  232. "sum_doc_freq": 1751568,
  233. "sum_total_term_freq": -1,
  234. "min_value": -14,
  235. "max_value": 1277,
  236. "is_searchable": "true",
  237. "is_aggregatable": "true"
  238. }
  239. }
  240. }
  241. }
  242. }
  243. --------------------------------------------------
  244. <1> The `stack` key means it contains all field stats for the `stack` index.
  245. [float]
  246. === Field stats index constraints
  247. Field stats index constraints allows to omit all field stats for indices that don't match with the constraint. An index
  248. constraint can exclude indices' field stats based on the `min_value` and `max_value` statistic. This option is only
  249. useful if the `level` option is set to `indices`. Fields that are not indexed (not searchable) are always omitted when an index constraint is defined.
  250. For example index constraints can be useful to find out the min and max value of a particular property of your data in
  251. a time based scenario. The following request only returns field stats for the `answer_count` property for indices
  252. holding questions created in the year 2014:
  253. [source,js]
  254. --------------------------------------------------
  255. curl -XPOST "http://localhost:9200/_field_stats?level=indices" -d '{
  256. "fields" : ["answer_count"] <1>
  257. "index_constraints" : { <2>
  258. "creation_date" : { <3>
  259. "max_value" : { <4>
  260. "gte" : "2014-01-01T00:00:00.000Z"
  261. },
  262. "min_value" : { <4>
  263. "lt" : "2015-01-01T00:00:00.000Z"
  264. }
  265. }
  266. }
  267. }'
  268. --------------------------------------------------
  269. <1> The fields to compute and return field stats for.
  270. <2> The set index constraints. Note that index constrains can be defined for fields that aren't defined in the `fields` option.
  271. <3> Index constraints for the field `creation_date`.
  272. <4> Index constraints on the `max_value` and `min_value` property of a field statistic.
  273. For a field, index constraints can be defined on the `min_value` statistic, `max_value` statistic or both.
  274. Each index constraint support the following comparisons:
  275. [horizontal]
  276. `gte`:: Greater-than or equal to
  277. `gt`:: Greater-than
  278. `lte`:: Less-than or equal to
  279. `lt`:: Less-than
  280. Field stats index constraints on date fields optionally accept a `format` option, used to parse the constraint's value.
  281. If missing, the format configured in the field's mapping is used.
  282. [source,js]
  283. --------------------------------------------------
  284. curl -XPOST "http://localhost:9200/_field_stats?level=indices" -d '{
  285. "fields" : ["answer_count"]
  286. "index_constraints" : {
  287. "creation_date" : {
  288. "max_value" : {
  289. "gte" : "2014-01-01",
  290. "format" : "date_optional_time" <1>
  291. },
  292. "min_value" : {
  293. "lt" : "2015-01-01",
  294. "format" : "date_optional_time"
  295. }
  296. }
  297. }
  298. }'
  299. --------------------------------------------------
  300. <1> Custom date format