2015-08-06 11:24:29 -04:00
|
|
|
[[multi-fields]]
|
|
|
|
=== `fields`
|
|
|
|
|
|
|
|
It is often useful to index the same field in different ways for different
|
|
|
|
purposes. This is the purpose of _multi-fields_. For instance, a `string`
|
2016-03-18 12:01:27 -04:00
|
|
|
field could be mapped as a `text` field for full-text
|
|
|
|
search, and as a `keyword` field for sorting or aggregations:
|
2015-08-06 11:24:29 -04:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"city": {
|
|
|
|
"type": "text",
|
|
|
|
"fields": {
|
|
|
|
"raw": { <1>
|
|
|
|
"type": "keyword"
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"city": "New York"
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/2
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"city": "York"
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
GET my-index-000001/_search
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"city": "york" <2>
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sort": {
|
|
|
|
"city.raw": "asc" <3>
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"Cities": {
|
|
|
|
"terms": {
|
|
|
|
"field": "city.raw" <3>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2016-03-18 12:01:27 -04:00
|
|
|
<1> The `city.raw` field is a `keyword` version of the `city` field.
|
|
|
|
<2> The `city` field can be used for full text search.
|
2015-08-06 11:24:29 -04:00
|
|
|
<3> The `city.raw` field can be used for sorting and aggregations
|
|
|
|
|
|
|
|
NOTE: Multi-fields do not change the original `_source` field.
|
|
|
|
|
2019-06-20 03:15:43 -04:00
|
|
|
TIP: New multi-fields can be added to existing
|
2015-08-12 15:21:37 -04:00
|
|
|
fields using the <<indices-put-mapping,PUT mapping API>>.
|
|
|
|
|
2015-08-06 11:24:29 -04:00
|
|
|
==== Multi-fields with multiple analyzers
|
|
|
|
|
|
|
|
Another use case of multi-fields is to analyze the same field in different
|
|
|
|
ways for better relevance. For instance we could index a field with the
|
|
|
|
<<analysis-standard-analyzer,`standard` analyzer>> which breaks text up into
|
|
|
|
words, and again with the <<english-analyzer,`english` analyzer>>
|
|
|
|
which stems words into their root form:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2015-08-06 11:24:29 -04:00
|
|
|
--------------------------------------------------
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"text": { <1>
|
|
|
|
"type": "text",
|
|
|
|
"fields": {
|
|
|
|
"english": { <2>
|
|
|
|
"type": "text",
|
|
|
|
"analyzer": "english"
|
2015-08-06 11:24:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/1
|
2015-08-06 11:24:29 -04:00
|
|
|
{ "text": "quick brown fox" } <3>
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
PUT my-index-000001/_doc/2
|
2015-08-06 11:24:29 -04:00
|
|
|
{ "text": "quick brown foxes" } <3>
|
|
|
|
|
2020-07-27 15:58:26 -04:00
|
|
|
GET my-index-000001/_search
|
2015-08-06 11:24:29 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"multi_match": {
|
|
|
|
"query": "quick brown foxes",
|
|
|
|
"fields": [ <4>
|
|
|
|
"text",
|
|
|
|
"text.english"
|
|
|
|
],
|
|
|
|
"type": "most_fields" <4>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
<1> The `text` field uses the `standard` analyzer.
|
|
|
|
<2> The `text.english` field uses the `english` analyzer.
|
|
|
|
<3> Index two documents, one with `fox` and the other with `foxes`.
|
|
|
|
<4> Query both the `text` and `text.english` fields and combine the scores.
|
|
|
|
|
|
|
|
The `text` field contains the term `fox` in the first document and `foxes` in
|
|
|
|
the second document. The `text.english` field contains `fox` for both
|
|
|
|
documents, because `foxes` is stemmed to `fox`.
|
|
|
|
|
|
|
|
The query string is also analyzed by the `standard` analyzer for the `text`
|
2018-11-20 04:08:32 -05:00
|
|
|
field, and by the `english` analyzer for the `text.english` field. The
|
2015-08-06 11:24:29 -04:00
|
|
|
stemmed field allows a query for `foxes` to also match the document containing
|
|
|
|
just `fox`. This allows us to match as many documents as possible. By also
|
|
|
|
querying the unstemmed `text` field, we improve the relevance score of the
|
|
|
|
document which matches `foxes` exactly.
|