嵌套查询相当于以下 ElasticSearch 查询

Bij*_*kel 5 c# elasticsearch nest .net-core

我是 .net core 的新手。我使用https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html作为我的弹性搜索客户端。

我有以下用弹性搜索编写的查询:

{
    "query": {
        "bool":{
         "must":[
            {
               "term":{
                  "bookId.keyword":"c962ec04-6d25-4823-8e78-6d99d4072032"
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "match_phrase":{
                         "text":"Plants"
                        }
                     }
                  ]
               }
            }
         ]
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然我的 NEST 代码是:

{
    "query": {
        "bool":{
         "must":[
            {
               "term":{
                  "bookId.keyword":"c962ec04-6d25-4823-8e78-6d99d4072032"
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "match_phrase":{
                         "text":"Plants"
                        }
                     }
                  ]
               }
            }
         ]
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我无法得到预期的结果。谁能帮我这个?

Mah*_*pal 3

您的 NEST 查询与 DSL 查询不完全匹配。请修改您的查询以匹配以下内容。

var searchResponse = await _esClient.SearchAsync<Content>(s => s
    .Query(q => q
        .Bool(b => b
            .Must(m => m
                .Term(terms => terms
                    .Field(field => field
                        .BookId.Suffix("keyword"))
                        .Value(bookId.ToString())
                ) && m.Bool(mb => mb
                    .Should(sh => sh
                        .MatchPhrase(mp => mp
                           .Field(f => f.Text).Query(query))))
                    )
                )
            ));
Run Code Online (Sandbox Code Playgroud)