|
| 1 | +[[alias]] |
| 2 | +=== Alias datatype |
| 3 | + |
| 4 | +An `alias` mapping defines an alternate name for a field in the index. |
| 5 | +The alias can be used in place of the target field in <<search, search>> requests, |
| 6 | +and selected other APIs like <<search-field-caps, field capabilities>>. |
| 7 | + |
| 8 | +[source,js] |
| 9 | +-------------------------------- |
| 10 | +PUT trips |
| 11 | +{ |
| 12 | + "mappings": { |
| 13 | + "_doc": { |
| 14 | + "properties": { |
| 15 | + "distance": { |
| 16 | + "type": "long" |
| 17 | + }, |
| 18 | + "route_length_miles": { |
| 19 | + "type": "alias", |
| 20 | + "path": "distance" // <1> |
| 21 | + }, |
| 22 | + "transit_mode": { |
| 23 | + "type": "keyword" |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | +
|
| 30 | +GET _search |
| 31 | +{ |
| 32 | + "query": { |
| 33 | + "range" : { |
| 34 | + "route_length_miles" : { |
| 35 | + "gte" : 39 |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +-------------------------------- |
| 41 | +// CONSOLE |
| 42 | + |
| 43 | +<1> The path to the target field. Note that this must be the full path, including any parent |
| 44 | +objects (e.g. `object1.object2.field`). |
| 45 | + |
| 46 | +Almost all components of the search request accept field aliases. In particular, aliases can be |
| 47 | +used in queries, aggregations, and sort fields, as well as when requesting `docvalue_fields`, |
| 48 | +`stored_fields`, suggestions, and highlights. Scripts also support aliases when accessing |
| 49 | +field values. Please see the section on <<unsupported-apis, unsupported APIs>> for exceptions. |
| 50 | + |
| 51 | +In some parts of the search request and when requesting field capabilities, field wildcard patterns can be |
| 52 | +provided. In these cases, the wildcard pattern will match field aliases in addition to concrete fields: |
| 53 | + |
| 54 | +[source,js] |
| 55 | +-------------------------------- |
| 56 | +GET trips/_field_caps?fields=route_*,transit_mode |
| 57 | +-------------------------------- |
| 58 | +// CONSOLE |
| 59 | +// TEST[continued] |
| 60 | + |
| 61 | +[[alias-targets]] |
| 62 | +==== Alias targets |
| 63 | + |
| 64 | +There are a few restrictions on the target of an alias: |
| 65 | + |
| 66 | + * The target must be a concrete field, and not an object or another field alias. |
| 67 | + * The target field must exist at the time the alias is created. |
| 68 | + * If nested objects are defined, a field alias must have the same nested scope as its target. |
| 69 | + |
| 70 | +Additionally, a field alias can only have one target. This means that it is not possible to use a |
| 71 | +field alias to query over multiple target fields in a single clause. |
| 72 | + |
| 73 | +[[unsupported-apis]] |
| 74 | +==== Unsupported APIs |
| 75 | + |
| 76 | +Writes to field aliases are not supported: attempting to use an alias in an index or update request |
| 77 | +will result in a failure. |
| 78 | + |
| 79 | +Because alias names are not present in the document source, aliases cannot be used when performing |
| 80 | +source filtering. For example, the following request will return an empty result for `_source`: |
| 81 | + |
| 82 | +[source,js] |
| 83 | +-------------------------------- |
| 84 | +GET /_search |
| 85 | +{ |
| 86 | + "query" : { |
| 87 | + "match_all": {} |
| 88 | + }, |
| 89 | + "_source": "route_length_miles" |
| 90 | +} |
| 91 | +-------------------------------- |
| 92 | +// CONSOLE |
| 93 | +// TEST[continued] |
| 94 | + |
| 95 | +Finally, currently only the search and field capabilities APIs will accept and resolve |
| 96 | +field aliases. Other APIs that accept field names, such as <<docs-termvectors, term vectors>>, |
| 97 | +cannot be used with field aliases. |
0 commit comments