PHP Elasticsearch 7.5 - Painless 脚本条件未提供正确的结果

PHP Elasticsearch 7.5 - Painless script condition is not providing proper results

这是我的弹性搜索查询。我正在尝试获取满足条件的所有文档,如果 job_id 为 1,则获取具有 sourced = 0 的文档,否则,如果 job_id 不为 1,则获取具有 sourced = 1 的文档.

但是它returns所有文件。

[
'script' => 
    ['script' => 
        [
            'lang' => 'painless', 
            'source' => "(doc['job_id'].size() > 0 && doc['job_id'].value !==  params.jid) || (doc['job_id'].size() > 0 && doc['job_id'].value == params.jid && doc['sourced'].value == 0)",
            'params' => ['jid' => 1]
        ]
    ]
]

引用字段的映射

"job_id" : {
      "type" : "long"
    },
"sourced" : {
      "type" : "byte"
    },

每个文档都由内联脚本评估。

一个示例(基于您的映射):

插入文件

PUT my_index/_doc/1
{
  "job_id": 1,
  "sourced": 0
}

PUT my_index/_doc/2
{
  "job_id": 0,
  "sourced": 2
}

搜索查询

GET my_index/_search (Kibana usage)
{
  "query": {
    "script": {
      "script": {
        "lang": "painless",
        "source": "doc['job_id'].value == params.jid ? doc['sourced'].value == 0 : doc['sourced'].value == 1",
        "params": {
          "jid": 1
        }
      }
    }
  }
}

您的使用情况

[
'script' => 
    ['script' => 
        [
            'lang' => 'painless', 
            'source' => "doc['job_id'].value == params.jid ? doc['sourced'].value == 0 : doc['sourced'].value == 1",
            'params' => ['jid' => 1]
        ]
    ]
]

结果

 "hits" : [
  {
    "_index" : "my_index",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "job_id" : 1,
      "sourced" : 0
    }
  }
]

希望对您有所帮助