我正在尝试在代码中生成以下查询:
{
"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"
}
}
}
]
}
}
}
我想念的是什么...?
答案 0 :(得分:1)
首先,您使用的是.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();