copy_to

The copy_to parameter allows you to copy the values of multiple fields into a group field, which can then be queried as a single field.

If you often search multiple fields, you can improve search speeds by using copy_to to search fewer fields. See Search as few fields as possible.

For example, the first_name and last_name fields can be copied to the full_name field as follows:

  1. PUT my-index-000001
  2. {
  3. "mappings": {
  4. "properties": {
  5. "first_name": {
  6. "type": "text",
  7. "copy_to": "full_name"
  8. },
  9. "last_name": {
  10. "type": "text",
  11. "copy_to": "full_name"
  12. },
  13. "full_name": {
  14. "type": "text"
  15. }
  16. }
  17. }
  18. }
  19. PUT my-index-000001/_doc/1
  20. {
  21. "first_name": "John",
  22. "last_name": "Smith"
  23. }
  24. GET my-index-000001/_search
  25. {
  26. "query": {
  27. "match": {
  28. "full_name": {
  29. "query": "John Smith",
  30. "operator": "and"
  31. }
  32. }
  33. }
  34. }

The values of the first_name and last_name fields are copied to the full_name field.

The first_name and last_name fields can still be queried for the first name and last name respectively, but the full_name field can be queried for both first and last names.

Some important points:

  • It is the field value which is copied, not the terms (which result from the analysis process).
  • The original _source field will not be modified to show the copied values.
  • The same value can be copied to multiple fields, with "copy_to": [ "field_1", "field_2" ]
  • You cannot copy recursively via intermediary fields such as a copy_to on field_1 to field_2 and copy_to on field_2 to field_3 expecting indexing into field_1 will eventuate in field_3, instead use copy_to directly to multiple fields from the originating field.