ElasticSearch Nest - 查询数组字段

ElasticSearch Nest - query on an array field

我有包含数组元素的文档:

{
...,
"roles": ["Worker", "Admin",...]
...
}

我如何创建一个查询,returns 所有文档在此数组中的所有值都不是例如“客户端”?

假设您的字段 roles 没有嵌套,请尝试以下解决方案。

GET /<your-index>/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "roles": {
              "value": "client"
            }
          }
        }
      ]
    }
  }
}

您为字段 roles 设置的映射很重要:

  1. 如果您有映射 text,为了便于查询,它将存储为 roles: ["worker", "admin", "client"]。这意味着您的查询必须包含 "client"(而不是 "Client")。
  2. 如果您有映射 keyword,则值是“原始”存储的,您应该使用 "Client" 进行搜索。不知道就测试2种可能吧

不得嵌套查询

var searchResults = this.elasticClient.Search<Model>(s => s
    .Query(q => q
        .Bool(b => b
            .MustNot(
                bs => bs.Term(p => p.roles, "client"),
            )
        )
    )
);