Elasticsearch - must_not不影响嵌套字段的得分



我试图根据以下规则查询一些按最佳分数排序的日期:

  1. 批准应该是action=accepted或type=a1或type=a2
  2. 批准不应该匹配任何操作=被拒绝

我没有从查询中过滤出任何数据,我只是想先得到最好的数学。

映射:

PUT test
{
"mappings": {
"properties": {
"savedAt": {
"type":   "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
},
"approvals": {
"type": "nested",
"properties": {
"action": {
"type":   "text",
"analyzer": "keyword"
},
"by": {
"type":   "text",
"analyzer": "keyword"
},
"type": {
"type":   "text",
"analyzer": "keyword"
}
}
}
}
}
}

数据:

POST test/_create/1
{
"savedBy": "Donatello",
"savedAt": "2022-04-18T19:09:27.527+0200",
"approvals": [
{
"action": "approved",
"type": "a1",
"by": "Raphael"
},
{
"action": "approved",
"type": "a2",
"by": "Michelangelo"
}
]
}
POST test/_create/2
{
"savedBy": "Michelangelo",
"savedAt": "2022-04-19T19:09:27.527+0200",
"approvals": [
{
"action": "approved",
"type": "a1",
"by": "Raphael"
},
{
"action": "rejected",
"type": "a2",
"by": "Leonardo"
}
]
}
POST test/_create/3
{
"savedBy": "Raphael",
"savedAt": "2022-04-20T19:09:27.527+0200",
"approvals": [
{
"action": "approved",
"type": "a1",
"by": "Leonardo"
}
]
}
查询:

GET test/_search
{
"sort" : [
"_score",
{ "savedAt" : "desc" }
],
"query": {
"bool": {
"should": [
{
"nested": {
"path": "approvals",
"query": {
"bool": {
"must_not": [
{
"term": {
"approvals.action": {
"value": "rejected"
}
}
}
], 
"should": [
{
"term": {
"approvals.action": {
"value": "approved"
}
}
},
{
"term": {
"approvals.type": {
"value": "a1"
}
}
},
{
"term": {
"approvals.type": {
"value": "a2"
}
}
}
],
"minimum_should_match": 2
}
}
}
}
],
"minimum_should_match": 0
}
}
}

反应:

{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.99491465,
"_source" : {
"savedBy" : "Donatello",
"savedAt" : "2022-04-18T19:09:27.527+0200",
"approvals" : [
{
"action" : "approved",
"type" : "a1",
"by" : "Raphael"
},
{
"action" : "approved",
"type" : "a2",
"by" : "Michelangelo"
}
]
},
"sort" : [
0.99491465,
1650301767527
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.8266785,
"_source" : {
"savedBy" : "Raphael",
"savedAt" : "2022-04-20T19:09:27.527+0200",
"approvals" : [
{
"action" : "approved",
"type" : "a1",
"by" : "Leonardo"
}
]
},
"sort" : [
0.8266785,
1650474567527
]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.8266785,
"_source" : {
"savedBy" : "Michelangelo",
"savedAt" : "2022-04-19T19:09:27.527+0200",
"approvals" : [
{
"action" : "approved",
"type" : "a1",
"by" : "Raphael"
},
{
"action" : "rejected",
"type" : "a2",
"by" : "Leonardo"
}
]
},
"sort" : [
0.8266785,
1650388167527
]
}
]
}
}

可以看到data id=2 &Id =3有相同的分数(_score": 0.8266785)

我期望id=2会有最低的分数,因为它有action=rejected(在must_not标准中声明)

有人可以解释我Elasticsearch是如何得分在这种情况下,请?

must_not对评分没有贡献

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

must_not

子句(查询)不能出现在匹配的文档中。子句在过滤器上下文中执行,这意味着评分被忽略,子句被考虑用于缓存。因为忽略了评分,所以返回所有文档的评分为0。

最新更新