过滤掉不包含条款的成员的项目的最有效方法是什么?

What's the most efficient way to filter out items with member that don't contain the terms?

编写以下查询的最有效方法是什么?

获得 5 个成员 member 包含以下任何项目的项目:['item1', 'item2']

should: [{ terms : {member: ['item1', 'item2'] } }]

如果您只找到 3 件物品,再得到 2 件,其中 member

如何完成此查询?

您可以在 term and not exists 中使用 should 子句。因此它将获取字段成员与输入查询匹配且字段不存在的文档。您可以传递大小以从结果中获取前 5 个文档

{
  "size": 5,
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "member": [
              "1",
              "2",
              "3"
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "member"
                }
              }
            ]
          }
        }
      ]
    }
  }
}