Elasticsearch Java Jest 客户端查询生成器范围与 bool 查询
Elasticsearch Java Jest client query builder range with bool query
我需要使用 Jest 客户端进行 Elasticsearch 查询,以将一些术语和日期与范围查询相匹配。因此,我需要使用 Jest QueryBuilder
执行带有范围查询的 bool 查询,以获得这样的请求:
{
"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
已经有一个 boolquery,我无法向其添加范围查询。
您需要如下范围查询:
QueryBuilder rangeQ = QueryBuilders
.rangeQuery("begindate")
.from(5)
.to(10);
然后将两个查询与 should 结合起来:
QueryBuilder qb = QueryBuilders
.boolQuery()
.should(rangeQ)
.should(boolQ);
看看这个 post:How to construct a boolquery dynamically in java api? 了解更多信息。
试试这个。
QueryBuilder rangeQuery = QueryBuilders
.rangeQuery("field")
.from("2016-01-01||/D")
.to("2017-01-01||/D")
.includeLower(true)
.includeUpper(false);
QueryBuilder termsQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("field1","12"))
.must(QueryBuilders.termQuery("field2", "abc"))
.must(QueryBuilders.termQuery("field3", "def"));
QueryBuilder qb = QueryBuilders
.boolQuery()
.should(rangeQuery)
.should(termsQuery);
我需要使用 Jest 客户端进行 Elasticsearch 查询,以将一些术语和日期与范围查询相匹配。因此,我需要使用 Jest QueryBuilder
执行带有范围查询的 bool 查询,以获得这样的请求:
{
"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
已经有一个 boolquery,我无法向其添加范围查询。
您需要如下范围查询:
QueryBuilder rangeQ = QueryBuilders
.rangeQuery("begindate")
.from(5)
.to(10);
然后将两个查询与 should 结合起来:
QueryBuilder qb = QueryBuilders
.boolQuery()
.should(rangeQ)
.should(boolQ);
看看这个 post:How to construct a boolquery dynamically in java api? 了解更多信息。
试试这个。
QueryBuilder rangeQuery = QueryBuilders
.rangeQuery("field")
.from("2016-01-01||/D")
.to("2017-01-01||/D")
.includeLower(true)
.includeUpper(false);
QueryBuilder termsQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("field1","12"))
.must(QueryBuilders.termQuery("field2", "abc"))
.must(QueryBuilders.termQuery("field3", "def"));
QueryBuilder qb = QueryBuilders
.boolQuery()
.should(rangeQuery)
.should(termsQuery);