_uid field

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-uid-field.html

译文链接 : http://www.apache.wiki/display/Elasticsearch/_uid+field

贡献者 : 朱彦安ApacheCNApache中文网

每个索引的文档都与一个 _type(请参见“Mapping Typesedit”一节)和一个 _id 相关联。

这些值组合为 {type}#{id} 并作为 _uid 字段编入索引。

_uid字段的值可以在 queries(查询),aggregations(聚合),scripts(脚本)以及 sorting(排序)时访问 :

  1. # Example documents
  2. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  3. {
  4. "text": "Document with ID 1"
  5. }
  6. '
  7. curl -XPUT 'localhost:9200/my_index/my_type/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
  8. {
  9. "text": "Document with ID 2"
  10. }
  11. '
  12. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  13. {
  14. "query": {
  15. "terms": {
  16. "_uid": [ "my_type#1", "my_type#2" ] # 1
  17. }
  18. },
  19. "aggs": {
  20. "UIDs": {
  21. "terms": {
  22. "field": "_uid", # 2
  23. "size": 10
  24. }
  25. }
  26. },
  27. "sort": [
  28. {
  29. "_uid": { # 3
  30. "order": "desc"
  31. }
  32. }
  33. ],
  34. "script_fields": {
  35. "UID": {
  36. "script": {
  37. "lang": "painless",
  38. "inline": "doc['_uid']" # 4
  39. }
  40. }
  41. }
  42. }
  43. '

| 1 | 在 _uid 字段上查询 (也可以参考 ids query) | | 2 | 在 _uid 字段上聚合 | | 3 | 在 _uid 字段上排序 | | 4 | 在脚本中访问 _uid 字段 |