当至少一个对象不可用时,按对象数组选择文档';不包含必要的字段弹性搜索

当至少一个对象不可用时,按对象数组选择文档';不包含必要的字段弹性搜索,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我在elasticsearch中有文档,无法理解如何应用搜索脚本,如果任何附件不包含uuid或uuid为空,该脚本应返回文档。弹性5.2的版本。 文件映射 "mappings": { "documentType": { "properties": { "attachment": { "properties": {

我在elasticsearch中有文档,无法理解如何应用搜索脚本,如果任何附件不包含uuid或uuid为空,该脚本应返回文档。弹性5.2的版本。 文件映射

"mappings": {
    "documentType": {
        "properties": {
            "attachment": {
                "properties": {
                    "uuid": {
                        "type": "text"
                    },
                    "path": {
                        "type": "text"
                    },
                    "size": {
                        "type": "long"
                    }
                }
            }}}
在elasticsearch中,它看起来像

{
        "_index": "documents",
        "_type": "documentType",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "attachment": [
               {
                "uuid": "21321321",
                "path": "../uploads/somepath",
                "size":1231
               },
               {
                "path": "../uploads/somepath",
                "size":1231
               },      
         ]},
{
        "_index": "documents",
        "_type": "documentType",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "attachment": [
               {
                "uuid": "223645641321321",
                "path": "../uploads/somepath",
                "size":1231
               },
               {
                "uuid": "22341424321321",
                "path": "../uploads/somepath",
                "size":1231
               },        
         ]},
{
        "_index": "documents",
        "_type": "documentType",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "attachment": [
               {
                "uuid": "22789789341321321",
                "path": "../uploads/somepath",
                "size":1231
               }, 
               {
                "path": "../uploads/somepath",
                "size":1231
               },      
         ]}
因此,我希望获得id为1和3的附件。但结果我得到了错误的脚本 我尝试应用下一个脚本:

{
    "query": {
        "bool": {
            "must": [
                {
                    "exists": {
                        "field": "attachment"
                    }
                },
                {
                    "script": {
                        "script": {
                            "inline": "for (item in doc['attachment'].value) { if (item['uuid'] == null) { return true}}",
                            "lang": "painless"
                        }
                    }
                }
            ]
        }
    }
}
下一个错误是:

 "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:77)",
                    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:36)",
                    "for (item in doc['attachment'].value) { ",
                    "                 ^---- HERE"
                ],
                "script": "for (item in doc['attachment'].value) { if (item['uuid'] == null) { return true}}",
                "lang": "painless"
            }
        ],

即使一个附件对象不包含uuid,也可以选择文档吗?

迭代对象数组并不像人们期望的那样简单。我写了很多关于它的文章

由于您的
附件
未定义为
嵌套
,因此ES将在内部将其表示为扁平的值列表(也称为“文档值”)。例如,doc#2中的
附件.uuid
将变成
[“223645641321”,“22341424321321”
,而
附件.大小将变成
[12311231]

这意味着您可以简单地比较这些展平表示的
.length
!我假设
attachment.size
将始终存在,因此可以作为比较基线

还有一件事。要利用这些文本字段的优化文档值,它将:

PUT文档/documentType/\u映射
{
“财产”:{
“附件”:{
“财产”:{
“uuid”:{
“类型”:“文本”,
“fielddata”:true仅作补充。如果uuid字段的映射是自动创建的,弹性搜索将以以下方式添加它:

"uuid": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }
    }
}
然后脚本可能看起来像:

POST documents/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "exists": {
                        "field": "attachment"
                    }
                },
                {
                    "script": {
                        "script": {
                            "inline": "doc['attachment.size'].length > doc['attachment.uuid.keyword'].length",
                            "lang": "painless"
                        }
                    }
                }
            ]
        }
    }
}

感谢您的回复。我会检查它并寻找未来。我会为嵌套的更改类型的附件制作重新索引。不客气。将附件嵌套是一个好方法。请记住,使用
嵌套的
字段编写脚本是它自己的一章。我的答案顶部的链接应该会将您指向右侧的d方向,虽然!它的工作。我已经重新索引和运行您的脚本,它的工作。非常感谢你。你是对的,你的建议也很好,即使没有重新索引