range-facet.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. [[search-facets-range-facet]]
  2. === Range Facets
  3. include::deprecated.asciidoc[]
  4. `range` facet allows to specify a set of ranges and get both the number
  5. of docs (count) that fall within each range, and aggregated data either
  6. based on the field, or using another field. Here is a simple example:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "query" : {
  11. "match_all" : {}
  12. },
  13. "facets" : {
  14. "range1" : {
  15. "range" : {
  16. "field" : "field_name",
  17. "ranges" : [
  18. { "to" : 50 },
  19. { "from" : 20, "to" : 70 },
  20. { "from" : 70, "to" : 120 },
  21. { "from" : 150 }
  22. ]
  23. }
  24. }
  25. }
  26. }
  27. --------------------------------------------------
  28. Another option which is a bit more DSL enabled is to provide the ranges
  29. on the actual field name, for example:
  30. [source,js]
  31. --------------------------------------------------
  32. {
  33. "query" : {
  34. "match_all" : {}
  35. },
  36. "facets" : {
  37. "range1" : {
  38. "range" : {
  39. "my_field" : [
  40. { "to" : 50 },
  41. { "from" : 20, "to" : 70 },
  42. { "from" : 70, "to" : 120 },
  43. { "from" : 150 }
  44. ]
  45. }
  46. }
  47. }
  48. }
  49. --------------------------------------------------
  50. The `range` facet always includes the `from` parameter and excludes the
  51. `to` parameter for each range.
  52. ==== Key and Value
  53. The `range` facet allows to use a different field to check if its value
  54. falls within a range, and another field to compute aggregated data per
  55. range (like total). For example:
  56. [source,js]
  57. --------------------------------------------------
  58. {
  59. "query" : {
  60. "match_all" : {}
  61. },
  62. "facets" : {
  63. "range1" : {
  64. "range" : {
  65. "key_field" : "field_name",
  66. "value_field" : "another_field_name",
  67. "ranges" : [
  68. { "to" : 50 },
  69. { "from" : 20, "to" : 70 },
  70. { "from" : 70, "to" : 120 },
  71. { "from" : 150 }
  72. ]
  73. }
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. ==== Script Key and Value
  79. Sometimes, some munging of both the key and the value are needed. In the
  80. key case, before it is checked if it falls within a range, and for the
  81. value, when the statistical data is computed per range scripts can be
  82. used. Here is an example:
  83. [source,js]
  84. --------------------------------------------------
  85. {
  86. "query" : {
  87. "match_all" : {}
  88. },
  89. "facets" : {
  90. "range1" : {
  91. "range" : {
  92. "key_script" : "doc['date'].date.minuteOfHour",
  93. "value_script" : "doc['num1'].value",
  94. "ranges" : [
  95. { "to" : 50 },
  96. { "from" : 20, "to" : 70 },
  97. { "from" : 70, "to" : 120 },
  98. { "from" : 150 }
  99. ]
  100. }
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. ==== Date Ranges
  106. The range facet support also providing the range as string formatted
  107. dates.