TL; DR
当我尝试匹配Bool.Match
子句中的多个内容时(在 bool(boolean) 环境中),Nest正在创建额外的内部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的评论中所述。
我应该使用昏迷而不是&&
,即正确的方法是:
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# - 如何使用应存在的2个字段编写Nest查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52203057/