TL; DR
当我尝试匹配Bool.Match
子句中的多个对象时(在布尔上下文中),嵌套会创建一个额外的内部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正在创建一个很好的查询-那让我认为我应该以其他方式添加其他字段(我没有找到)
答案 0 :(得分:2)
如@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))
)
)
)
)
);