feild option: norms #
建立索引:
text
PUT my_index
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"_doc": {
"_source": {
"enabled": true
},
"properties": {
"title": {
"type": "text",
"norms": false
},
"overview": {
"type": "text",
"norms": true
},
"body": {
"type": "text"
},
"author": {
"type": "keyword",
"norms": true
},
"chapters": {
"type": "keyword",
"norms": false
},
"email": {
"type": "keyword"
}
}
}
}
}
查询索引:
text
GET my_index/_mapping
{
"my_index": {
"mappings": {
"_doc": {
"properties": {
"author": {
"type": "keyword",
"norms": true
},
"body": {
"type": "text"
},
"chapters": {
"type": "keyword"
},
"email": {
"type": "keyword"
},
"overview": {
"type": "text"
},
"title": {
"type": "text",
"norms": false
}
}
}
}
}
}
- norms 选项对于 text 类型的字段,默认开启;对于 keyword 类型的字段,默认关闭。
norms 选项作用 #
norms 是一个用来计算文档/字段得分(Score)的"调节因子"。TF-IDF、BM25算法计算文档得分时都用到了norms参数。
ES官方文档关于Norms解释:
Norms store various normalization factors that are later used at query time in order to compute the score of a document relatively to a query.
这里的 normalization factors 用于查询计算文档得分时进行 boosting。比如根据BM25算法给出的公式(freq*(k1+1))/(freq+k1*(1-b+b*fieldLength/avgFieldLength))
计算文档得分时,其中的fieldLength/avgFieldLength
就是 normalization factors。
norms 最佳实践 #
建议默认将norms设置为false,节省磁盘空间,提升索引性能,只有在需要评分的场景才设置为true。
例1:搜索应用名称等场景下,往往字段内容长度较短,如果开启norms选项、完全中文分词,则可能导致叠词的、部分匹配的应用名称获得更高的评分,以及完全匹配关键词的应用名称排序不及预期,因此这类场景推荐关闭norms选项。
例2:搜索文章内容等场景下,更看重关键词在文章中的比例,开启norms选项可以优化内容长度对评分的影响,使排序结果更符合预期。