繁体   English   中英

如何获取具有多个值的数组字段的弹性文档?

[英]How to obtain the elastic documents that have an array field with more than one value?

我是 elasticsearch 的新手,我正在尝试查询但没有成功。 我的索引中有以下文档:

"hits" : [
      {
        "_index" : "index_text",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "alvaro",
          "cars" : [
            {
              "model" : "opel"
            },
            {
              "model" : "renault"
            }
          ]
        }
      },
      {
        "_index" : "index_text",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "pepe",
          "cars" : {
            "model" : "opel"
          }
        }
      }
    ]

我想要的是获得不止一辆车的文件。 我已经尝试了很多其他方法:

GET index_text/_search
{
  "query": {
    "script": {
      "script": "doc['cars'].value > 2"
    }
  }
}

结果是:

"caused_by" : {
  "type" : "illegal_argument_exception",
  "reason" : "No field found for [cars] in mapping with types []"
}

我可以做什么?

您需要使用脚本查询,根据提供的脚本过滤文档。

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['cars.model.keyword'].length > 1",
            "lang": "painless"
          }
        }
      }
    }
  }
}

搜索结果将是

"hits": [
      {
        "_index": "67198142",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "name": "alvaro",
          "cars": [
            {
              "model": "opel"
            },
            {
              "model": "renault"
            }
          ]
        }
      }
    ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM