sort.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. [[search-request-sort]]
  2. === Sort
  3. Allows to add one or more sort on specific fields. Each sort can be
  4. reversed as well. The sort is defined on a per field level, with special
  5. field name for `_score` to sort by score.
  6. [source,js]
  7. --------------------------------------------------
  8. {
  9. "sort" : [
  10. { "post_date" : {"order" : "asc"}},
  11. "user",
  12. { "name" : "desc" },
  13. { "age" : "desc" },
  14. "_score"
  15. ],
  16. "query" : {
  17. "term" : { "user" : "kimchy" }
  18. }
  19. }
  20. --------------------------------------------------
  21. ==== Sort Values
  22. The sort values for each document returned are also returned as part of
  23. the response.
  24. ==== Sort mode option
  25. Elasticsearch supports sorting by array or multi-valued fields. The `mode` option
  26. controls what array value is picked for sorting the document it belongs
  27. to. The `mode` option can have the following values:
  28. [horizontal]
  29. `min`:: Pick the lowest value.
  30. `max`:: Pick the highest value.
  31. `sum`:: Use the sum of all values as sort value. Only applicable for
  32. number based array fields.
  33. `avg`:: Use the average of all values as sort value. Only applicable
  34. for number based array fields.
  35. ===== Sort mode example usage
  36. In the example below the field price has multiple prices per document.
  37. In this case the result hits will be sort by price ascending based on
  38. the average price per document.
  39. [source,js]
  40. --------------------------------------------------
  41. curl -XPOST 'localhost:9200/_search' -d '{
  42. "query" : {
  43. ...
  44. },
  45. "sort" : [
  46. {"price" : {"order" : "asc", "mode" : "avg"}}
  47. ]
  48. }'
  49. --------------------------------------------------
  50. ==== Sorting within nested objects.
  51. Elasticsearch also supports sorting by
  52. fields that are inside one or more nested objects. The sorting by nested
  53. field support has the following parameters on top of the already
  54. existing sort options:
  55. `nested_path`::
  56. Defines the on what nested object to sort. The actual
  57. sort field must be a direct field inside this nested object. The default
  58. is to use the most immediate inherited nested object from the sort
  59. field.
  60. `nested_filter`::
  61. A filter the inner objects inside the nested path
  62. should match with in order for its field values to be taken into account
  63. by sorting. Common case is to repeat the query / filter inside the
  64. nested filter or query. By default no `nested_filter` is active.
  65. ===== Nested sorting example
  66. In the below example `offer` is a field of type `nested`. Because
  67. `offer` is the closest inherited nested field, it is picked as
  68. `nested_path`. Only the inner objects that have color blue will
  69. participate in sorting.
  70. [source,js]
  71. --------------------------------------------------
  72. curl -XPOST 'localhost:9200/_search' -d '{
  73. "query" : {
  74. ...
  75. },
  76. "sort" : [
  77. {
  78. "offer.price" : {
  79. "mode" : "avg",
  80. "order" : "asc",
  81. "nested_filter" : {
  82. "term" : { "offer.color" : "blue" }
  83. }
  84. }
  85. }
  86. ]
  87. }'
  88. --------------------------------------------------
  89. Nested sorting is also supported when sorting by
  90. scripts and sorting by geo distance.
  91. ==== Missing Values
  92. The `missing` parameter specifies how docs which are missing
  93. the field should be treated: The `missing` value can be
  94. set to `_last`, `_first`, or a custom value (that
  95. will be used for missing docs as the sort value). For example:
  96. [source,js]
  97. --------------------------------------------------
  98. {
  99. "sort" : [
  100. { "price" : {"missing" : "_last"} },
  101. ],
  102. "query" : {
  103. "term" : { "user" : "kimchy" }
  104. }
  105. }
  106. --------------------------------------------------
  107. NOTE: If a nested inner object doesn't match with
  108. the `nested_filter` then a missing value is used.
  109. ==== Ignoring Unmapped Fields
  110. By default, the search request will fail if there is no mapping
  111. associated with a field. The `ignore_unmapped` option allows to ignore
  112. fields that have no mapping and not sort by them. Here is an example of
  113. how it can be used:
  114. [source,js]
  115. --------------------------------------------------
  116. {
  117. "sort" : [
  118. { "price" : {"ignore_unmapped" : true} },
  119. ],
  120. "query" : {
  121. "term" : { "user" : "kimchy" }
  122. }
  123. }
  124. --------------------------------------------------
  125. ==== Geo Distance Sorting
  126. Allow to sort by `_geo_distance`. Here is an example:
  127. [source,js]
  128. --------------------------------------------------
  129. {
  130. "sort" : [
  131. {
  132. "_geo_distance" : {
  133. "pin.location" : [-70, 40],
  134. "order" : "asc",
  135. "unit" : "km"
  136. }
  137. }
  138. ],
  139. "query" : {
  140. "term" : { "user" : "kimchy" }
  141. }
  142. }
  143. --------------------------------------------------
  144. Note: the geo distance sorting supports `sort_mode` options: `min`,
  145. `max` and `avg`.
  146. The following formats are supported in providing the coordinates:
  147. ===== Lat Lon as Properties
  148. [source,js]
  149. --------------------------------------------------
  150. {
  151. "sort" : [
  152. {
  153. "_geo_distance" : {
  154. "pin.location" : {
  155. "lat" : 40,
  156. "lon" : -70
  157. },
  158. "order" : "asc",
  159. "unit" : "km"
  160. }
  161. }
  162. ],
  163. "query" : {
  164. "term" : { "user" : "kimchy" }
  165. }
  166. }
  167. --------------------------------------------------
  168. ===== Lat Lon as String
  169. Format in `lat,lon`.
  170. [source,js]
  171. --------------------------------------------------
  172. {
  173. "sort" : [
  174. {
  175. "_geo_distance" : {
  176. "pin.location" : "-70,40",
  177. "order" : "asc",
  178. "unit" : "km"
  179. }
  180. }
  181. ],
  182. "query" : {
  183. "term" : { "user" : "kimchy" }
  184. }
  185. }
  186. --------------------------------------------------
  187. ===== Geohash
  188. [source,js]
  189. --------------------------------------------------
  190. {
  191. "sort" : [
  192. {
  193. "_geo_distance" : {
  194. "pin.location" : "drm3btev3e86",
  195. "order" : "asc",
  196. "unit" : "km"
  197. }
  198. }
  199. ],
  200. "query" : {
  201. "term" : { "user" : "kimchy" }
  202. }
  203. }
  204. --------------------------------------------------
  205. ===== Lat Lon as Array
  206. Format in `[lon, lat]`, note, the order of lon/lat here in order to
  207. conform with http://geojson.org/[GeoJSON].
  208. [source,js]
  209. --------------------------------------------------
  210. {
  211. "sort" : [
  212. {
  213. "_geo_distance" : {
  214. "pin.location" : [-70, 40],
  215. "order" : "asc",
  216. "unit" : "km"
  217. }
  218. }
  219. ],
  220. "query" : {
  221. "term" : { "user" : "kimchy" }
  222. }
  223. }
  224. --------------------------------------------------
  225. ==== Script Based Sorting
  226. Allow to sort based on custom scripts, here is an example:
  227. [source,js]
  228. --------------------------------------------------
  229. {
  230. "query" : {
  231. ....
  232. },
  233. "sort" : {
  234. "_script" : {
  235. "script" : "doc['field_name'].value * factor",
  236. "type" : "number",
  237. "params" : {
  238. "factor" : 1.1
  239. },
  240. "order" : "asc"
  241. }
  242. }
  243. }
  244. --------------------------------------------------
  245. Note, it is recommended, for single custom based script based sorting,
  246. to use `custom_score` query instead as sorting based on score is faster.
  247. ==== Track Scores
  248. When sorting on a field, scores are not computed. By setting
  249. `track_scores` to true, scores will still be computed and tracked.
  250. [source,js]
  251. --------------------------------------------------
  252. {
  253. "track_scores": true,
  254. "sort" : [
  255. { "post_date" : {"reverse" : true} },
  256. { "name" : "desc" },
  257. { "age" : "desc" }
  258. ],
  259. "query" : {
  260. "term" : { "user" : "kimchy" }
  261. }
  262. }
  263. --------------------------------------------------
  264. ==== Memory Considerations
  265. When sorting, the relevant sorted field values are loaded into memory.
  266. This means that per shard, there should be enough memory to contain
  267. them. For string based types, the field sorted on should not be analyzed
  268. / tokenized. For numeric types, if possible, it is recommended to
  269. explicitly set the type to six_hun types (like `short`, `integer` and
  270. `float`).