写在前面
公司产品提出需求,需要对文档进行评分,如果评分不相同则按照评分大小排序,如果评分相同则在相同的结果下按照试题年份排序.
问题原因
es在执行sort之后方评分失效
解决办法
通过阅读文档得知,es在执行评分会大量耗时,并且在排序的时候由于没有指定元字段’_score’会导致不会进行评分.所以我们在使用的时候如果想根据评分相同的情况下在进行排序那我们就需要对评分进行选定.
选定评分的办法
在查询的时候添加家元属性
"track_scores":"true"
在进行排序的时候一定要将评分算入排序规则中
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"year": {
"order": "desc"
}
}
]
结果测试
测试环境
- es(一个负载节点,两个存储节点)
- 15万道试题单子段长度平均大于50字
响应时间
- 未排序:33m
- 排序后:47m
结果
就测试结果而言,评分之后在进行排序对性能损耗是可以接受的.
疑问
由于业务的要求,目前检索数据需求就是对字段进行双模糊查询,所以使用的是wildcard去检索数据,这个字段对内容的评分定义非常粗糙他的算法和正常的match查询算法是不一样的具体细节有待研究!!!
贴上我的查询语句仅供参考
{
"track_scores":"true",
"from": 0,
"size": 20,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"stem.keyword": {
"wildcard": "*大城市*",
"boost": 100
}
}
},
{
"wildcard": {
"choices.keyword": {
"wildcard": "*大城市*",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"filter": [
{
"term": {
"mode": {
"value": "1",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"year": {
"order": "desc"
}
}
],
"highlight": {
"pre_tags": [
""
],
"post_tags": [
""
],
"fragment_size": 100,
"number_of_fragments": 1,
"fields": {
"stem.keyword": {},
"choices.keyword": {}
}
}
}