nest:构建一个带有 must 和 should 约束的 QueryContainer 请求
nest: build a QueryContainer request with must and a should constraint
我尝试构建一个带有必须和应该请求的 elasticsearch 请求
我的目标要求如下:
POST /index/type/_search
{
"query": {
"bool" : {
"must" : {
"match":{"field1":{"query":"word1"}}
},
"should":{
"match":{"field2":{"query":"word2"}}
}
}
}}
我写了:
var queryContainer = new QueryContainer();
queryContainer &= new MatchQuery() { Field = "field1", Query = "word1" };
queryContainer |= new MatchQuery() { Field = "field2", Query = "word2"};
var searchRequest = new SearchRequest<type>
{
Query = queryContainer
};
但它产生了我:
POST /index/type/_search
{
"query":{
"bool":{
"should":[
{"match":{"field1":{"query":"word1"}}},
{"match":{"field2":{"query":"word2"}}}]
}
}
}
你知道在构建我的 QueryContainer 时我应该写些什么吗?
在这里使用运算符重载没有多大帮助;我认为在这种情况下,构建 bool
查询更清晰
对象初始化器语法
var searchRequest = new SearchRequest<Document>()
{
Query = new BoolQuery
{
Must = new QueryContainer[] { new MatchQuery() { Field = "field1", Query = "word1" } },
Should = new QueryContainer[] { new MatchQuery() { Field = "field2", Query = "word2" } }
}
};
client.Search<Document>(searchRequest);
或流利API
client.Search<Document>(s => s
.Query(q => q
.Bool(b => b
.Must(fi => fi
.Match(m => m
.Field("field2")
.Query("word2")
)
)
.Should(sh => sh
.Match(m => m
.Field("field1")
.Query("word1")
)
)
)
)
);
我尝试构建一个带有必须和应该请求的 elasticsearch 请求 我的目标要求如下:
POST /index/type/_search
{
"query": {
"bool" : {
"must" : {
"match":{"field1":{"query":"word1"}}
},
"should":{
"match":{"field2":{"query":"word2"}}
}
}
}}
我写了:
var queryContainer = new QueryContainer();
queryContainer &= new MatchQuery() { Field = "field1", Query = "word1" };
queryContainer |= new MatchQuery() { Field = "field2", Query = "word2"};
var searchRequest = new SearchRequest<type>
{
Query = queryContainer
};
但它产生了我:
POST /index/type/_search
{
"query":{
"bool":{
"should":[
{"match":{"field1":{"query":"word1"}}},
{"match":{"field2":{"query":"word2"}}}]
}
}
}
你知道在构建我的 QueryContainer 时我应该写些什么吗?
在这里使用运算符重载没有多大帮助;我认为在这种情况下,构建 bool
查询更清晰
对象初始化器语法
var searchRequest = new SearchRequest<Document>()
{
Query = new BoolQuery
{
Must = new QueryContainer[] { new MatchQuery() { Field = "field1", Query = "word1" } },
Should = new QueryContainer[] { new MatchQuery() { Field = "field2", Query = "word2" } }
}
};
client.Search<Document>(searchRequest);
或流利API
client.Search<Document>(s => s
.Query(q => q
.Bool(b => b
.Must(fi => fi
.Match(m => m
.Field("field2")
.Query("word2")
)
)
.Should(sh => sh
.Match(m => m
.Field("field1")
.Query("word1")
)
)
)
)
);