如何编写NEST代码来为这个简单的布尔逻辑生成弹性搜索查询?
term1 && (term2 || term3 || term4)
我使用Nest(5.2)语句执行此逻辑的伪代码查询ElasticSearch(5.2)
// additional requirements
( truckOemName = "HYSTER" && truckModelName = "S40FT" && partCategoryCode = "RECO" && partID != "")
//Section I can't get working correctly
AND (
( SerialRangeInclusiveFrom <= "F187V-6785D" AND SerialRangeInclusiveTo >= "F187V-6060D" )
OR
( SerialRangeInclusiveFrom = "" || SerialRangeInclusiveTo = "" )
)
&#34;将查询与||结合使用或{/ 3}}提及
中的条款&#34;
bool
查询并不完全遵循您对编程语言所期望的相同布尔逻辑。term1 && (term2 || term3 || term4)
不会成为
bool
|___must
| |___term1
|
|___should
|___term2
|___term3
|___term4
您可以取回仅包含term1
的结果
这正是我的想法。
但他们解决这个问题的答案超出了我对如何将其应用于Nest的理解。答案是?
- 添加括号以强制评估订单(我)
- 使用
醇>boost
因素? (什么?)
这是NEST代码
var searchDescriptor = new SearchDescriptor<ElasticPart>();
var terms = new List<Func<QueryContainerDescriptor<ElasticPart>, QueryContainer>>
{
s =>
(s.TermRange(r => r.Field(f => f.SerialRangeInclusiveFrom)
.LessThanOrEquals(dataSearchParameters.SerialRangeEnd))
&&
s.TermRange(r => r.Field(f => f.SerialRangeInclusiveTo)
.GreaterThanOrEquals(dataSearchParameters.SerialRangeStart)))
//None of the data that matches these ORs returns with the query this code generates, below.
||
(!s.Exists(exists => exists.Field(f => f.SerialRangeInclusiveFrom))
||
!s.Exists(exists => exists.Field(f => f.SerialRangeInclusiveTo))
)
};
//Terms is the piece in question
searchDescriptor.Query(s => s.Bool(bq => bq.Filter(terms))
&& !s.Terms(term => term.Field(x => x.OemID)
.Terms(RulesHelper.GetOemExclusionList(exclusions))));
searchDescriptor.Aggregations(a => a
.Terms(aggPartInformation, t => t.Script(s => s.Inline(script)).Size(50000))
);
searchDescriptor.Type(string.Empty);
searchDescriptor.Size(0);
var searchResponse = ElasticClient.Search<ElasticPart>(searchDescriptor);
这是它生成的ES JSON查询
{
"query":{
"bool":{
"must":[
{
"term":{ "truckOemName": { "value":"HYSTER" }}
},
{
"term":{ "truckModelName": { "value":"S40FT" }}
},
{
"term":{ "partCategoryCode": { "value":"RECO" }}
},
{
"bool":{
"should":[
{
"bool":{
"must":[
{
"range":{ "serialRangeInclusiveFrom": { "lte":"F187V-6785D" }}
},
{
"range":{ "serialRangeInclusiveTo": { "gte":"F187V-6060D" }}
}
]
}
},
{
"bool":{
"must_not":[
{
"exists":{ "field":"serialRangeInclusiveFrom" }
}
]
}
},
{
"bool":{
"must_not":[
{
"exists":{ "field":"serialRangeInclusiveTo" }
}
]
}
}
]
}
},
{
"exists":{
"field":"partID"
}
}
]
}
}
}
以下是我们希望生成的查询似乎有效。
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": { "truckOemName": { "value": "HYSTER" }}
},
{
"term": {"truckModelName": { "value": "S40FT" }}
},
{
"term": {"partCategoryCode": { "value": "RECO" }}
},
{
"exists": { "field": "partID" }
}
],
"should": [
{
"bool": {
"must": [
{
"range": { "serialRangeInclusiveFrom": {"lte": "F187V-6785D"}}
},
{
"range": {"serialRangeInclusiveTo": {"gte": "F187V-6060D"}}
}
]
}
},
{
"bool": {
"must_not": [
{
"exists": {"field": "serialRangeInclusiveFrom"}
},
{
"exists": { "field": "serialRangeInclusiveTo"}
}
]
}
}
]
}
}
]
}
}
}
答案 0 :(得分:1)
对于bool
个查询的重载运算符,无法表达must
子句与should
子句相结合,即
term1 && (term2 || term3 || term4)
成为
bool
|___must
|___term1
|___bool
|___should
|___term2
|___term3
|___term4
这是bool
个查询,其中包含两个must
子句,其中第二个must
子句是bool
个查询,其中必须至少有一个匹配should
条款。 NEST结合了这样的查询,因为它符合.NET中布尔逻辑的期望。
如果 它确实变为
bool
|___must
| |___term1
|
|___should
|___term2
|___term3
|___term4
如果文档仅满足must
子句,则视为匹配。在这种情况下,should
条款可以作为提升,即如果文档与should
子句中的一个或多个must
子句匹配,那么它将具有更高的相关性得分,假设term2
,term3
和term4
是计算相关性得分的查询。
在此基础上,您要生成的查询表示,对于要视为匹配的文档,它必须匹配must
子句中的所有4个查询
"must": [
{
"term": { "truckOemName": { "value": "HYSTER" }}
},
{
"term": {"truckModelName": { "value": "S40FT" }}
},
{
"term": {"partCategoryCode": { "value": "RECO" }}
},
{
"exists": { "field": "partID" }
}
],
然后,对于匹配must
子句的文档,如果
serialRangeInclusiveFrom
小于或等于"F187V-6785D"
且serialRangeInclusiveFrom
大于或等于"F187V-6060D"
或
serialRangeInclusiveFrom
和serialRangeInclusiveTo
然后提升文档相关性得分。关键在于
如果文档与
must
条款匹配但与任何条款不匹配 在should
子句中,它仍然是查询的匹配项(但是 相关性得分较低。
如果这是意图,则可以构造此查询using the longer form of the Bool
query