statistical-facet.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. [[search-facets-statistical-facet]]
  2. === Statistical Facet
  3. include::deprecated.asciidoc[]
  4. Statistical facet allows to compute statistical data on a numeric
  5. fields. The statistical data include count, total, sum of squares, mean
  6. (average), minimum, maximum, variance, and standard deviation. Here is
  7. an example:
  8. [source,js]
  9. --------------------------------------------------
  10. {
  11. "query" : {
  12. "match_all" : {}
  13. },
  14. "facets" : {
  15. "stat1" : {
  16. "statistical" : {
  17. "field" : "num1"
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. ==== Script field
  24. When using `field`, the numeric value of the field is used to compute
  25. the statistical information. Sometimes, several fields values represent
  26. the statistics we want to compute, or some sort of mathematical
  27. evaluation. The script field allows to define a
  28. <<modules-scripting,script>> to evaluate, with
  29. its value used to compute the statistical information. For example:
  30. [source,js]
  31. --------------------------------------------------
  32. {
  33. "query" : {
  34. "match_all" : {}
  35. },
  36. "facets" : {
  37. "stat1" : {
  38. "statistical" : {
  39. "script" : "doc['num1'].value + doc['num2'].value"
  40. }
  41. }
  42. }
  43. }
  44. --------------------------------------------------
  45. Parameters can also be provided to the different scripts (preferable if
  46. the script is the same, with different values for a specific parameter,
  47. like "factor"):
  48. [source,js]
  49. --------------------------------------------------
  50. {
  51. "query" : {
  52. "match_all" : {}
  53. },
  54. "facets" : {
  55. "stat1" : {
  56. "statistical" : {
  57. "script" : "(doc['num1'].value + doc['num2'].value) * factor",
  58. "params" : {
  59. "factor" : 5
  60. }
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. ==== Multi Field
  67. The statistical facet can be executed against more than one field,
  68. returning the aggregation result across those fields. For example:
  69. [source,js]
  70. --------------------------------------------------
  71. {
  72. "query" : {
  73. "match_all" : {}
  74. },
  75. "facets" : {
  76. "stat1" : {
  77. "statistical" : {
  78. "fields" : ["num1", "num2"]
  79. }
  80. }
  81. }
  82. }
  83. --------------------------------------------------
  84. ==== Memory Considerations
  85. In order to implement the statistical facet, the relevant field values
  86. are loaded into memory from the index. This means that per shard, there
  87. should be enough memory to contain them. Since by default, dynamic
  88. introduced types are `long` and `double`, one option to reduce the
  89. memory footprint is to explicitly set the types for the relevant fields
  90. to either `short`, `integer`, or `float` when possible.