如何使用对象初始值设定项语法将 bool 查询 should 子句包装在 bool 查询过滤器子句中?
How wrap a bool query should clause in a bool query filter clause with Object Initializer Syntax?
我想在过滤器中执行一个 should 操作,以便在结果与某个条件或另一个条件匹配时得到结果。
在 elasticsearch 中它将是:
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match": {
"type1_title": "searched match"
}
},
{
"match": {
"type2_title": "searched match"
}
}
]
}
}
]
}
}
}
我试图在嵌套的另一个 boolQuery 中输入 en boolQuery:
var shouldForFilter = new List<QueryContainer>();
shouldForFilter.Add(new TermQuery()
{
Field = "type1Title", Value = "searched match",
});
shouldForFilter.Add(new TermQuery()
{
Field = "type2Title", Value = "searched match",
});
var shouldContainer = new List<QueryContainer> {
....
}
...
var innerFilterQuery = new BoolQuery
{
Should = shouldForFilter
};
var searchQuery = new BoolQuery
{
Filter = innerFilterQuery, //does not work
Should = shouldContainer,
Must = mustContainer,
From = 10,
Size = 20
Minscore = 1
};
显然类型不匹配。
你知道怎么做吗?
在 NEST 中使用运算符重载,这可以非常简洁地表达
client.Search<MyDocument>(s => s
.Query(q => +(q
.Match(m => m
.Field("type1Title")
.Query("searched match")
) || q
.Match(m => m
.Field("type2Title")
.Query("searched match")
))
)
);
产生
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match": {
"type1Title": {
"query": "searched match"
}
}
},
{
"match": {
"type2Title": {
"query": "searched match"
}
}
}
]
}
}
]
}
}
}
您还可以通过对象初始化语法使用运算符重载,这可能比在 bool
查询中包装更简洁。重要的部分是以正确的顺序应用操作。此示例生成与上面相同的查询
QueryContainer query = new MatchQuery
{
Field = "type1Title",
Query = "searched match"
};
// use binary || operator to wrap combined queries
// into bool query should clause
query |= new MatchQuery
{
Field = "type2Title",
Query = "searched match"
};
// use unary operator to wrap query in bool
// query filter clause
query = +query;
var request = new SearchRequest<MyDocument>
{
Query = query
};
client.Search<MyDocument>(request);
我想在过滤器中执行一个 should 操作,以便在结果与某个条件或另一个条件匹配时得到结果。
在 elasticsearch 中它将是:
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match": {
"type1_title": "searched match"
}
},
{
"match": {
"type2_title": "searched match"
}
}
]
}
}
]
}
}
}
我试图在嵌套的另一个 boolQuery 中输入 en boolQuery:
var shouldForFilter = new List<QueryContainer>();
shouldForFilter.Add(new TermQuery()
{
Field = "type1Title", Value = "searched match",
});
shouldForFilter.Add(new TermQuery()
{
Field = "type2Title", Value = "searched match",
});
var shouldContainer = new List<QueryContainer> {
....
}
...
var innerFilterQuery = new BoolQuery
{
Should = shouldForFilter
};
var searchQuery = new BoolQuery
{
Filter = innerFilterQuery, //does not work
Should = shouldContainer,
Must = mustContainer,
From = 10,
Size = 20
Minscore = 1
};
显然类型不匹配。
你知道怎么做吗?
在 NEST 中使用运算符重载,这可以非常简洁地表达
client.Search<MyDocument>(s => s
.Query(q => +(q
.Match(m => m
.Field("type1Title")
.Query("searched match")
) || q
.Match(m => m
.Field("type2Title")
.Query("searched match")
))
)
);
产生
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match": {
"type1Title": {
"query": "searched match"
}
}
},
{
"match": {
"type2Title": {
"query": "searched match"
}
}
}
]
}
}
]
}
}
}
您还可以通过对象初始化语法使用运算符重载,这可能比在 bool
查询中包装更简洁。重要的部分是以正确的顺序应用操作。此示例生成与上面相同的查询
QueryContainer query = new MatchQuery
{
Field = "type1Title",
Query = "searched match"
};
// use binary || operator to wrap combined queries
// into bool query should clause
query |= new MatchQuery
{
Field = "type2Title",
Query = "searched match"
};
// use unary operator to wrap query in bool
// query filter clause
query = +query;
var request = new SearchRequest<MyDocument>
{
Query = query
};
client.Search<MyDocument>(request);