使用必须字段值编写 Elasticsearch Nest Bool 查询

Writing Elasticsearch Nest Bool query with Must Field value

我正在尝试重现以下在 Kibana 中运行良好的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "fake"
          }
        }
      ],
      "filter": {
        "term": {
          "roleIds": "af54122f-8d99-47e5-9e5a-88659a1229d4"
        }
      }
    }
  }
}

这是我在 .NET 中的尝试:

ISearchResponse<T> response = await client.SearchAsync<T>(s => s
    .Query(q => q
        .Bool(b => b
            .Must(m => m
                .Match(t => t
                    .Field(new Field("title"))
                    .Value("fake") // "Value" is red.
                ))
        .Filter(f => f
            .Term(t => t
                .Field(new Field("roleIds")).Value(RoleId))) // Value works here.
          )); 

值在这里不起作用。编译器错误是:

Error   CS1061  'MatchQueryDescriptor<T>' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'MatchQueryDescriptor<T>' could be found (are you missing a using directive or an assembly reference?)

如何写出 bool > must > match > title: val 的等效项?

只需将 .Value("fake") 替换为 .Query("fake")。作为旁注,您可以使用 .Field("title").

简化 .Field(new Field("title"))

希望对您有所帮助。