Elasticsearch 映射参数 fields
fields
处于不同的目的,通过不同的方法索引相同的字段通常非常有用。这也是多字段的目的。例如,一个字符串字段可以映射为text字段用于全文本搜索,也可以映射为keyword字段用于排序或聚合。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
PUT my_index
{
"mappings" : {
"_doc" : {
"properties" : {
"city" : {
"type" : "text" ,
"fields" : {
"raw" : {
"type" : "keyword"
}
}
}
}
}
}
}
|
note
:city.raw字段是city字段的keyword版本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
GET my_index/_search
{
"query" : {
"match" : {
"city" : "york"
}
},
"sort" : {
"city.raw" : "asc"
},
"aggs" : {
"Cities" : {
"terms" : {
"field" : "city.raw"
}
}
}
}
|
note
:city字段用于全文本搜索。
note
:city.raw用于排序与聚合。
多字段不能修改原始_source字段。
对于相同索引中具有相同名称的字段,fields设置允许有不同的设置。可以使用PUT映射API将新的多字段添加到已存在的字段中。
带有多个分析的多字段
多字段的另一个应用场景是使用不同的方法分析相同的字段以求获得更好的相关性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
PUT my_index
{
"mappings" : {
"_doc" : {
"properties" : {
"text" : {
"type" : "text" ,
"fields" : {
"english" : {
"type" : "text" ,
"analyzer" : "english"
}
}
}
}
}
}
}
|
note
:text.field字段使用english分析器。
elasticsearch注解实现fields
mapping效果:
1
2
3
4
5
6
7
8
9
|
"label" : {
"type" : "keyword" ,
"fields" : {
"IKS" : {
"type" : "text" ,
"analyzer" : "ikIndexAnalyzer"
}
}
}
|
1
2
3
4
5
6
7
8
|
@Column (name = "标签" )
@MultiField (
mainField = @Field (type = FieldType.Keyword),
otherFields = {
@InnerField (suffix = "IKS" , type = FieldType.Text, analyzer = "ikIndexAnalyzer" )
}
)
protected String label;
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_32165041/article/details/83688593