gpt4 book ai didi

elasticsearch - nest:使用must和should约束构建QueryContainer请求

转载 作者:行者123 更新时间:2023-12-02 22:58:56 25 4
gpt4 key购买 nike

我尝试建立一个带有must和must请求的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);

或Fluent 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 - nest:使用must和should约束构建QueryContainer请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41934105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com