gpt4 book ai didi

elasticsearch - 在代码中生成查询主体

转载 作者:行者123 更新时间:2023-12-03 01:37:15 25 4
gpt4 key购买 nike

我正在尝试在代码中生成以下查询:

{
"query": {
"bool": {
"should": [
{
"match": {
"title1": "brown"
}
},
{
"match": {
"title2": "fox"
}
},
{
"match": {
"title3": "dog"
}
}
]
}
}
}

这是我用 QueryBuilders构建的Java代码
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 
boolQueryBuilder.must(matchQuery("title1", "brown"));
boolQueryBuilder.must(matchQuery("title2", "fox"));
boolQueryBuilder.must(matchQuery("title3", "dog")); SearchSourceBuilder
searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);

String s = searchSourceBuilder.toString();

而且我没有得到相同的字符串...我得到的是:
  {
"query": {
"bool": {
"must": [
{
"match": {
"title1": {
"query": "brown",
"type": "boolean"
}
}
},
{
"match": {
"title2": {
"query": "fox",
"type": "boolean"
}
}
},
{
"match": {
"title3": {
"query": "dog",
"type": "boolean"
}
}
}
]
}
}

}

我想念什么...?

最佳答案

首先,您使用的是.must,因此您必须拥有而不应该拥有。

这是实现您想要做的事的一个例子

MatchQueryBuilder match1 = QueryBuilders.matchQuery("title1", "brown");
MatchQueryBuilder match2 = QueryBuilders.matchQuery("title2", "fox");
MatchQueryBuilder match3 = QueryBuilders.matchQuery("title3", "dog");

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(match1);
boolQueryBuilder.should(match2);
boolQueryBuilder.should(match3);

String queryString = boolQueryBuilder.toString();

关于elasticsearch - 在代码中生成查询主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51382464/

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