Copy_to

The copy_to parameter allows you to copy the values of multiple fields into a single field. This parameter can be useful if you often search across multiple fields because it allows you to search the group field instead.

Only the field value is copied and not the terms resulting from the analysis process. The original _source field remains unmodified, and the same value can be copied to multiple fields using the copy_to parameter. However, recursive copying through intermediary fields is not supported; instead, use copy_to directly from the originating field to multiple target fields.


Examples

The following example uses the copy_to parameter to search for products by their name and description and copy those values into a single field:

  1. PUT my-products-index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name": {
  6. "type": "text",
  7. "copy_to": "product_info"
  8. },
  9. "description": {
  10. "type": "text",
  11. "copy_to": "product_info"
  12. },
  13. "product_info": {
  14. "type": "text"
  15. },
  16. "price": {
  17. "type": "float"
  18. }
  19. }
  20. }
  21. }
  22. PUT my-products-index/_doc/1
  23. {
  24. "name": "Wireless Headphones",
  25. "description": "High-quality wireless headphones with noise cancellation",
  26. "price": 99.99
  27. }
  28. PUT my-products-index/_doc/2
  29. {
  30. "name": "Bluetooth Speaker",
  31. "description": "Portable Bluetooth speaker with long battery life",
  32. "price": 49.99
  33. }

copy

In this example, the values from the name and description fields are copied into the product_info field. You can now search for products by querying the product_info field, as follows:

  1. GET my-products-index/_search
  2. {
  3. "query": {
  4. "match": {
  5. "product_info": "wireless headphones"
  6. }
  7. }
  8. }

copy

Response

  1. {
  2. "took": 20,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.9061546,
  16. "hits": [
  17. {
  18. "_index": "my-products-index",
  19. "_id": "1",
  20. "_score": 1.9061546,
  21. "_source": {
  22. "name": "Wireless Headphones",
  23. "description": "High-quality wireless headphones with noise cancellation",
  24. "price": 99.99
  25. }
  26. }
  27. ]
  28. }
  29. }