copy-to.asciidoc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. [[copy-to]]
  2. === `copy_to`
  3. The `copy_to` parameter allows you to copy the values of multiple
  4. fields into a group field, which can then be queried as a single
  5. field. For instance, the `first_name` and `last_name` fields can be copied to
  6. the `full_name` field as follows:
  7. [source,js]
  8. --------------------------------------------------
  9. PUT my_index
  10. {
  11. "mappings": {
  12. "properties": {
  13. "first_name": {
  14. "type": "text",
  15. "copy_to": "full_name" <1>
  16. },
  17. "last_name": {
  18. "type": "text",
  19. "copy_to": "full_name" <1>
  20. },
  21. "full_name": {
  22. "type": "text"
  23. }
  24. }
  25. }
  26. }
  27. PUT my_index/_doc/1
  28. {
  29. "first_name": "John",
  30. "last_name": "Smith"
  31. }
  32. GET my_index/_search
  33. {
  34. "query": {
  35. "match": {
  36. "full_name": { <2>
  37. "query": "John Smith",
  38. "operator": "and"
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. // CONSOLE
  45. <1> The values of the `first_name` and `last_name` fields are copied to the
  46. `full_name` field.
  47. <2> The `first_name` and `last_name` fields can still be queried for the
  48. first name and last name respectively, but the `full_name` field can be
  49. queried for both first and last names.
  50. Some important points:
  51. * It is the field _value_ which is copied, not the terms (which result from the analysis process).
  52. * The original <<mapping-source-field,`_source`>> field will not be modified to show the copied values.
  53. * The same value can be copied to multiple fields, with `"copy_to": [ "field_1", "field_2" ]`
  54. * You cannot copy recursively via intermediary fields such as a `copy_to` on
  55. `field_1` to `field_2` and `copy_to` on `field_2` to `field_3` expecting
  56. indexing into `field_1` will eventuate in `field_3`, instead use copy_to
  57. directly to multiple fields from the originating field.