index-sorting.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. [[index-modules-index-sorting]]
  2. == Index Sorting
  3. When creating a new index in Elasticsearch it is possible to configure how the Segments
  4. inside each Shard will be sorted. By default Lucene does not apply any sort.
  5. The `index.sort.*` settings define which fields should be used to sort the documents inside each Segment.
  6. [WARNING]
  7. nested fields are not compatible with index sorting because they rely on the assumption
  8. that nested documents are stored in contiguous doc ids, which can be broken by index sorting.
  9. An error will be thrown if index sorting is activated on an index that contains nested fields.
  10. For instance the following example shows how to define a sort on a single field:
  11. [source,js]
  12. --------------------------------------------------
  13. PUT twitter
  14. {
  15. "settings" : {
  16. "index" : {
  17. "sort.field" : "date", <1>
  18. "sort.order" : "desc" <2>
  19. }
  20. },
  21. "mappings": {
  22. "properties": {
  23. "date": {
  24. "type": "date"
  25. }
  26. }
  27. }
  28. }
  29. --------------------------------------------------
  30. // CONSOLE
  31. <1> This index is sorted by the `date` field
  32. <2> ... in descending order.
  33. It is also possible to sort the index by more than one field:
  34. [source,js]
  35. --------------------------------------------------
  36. PUT twitter
  37. {
  38. "settings" : {
  39. "index" : {
  40. "sort.field" : ["username", "date"], <1>
  41. "sort.order" : ["asc", "desc"] <2>
  42. }
  43. },
  44. "mappings": {
  45. "properties": {
  46. "username": {
  47. "type": "keyword",
  48. "doc_values": true
  49. },
  50. "date": {
  51. "type": "date"
  52. }
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. // CONSOLE
  58. <1> This index is sorted by `username` first then by `date`
  59. <2> ... in ascending order for the `username` field and in descending order for the `date` field.
  60. Index sorting supports the following settings:
  61. `index.sort.field`::
  62. The list of fields used to sort the index.
  63. Only `boolean`, `numeric`, `date` and `keyword` fields with `doc_values` are allowed here.
  64. `index.sort.order`::
  65. The sort order to use for each field.
  66. The order option can have the following values:
  67. * `asc`: For ascending order
  68. * `desc`: For descending order.
  69. `index.sort.mode`::
  70. Elasticsearch supports sorting by multi-valued fields.
  71. The mode option controls what value is picked to sort the document.
  72. The mode option can have the following values:
  73. * `min`: Pick the lowest value.
  74. * `max`: Pick the highest value.
  75. `index.sort.missing`::
  76. The missing parameter specifies how docs which are missing the field should be treated.
  77. The missing value can have the following values:
  78. * `_last`: Documents without value for the field are sorted last.
  79. * `_first`: Documents without value for the field are sorted first.
  80. [WARNING]
  81. Index sorting can be defined only once at index creation. It is not allowed to add or update
  82. a sort on an existing index. Index sorting also has a cost in terms of indexing throughput since
  83. documents must be sorted at flush and merge time. You should test the impact on your application
  84. before activating this feature.
  85. [float]
  86. [[early-terminate]]
  87. === Early termination of search request
  88. By default in Elasticsearch a search request must visit every document that match a query to
  89. retrieve the top documents sorted by a specified sort.
  90. Though when the index sort and the search sort are the same it is possible to limit
  91. the number of documents that should be visited per segment to retrieve the N top ranked documents globally.
  92. For example, let's say we have an index that contains events sorted by a timestamp field:
  93. [source,js]
  94. --------------------------------------------------
  95. PUT events
  96. {
  97. "settings" : {
  98. "index" : {
  99. "sort.field" : "timestamp",
  100. "sort.order" : "desc" <1>
  101. }
  102. },
  103. "mappings": {
  104. "properties": {
  105. "timestamp": {
  106. "type": "date"
  107. }
  108. }
  109. }
  110. }
  111. --------------------------------------------------
  112. // CONSOLE
  113. <1> This index is sorted by timestamp in descending order (most recent first)
  114. You can search for the last 10 events with:
  115. [source,js]
  116. --------------------------------------------------
  117. GET /events/_search
  118. {
  119. "size": 10,
  120. "sort": [
  121. { "timestamp": "desc" }
  122. ]
  123. }
  124. --------------------------------------------------
  125. // CONSOLE
  126. // TEST[continued]
  127. Elasticsearch will detect that the top docs of each segment are already sorted in the index
  128. and will only compare the first N documents per segment.
  129. The rest of the documents matching the query are collected to count the total number of results
  130. and to build aggregations.
  131. If you're only looking for the last 10 events and have no interest in
  132. the total number of documents that match the query you can set `track_total_hits`
  133. to false:
  134. [source,js]
  135. --------------------------------------------------
  136. GET /events/_search
  137. {
  138. "size": 10,
  139. "sort": [ <1>
  140. { "timestamp": "desc" }
  141. ],
  142. "track_total_hits": false
  143. }
  144. --------------------------------------------------
  145. // CONSOLE
  146. // TEST[continued]
  147. <1> The index sort will be used to rank the top documents and each segment will early terminate the collection after the first 10 matches.
  148. This time, Elasticsearch will not try to count the number of documents and will be able to terminate the query
  149. as soon as N documents have been collected per segment.
  150. [source,js]
  151. --------------------------------------------------
  152. {
  153. "_shards": ...
  154. "hits" : { <1>
  155. "max_score" : null,
  156. "hits" : []
  157. },
  158. "took": 20,
  159. "timed_out": false
  160. }
  161. --------------------------------------------------
  162. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
  163. // TESTRESPONSE[s/"took": 20,/"took": "$body.took",/]
  164. <1> The total number of hits matching the query is unknown because of early termination.
  165. NOTE: Aggregations will collect all documents that match the query regardless
  166. of the value of `track_total_hits`
  167. [[index-modules-index-sorting-conjunctions]]
  168. === Use index sorting to speed up conjunctions
  169. Index sorting can be useful in order to organize Lucene doc ids (not to be
  170. conflated with `_id`) in a way that makes conjunctions (a AND b AND ...) more
  171. efficient. In order to be efficient, conjunctions rely on the fact that if any
  172. clause does not match, then the entire conjunction does not match. By using
  173. index sorting, we can put documents that do not match together, which will
  174. help skip efficiently over large ranges of doc IDs that do not match the
  175. conjunction.
  176. This trick only works with low-cardinality fields. A rule of thumb is that
  177. you should sort first on fields that both have a low cardinality and are
  178. frequently used for filtering. The sort order (`asc` or `desc`) does not
  179. matter as we only care about putting values that would match the same clauses
  180. close to each other.
  181. For instance if you were indexing cars for sale, it might be interesting to
  182. sort by fuel type, body type, make, year of registration and finally mileage.