我需要使用Jest客户端进行Elasticsearch查询,以便将一些术语和日期与范围查询相匹配。因此,我需要使用Jest对范围查询执行布尔查询
QueryBuilder
有这样的请求:
{
"query": {
"range": {
"gte": "begindate",
"lte": "enddate"
},
"bool": {
"must": [
{
"terms": {
"field1": [
55,
99
]
}
},
{
"terms": {
"field2": [
450
]
}
},
{
"terms": {
"field3": [
11
]
}
}
]
}
}
}
为此,我使用
queryBuilder
像这样:
Builder search = null;
Search buildedSearch = null;
SearchResult result = null;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder qb = QueryBuilders.boolQuery();
if (field1 != null) {
qb.must(QueryBuilders.termsQuery("field1", field1));
}
if (field2 != null) {
qb.must(QueryBuilders.termsQuery("field2", field2));
}
if (field3 != null) {
qb.must(QueryBuilders.termsQuery("field3", field3));
}
String query = searchSourceBuilder.query(qb).toString();
if (field != null) {
search = new Search.Builder(query).addIndex(index).addType(type);
if (beginIndex != -1) {
search.setParameter(Parameters.FROM, beginIndex);
}
if (endIndex != -1) {
search.setParameter(Parameters.SIZE, endIndex);
}
buildedSearch = search.build();
}
try {
result = client.execute(buildedSearch);
} catch (IOException e) {
LOGGER.info("Can't found result");
}
如何在搜索生成器上添加范围查询,因为我的对象
qb
已经有一个布尔查询,我无法向其中添加范围查询。