copy_to(合并参数)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/copy-to.html#copy-to

译文链接 : copy_to(合并参数)

贡献者 : 程威ApacheCNApache中文网

copy_to 参数允许你创建自定义的 _all 字段.换句换来说,可以将多个字段的值复制到 group field(组字段),然后可以作为单个字段进行查询.例如, first_name**last_name**可以复制到 full_name字段中,如下所示 :

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": {
  5. "properties": {
  6. "first_name": {
  7. "type": "text",
  8. "copy_to": "full_name" # 1
  9. },
  10. "last_name": {
  11. "type": "text",
  12. "copy_to": "full_name" # 2
  13. },
  14. "full_name": {
  15. "type": "text"
  16. }
  17. }
  18. }
  19. }
  20. }
  21. '
  22. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
  23. {
  24. "first_name": "John",
  25. "last_name": "Smith"
  26. }
  27. '
  28. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  29. {
  30. "query": {
  31. "match": {
  32. "full_name": { # 3
  33. "query": "John Smith",
  34. "operator": "and"
  35. }
  36. }
  37. }
  38. }
  39. '

| 12 | first_name(名字)和 last_name(姓氏)字段复制到full_name 字段. | | 3 | first_name(名字)和 last_name(姓氏)字段仍然可以分别查询, full_name 可以通过 first_name(名字)和 last_name(姓氏)来查询. |

一些要点:

  • 复制的是字段值,而不是 term(词条)(由分析过程产生).
  • _source 字段不会被修改来显示复制的值.
  • 相同的值可以复制到多个字段,通过 "copy_to": [ "field_1", "field_2" ] 来操作.