首页 > 解决方案 > 如何使用应该存在的 2 个字段编写嵌套查询

问题描述

TL;博士

当我尝试在一个子句中Bool.Match匹配多个事物时,Nest 正在创建一个额外的内部(在 bool 上下文中)should


预期 - 查询

我正在使用 Nest 5.6.1,并且正在尝试编写以下查询:

{
  "query": {
    "bool": {
      "minimum_should_match": 2,
      "should": [
        {
          "exists": {
            "field": "B"
          }
        },
        {
          "exists": {
            "field": "A"
          }
        },
        {
          "match": {
            "fields.MachineName": "MY_Machine"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "2018-09-03T00:00:00",
              "lt": "2018-09-04T00:00:00"
            }
          }
        }
      ]
    }
  }
}

我试过的

我试图通过以下方式实现它:

var searchResponse = connection.Search<ExcludedCusipsStructLog>(sd => sd
                         .Index(DefaultIndex)
                         .From(0)
                         .Size(1000)
                         .Type(LogType.ProxyLog)
                         .Query(q => q
                             .Bool(b => b
                                .Should(sh => sh
                                        .Exists(e => e
                                        .Field(fi => fi.A)
                                        )
                                      && sh
                                        .Exists(e => e
                                        .Field(fi => fi.B)
                                        )
                                      && sh
                                        .Match(ma => ma
                                        .Field(f => f.MachineName)
                                        .Query("MY_Machine")
                                        )
                                 )
                                .MinimumShouldMatch(2)
                                .Filter(fi => fi
                                        .DateRange(r => r
                                             .Field(f => f.Timestamp)
                                             .GreaterThanOrEquals(dateToSearch)
                                             .LessThan(dateToSearch.AddDays(1))
                                        )
                                 )

                             )
                         )
                    );

实际结果

问题是生成此请求的嵌套:

{
    "from": 0,
    "size": 1000,
    "query": {
        "bool": {
            "should": [{
                "bool": {
                    "must": [{
                        "exists": {
                            "field": "fields.invalidPositionList"
                        }
                    }, {
                        "exists": {
                            "field": "fields.excludedCusips"
                        }
                    }, {
                        "match": {
                            "fields.MachineName": {
                                "query": "GSMSIMPAPUA01"
                            }
                        }
                    }]
                }
            }],
            "filter": [{
                "range": {
                    "@timestamp": {
                        "gte": "2018-09-06T00:00:00",
                        "lt": "2018-09-07T00:00:00"
                    }
                }
            }],
            "minimum_should_match": 2
        }
    }
}

更多细节

我还发现,如果我只查看 Should 子句中的一个字段 - Nest 正在创建一个很好的查询 - 是什么让我认为我应该以不同的方式添加其他字段(我没有找到)

标签: c#elasticsearchnestelastic-stack

解决方案


正如@KozhevnikovDmitry 在评论中提到的那样。我应该使用昏迷而不是&&ie 正确的方法是:

var searchResponse = connection.Search<ExcludedCusipsStructLog>(sd => sd
                         .Index(DefaultIndex)
                         .From(0)
                         .Size(1000)
                         .Type(LogType.ProxyLog)
                         .Query(q => q
                             .Bool(b => b
                                .Should(sh => sh
                                        .Exists(e => e
                                        .Field(fi => fi.A)
                                        )
                                      ,sh => sh
                                        .Exists(e => e
                                        .Field(fi => fi.B)
                                        )
                                      ,sh => sh
                                        .Match(ma => ma
                                        .Field(f => f.MachineName)
                                        .Query("MY_Machine")
                                        )
                                 )
                                .MinimumShouldMatch(2)
                                .Filter(fi => fi
                                        .DateRange(r => r
                                             .Field(f => f.Timestamp)
                                             .GreaterThanOrEquals(dateToSearch)
                                             .LessThan(dateToSearch.AddDays(1))
                                        )
                                 )

                             )
                         )
                    );

推荐阅读