Skip to content

Commit f2db0e0

Browse files
committed
Add documentation around field aliases. (#31538)
1 parent d1393ff commit f2db0e0

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

docs/reference/indices/clearcache.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ explicitly by setting `query`, `fielddata` or `request`.
1616

1717
All caches relating to a specific field(s) can also be cleared by
1818
specifying `fields` parameter with a comma delimited list of the
19-
relevant fields.
19+
relevant fields. Note that the provided names must refer to concrete
20+
fields -- objects and field aliases are not supported.
2021

2122
[float]
2223
=== Multi Index

docs/reference/mapping.asciidoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ fields to an existing index with the <<indices-put-mapping,PUT mapping API>>.
124124

125125
Other than where documented, *existing field mappings cannot be
126126
updated*. Changing the mapping would mean invalidating already indexed
127-
documents. Instead, you should create a new index with the correct mappings
128-
and <<docs-reindex,reindex>> your data into that index.
127+
documents. Instead, you should create a new index with the correct mappings
128+
and <<docs-reindex,reindex>> your data into that index. If you only wish
129+
to rename a field and not change its mappings, it may make sense to introduce
130+
an <<alias, `alias`>> field.
129131

130132
[float]
131133
== Example mapping

docs/reference/mapping/types.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ string:: <<text,`text`>> and <<keyword,`keyword`>>
4040

4141
<<parent-join>>:: Defines parent/child relation for documents within the same index
4242

43+
<<alias>>:: Defines an alias to an existing field.
44+
4345
<<feature>>:: Record numeric features to boost hits at query time.
4446

4547
<<feature-vector>>:: Record numeric feature vectors to boost hits at query time.
@@ -58,6 +60,8 @@ the <<analysis-standard-analyzer,`standard` analyzer>>, the
5860
This is the purpose of _multi-fields_. Most datatypes support multi-fields
5961
via the <<multi-fields>> parameter.
6062

63+
include::types/alias.asciidoc[]
64+
6165
include::types/array.asciidoc[]
6266

6367
include::types/binary.asciidoc[]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)