c# - 如何使用应该存在的 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 正在创建一个很好的查询 - 是什么让我认为我应该以不同的方式添加其他字段(我没有找到)
解决方案
正如@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))
)
)
)
)
);
推荐阅读
- c - 安装加密库“libpqcrypto”
- jquery - 我需要将对象集合和字符串传递给 MVC 控制器
- firebase - 跨多个区域部署 Firebase Cloud Functions
- keyboard - 颤动数字键盘没有完成按钮
- wordpress - 提取我文章的所有特色图片的路径
- c# - 如何使用实时按钮访问另一个表单的属性?
- javascript - nodejs和网络浏览器中的单线程是否相同?
- angular - 在 aws 中安装 Angular 项目的问题
- python - 函数的类型注释,返回一个采用 args 和 kwargs 的函数
- c# - 当 WPF 应用程序中的文本框输入更改时,如何更改主窗口颜色?