copy-to.asciidoc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "_doc": {
  13. "properties": {
  14. "first_name": {
  15. "type": "text",
  16. "copy_to": "full_name" <1>
  17. },
  18. "last_name": {
  19. "type": "text",
  20. "copy_to": "full_name" <1>
  21. },
  22. "full_name": {
  23. "type": "text"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. PUT my_index/_doc/1
  30. {
  31. "first_name": "John",
  32. "last_name": "Smith"
  33. }
  34. GET my_index/_search
  35. {
  36. "query": {
  37. "match": {
  38. "full_name": { <2>
  39. "query": "John Smith",
  40. "operator": "and"
  41. }
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // CONSOLE
  47. <1> The values of the `first_name` and `last_name` fields are copied to the
  48. `full_name` field.
  49. <2> The `first_name` and `last_name` fields can still be queried for the
  50. first name and last name respectively, but the `full_name` field can be
  51. queried for both first and last names.
  52. Some important points:
  53. * It is the field _value_ which is copied, not the terms (which result from the analysis process).
  54. * The original <<mapping-source-field,`_source`>> field will not be modified to show the copied values.
  55. * The same value can be copied to multiple fields, with `"copy_to": [ "field_1", "field_2" ]`