0

给定以下数据,什么查询会返回恰好有 2 个成员(嵌套)的所有文档?

数据

PUT nested_test2
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "team": { "type": "keyword", "index": true, "doc_values": true },
      "nicknames": { "type": "keyword", "index": true, "doc_values": true },
      "members": {
        "type": "nested",
        "properties": {
          "name": { "type": "keyword", "index": true, "doc_values": true },
          "age": { "type": "integer", "index": true, "doc_values": true}
        }
      }
    }
  }
}
 
PUT nested_test2/_doc/1
{
    "team" : "A" ,
    "nicknames": ["Ants", "Antelopes"],
    "members" :
      [
        { "name" : "Curt" , "age" : "34" } ,
        { "name" : "Dave" , "age" : "33" }
      ]
}
 
PUT nested_test2/_doc/2
{
    "team" : "B" ,
    "nicknames": ["Bears"],
    "members" :
      [
        { "name" : "Alex" , "age" : "36" }
      ]
}

PUT nested_test2/_doc/3
{
    "team" : "C" ,
    "nicknames": ["Cats", "Caterpillars"],
    "members" :
      [
        { "name" : "Brad" , "age" : "35" } ,
        { "name" : "Gary" , "age" : "31" }
      ]
}

查询以获取恰好具有 2 个昵称的所有文档

这可行,但昵称不是嵌套的。简单地将此查询更改为引用“成员”而不是“昵称”是行不通的。什么语法会做到这一点?

POST nested_test2/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "if (doc['nicknames'].size()==2) return true;"
        }
      }
    }
  }
}
4

0 回答 0