Elasticsearch 根据 _id 排序

x33g5p2x  于2021-12-06 转载在 ElasticSearch  
字(8.0k)|赞(0)|评价(0)|浏览(551)

我这里有一个测试索引数据如下:

如果用普通的排序方法对_id中的数据进行排序的话:

  1. GET _search
  2. {
  3. "query": {
  4. "term": {
  5. "_index": "test"
  6. }
  7. },
  8. "sort": {
  9. "_id": {
  10. "order": "asc"
  11. }
  12. },
  13. "size": 20
  14. }

因为_id的数据为字符串类型,因此排序出来的结果其实并不是我们想要的:

  1. {
  2. "took" : 5,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 10,
  6. "successful" : 10,
  7. "skipped" : 9,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 19,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [
  17. {
  18. "_index" : "test",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : null,
  22. "_source" : { },
  23. "sort" : [
  24. "1"
  25. ]
  26. },
  27. {
  28. "_index" : "test",
  29. "_type" : "_doc",
  30. "_id" : "10",
  31. "_score" : null,
  32. "_source" : { },
  33. "sort" : [
  34. "10"
  35. ]
  36. },
  37. {
  38. "_index" : "test",
  39. "_type" : "_doc",
  40. "_id" : "11",
  41. "_score" : null,
  42. "_source" : { },
  43. "sort" : [
  44. "11"
  45. ]
  46. },
  47. {
  48. "_index" : "test",
  49. "_type" : "_doc",
  50. "_id" : "12",
  51. "_score" : null,
  52. "_source" : { },
  53. "sort" : [
  54. "12"
  55. ]
  56. },
  57. {
  58. "_index" : "test",
  59. "_type" : "_doc",
  60. "_id" : "13",
  61. "_score" : null,
  62. "_source" : { },
  63. "sort" : [
  64. "13"
  65. ]
  66. },
  67. {
  68. "_index" : "test",
  69. "_type" : "_doc",
  70. "_id" : "14",
  71. "_score" : null,
  72. "_source" : { },
  73. "sort" : [
  74. "14"
  75. ]
  76. },
  77. {
  78. "_index" : "test",
  79. "_type" : "_doc",
  80. "_id" : "15",
  81. "_score" : null,
  82. "_source" : { },
  83. "sort" : [
  84. "15"
  85. ]
  86. },
  87. {
  88. "_index" : "test",
  89. "_type" : "_doc",
  90. "_id" : "16",
  91. "_score" : null,
  92. "_source" : { },
  93. "sort" : [
  94. "16"
  95. ]
  96. },
  97. {
  98. "_index" : "test",
  99. "_type" : "_doc",
  100. "_id" : "17",
  101. "_score" : null,
  102. "_source" : { },
  103. "sort" : [
  104. "17"
  105. ]
  106. },
  107. {
  108. "_index" : "test",
  109. "_type" : "_doc",
  110. "_id" : "18",
  111. "_score" : null,
  112. "_source" : { },
  113. "sort" : [
  114. "18"
  115. ]
  116. },
  117. {
  118. "_index" : "test",
  119. "_type" : "_doc",
  120. "_id" : "19",
  121. "_score" : null,
  122. "_source" : { },
  123. "sort" : [
  124. "19"
  125. ]
  126. },
  127. {
  128. "_index" : "test",
  129. "_type" : "_doc",
  130. "_id" : "2",
  131. "_score" : null,
  132. "_source" : { },
  133. "sort" : [
  134. "2"
  135. ]
  136. },
  137. {
  138. "_index" : "test",
  139. "_type" : "_doc",
  140. "_id" : "3",
  141. "_score" : null,
  142. "_source" : { },
  143. "sort" : [
  144. "3"
  145. ]
  146. },
  147. {
  148. "_index" : "test",
  149. "_type" : "_doc",
  150. "_id" : "4",
  151. "_score" : null,
  152. "_source" : { },
  153. "sort" : [
  154. "4"
  155. ]
  156. },
  157. {
  158. "_index" : "test",
  159. "_type" : "_doc",
  160. "_id" : "5",
  161. "_score" : null,
  162. "_source" : { },
  163. "sort" : [
  164. "5"
  165. ]
  166. },
  167. {
  168. "_index" : "test",
  169. "_type" : "_doc",
  170. "_id" : "6",
  171. "_score" : null,
  172. "_source" : { },
  173. "sort" : [
  174. "6"
  175. ]
  176. },
  177. {
  178. "_index" : "test",
  179. "_type" : "_doc",
  180. "_id" : "7",
  181. "_score" : null,
  182. "_source" : { },
  183. "sort" : [
  184. "7"
  185. ]
  186. },
  187. {
  188. "_index" : "test",
  189. "_type" : "_doc",
  190. "_id" : "8",
  191. "_score" : null,
  192. "_source" : { },
  193. "sort" : [
  194. "8"
  195. ]
  196. },
  197. {
  198. "_index" : "test",
  199. "_type" : "_doc",
  200. "_id" : "9",
  201. "_score" : null,
  202. "_source" : { },
  203. "sort" : [
  204. "9"
  205. ]
  206. }
  207. ]
  208. }
  209. }

他会先对字符串的第一个字符进行排序,在依次对后面的字符进行排序。

想要对 String 类型的 _id 进行数字顺序排序,就需要用到排序脚本:

因此可以这样写:

  1. GET _search
  2. {
  3. "query": {
  4. "term": {
  5. "_index": "test"
  6. }
  7. },
  8. "sort": {
  9. "_script": {
  10. "type": "number",
  11. "script": "Integer.parseInt(doc['_id'].value)",
  12. "order": "asc"
  13. }
  14. },
  15. "size": 20
  16. }

看看结果:

  1. {
  2. "took" : 5,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 10,
  6. "successful" : 10,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 19,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [
  17. {
  18. "_index" : "test",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : null,
  22. "_source" : { },
  23. "sort" : [
  24. 1.0
  25. ]
  26. },
  27. {
  28. "_index" : "test",
  29. "_type" : "_doc",
  30. "_id" : "2",
  31. "_score" : null,
  32. "_source" : { },
  33. "sort" : [
  34. 2.0
  35. ]
  36. },
  37. {
  38. "_index" : "test",
  39. "_type" : "_doc",
  40. "_id" : "3",
  41. "_score" : null,
  42. "_source" : { },
  43. "sort" : [
  44. 3.0
  45. ]
  46. },
  47. {
  48. "_index" : "test",
  49. "_type" : "_doc",
  50. "_id" : "4",
  51. "_score" : null,
  52. "_source" : { },
  53. "sort" : [
  54. 4.0
  55. ]
  56. },
  57. {
  58. "_index" : "test",
  59. "_type" : "_doc",
  60. "_id" : "5",
  61. "_score" : null,
  62. "_source" : { },
  63. "sort" : [
  64. 5.0
  65. ]
  66. },
  67. {
  68. "_index" : "test",
  69. "_type" : "_doc",
  70. "_id" : "6",
  71. "_score" : null,
  72. "_source" : { },
  73. "sort" : [
  74. 6.0
  75. ]
  76. },
  77. {
  78. "_index" : "test",
  79. "_type" : "_doc",
  80. "_id" : "7",
  81. "_score" : null,
  82. "_source" : { },
  83. "sort" : [
  84. 7.0
  85. ]
  86. },
  87. {
  88. "_index" : "test",
  89. "_type" : "_doc",
  90. "_id" : "8",
  91. "_score" : null,
  92. "_source" : { },
  93. "sort" : [
  94. 8.0
  95. ]
  96. },
  97. {
  98. "_index" : "test",
  99. "_type" : "_doc",
  100. "_id" : "9",
  101. "_score" : null,
  102. "_source" : { },
  103. "sort" : [
  104. 9.0
  105. ]
  106. },
  107. {
  108. "_index" : "test",
  109. "_type" : "_doc",
  110. "_id" : "10",
  111. "_score" : null,
  112. "_source" : { },
  113. "sort" : [
  114. 10.0
  115. ]
  116. },
  117. {
  118. "_index" : "test",
  119. "_type" : "_doc",
  120. "_id" : "11",
  121. "_score" : null,
  122. "_source" : { },
  123. "sort" : [
  124. 11.0
  125. ]
  126. },
  127. {
  128. "_index" : "test",
  129. "_type" : "_doc",
  130. "_id" : "12",
  131. "_score" : null,
  132. "_source" : { },
  133. "sort" : [
  134. 12.0
  135. ]
  136. },
  137. {
  138. "_index" : "test",
  139. "_type" : "_doc",
  140. "_id" : "13",
  141. "_score" : null,
  142. "_source" : { },
  143. "sort" : [
  144. 13.0
  145. ]
  146. },
  147. {
  148. "_index" : "test",
  149. "_type" : "_doc",
  150. "_id" : "14",
  151. "_score" : null,
  152. "_source" : { },
  153. "sort" : [
  154. 14.0
  155. ]
  156. },
  157. {
  158. "_index" : "test",
  159. "_type" : "_doc",
  160. "_id" : "15",
  161. "_score" : null,
  162. "_source" : { },
  163. "sort" : [
  164. 15.0
  165. ]
  166. },
  167. {
  168. "_index" : "test",
  169. "_type" : "_doc",
  170. "_id" : "16",
  171. "_score" : null,
  172. "_source" : { },
  173. "sort" : [
  174. 16.0
  175. ]
  176. },
  177. {
  178. "_index" : "test",
  179. "_type" : "_doc",
  180. "_id" : "17",
  181. "_score" : null,
  182. "_source" : { },
  183. "sort" : [
  184. 17.0
  185. ]
  186. },
  187. {
  188. "_index" : "test",
  189. "_type" : "_doc",
  190. "_id" : "18",
  191. "_score" : null,
  192. "_source" : { },
  193. "sort" : [
  194. 18.0
  195. ]
  196. },
  197. {
  198. "_index" : "test",
  199. "_type" : "_doc",
  200. "_id" : "19",
  201. "_score" : null,
  202. "_source" : { },
  203. "sort" : [
  204. 19.0
  205. ]
  206. }
  207. ]
  208. }
  209. }

搞定!

相关文章

最新文章

更多