Detailed Explanation of Elasticsearch Mapping Parameters fields
- 2021-10-25 06:52:21
- OfStack
Elasticsearch mapping parameter fields
fields
It is often useful to index the same fields in different ways for different purposes. This is also the purpose of multi-fields. For example, a string field can be mapped to an text field for full-text search, or to an keyword field for sorting or aggregation.
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"city": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
}
note
The city. raw field is the keyword version of the city field.
GET my_index/_search
{
"query": {
"match": {
"city": "york"
}
},
"sort": {
"city.raw": "asc"
},
"aggs": {
"Cities": {
"terms": {
"field": "city.raw"
}
}
}
}
note
The city field is used for full-text searches.
note
: city. raw for sorting and aggregation.
Multiple fields cannot modify the original _ source field.
The fields setting allows for different settings for fields with the same name in the same index. You can use the PUT mapping API to add a new multi-field to an existing field.
Multiple Fields with Multiple Analyses
Another application scenario for multiple fields is to analyze the same fields in different ways for better correlation.
PUT my_index
{
"mappings": {
"_doc": {
"properties": {
"text": {
"type": "text",
"fields": {
"english": {
"type": "text",
"analyzer": "english"
}
}
}
}
}
}
}
note
The text. field field uses the english parser.
Implementation of fields by elasticsearch Annotation
mapping effect:
"label": {
"type": "keyword",
"fields": {
"IKS": {
"type": "text",
"analyzer": "ikIndexAnalyzer"
}
}
}
@Column(name = " Label ")
@MultiField(
mainField = @Field(type = FieldType.Keyword),
otherFields = {
@InnerField(suffix = "IKS", type = FieldType.Text, analyzer = "ikIndexAnalyzer")
}
)
protected String label;