这几天又遇到个问题,elastic的数据结构差的不多如下:
"labelInfo": [
{
"id":1/*产品推荐ID*/
"labelId":1,
"departDate":"2015-11-28",
"labelName":"主推",
"labelNotes":"标签备注",
"updateUserName":"谁",
"updateTime":"2015-11-0611: 05: 00"
},
{
"id":2/*产品推荐ID*/
"labelId":-1,
"departDate":"2015-11-30",
"labelName":"主推",
"labelNotes":"标签备注",
"updateUserName":"谁",
"updateTime":"2015-11-0611: 05: 00"
}
]
是一个list的结构,业务需求是查询labelId为1同时日期在2015-11-29之后的,一直不知道怎么组装querybuilder才能满足这条件,直到今天百度上搜搜看到有个国外老司机在NestedQueryBuilder里又放了个BoolQueryBuilder 陡然茅塞顿开啊。。(链接手残关掉了,历史记录都没找到。。)部分代码如下:
if (request.get("proLabelList") != null && !labelList.isEmpty()) {
BoolQueryBuilder bQueryBuilder = new BoolQueryBuilder();
bQueryBuilder.must(new TermsQueryBuilder("labelInfo.labelId", labelList));
if (!StringUtil.isEmptyString(startDate) && !StringUtil.isEmptyString(endDate)) {
bQueryBuilder.must(new RangeQueryBuilder("labelInfo.departDate").gte(startDate).lte(endDate));
} else if (!StringUtil.isEmptyString(startDate) && StringUtil.isEmptyString(endDate)) {
bQueryBuilder.must(new RangeQueryBuilder("labelInfo.departDate").from(startDate));
} else if (StringUtil.isEmptyString(startDate) && !StringUtil.isEmptyString(endDate)) {
bQueryBuilder.must(new RangeQueryBuilder("labelInfo.departDate").to(endDate));
}
boolQueryBuilder.must(new NestedQueryBuilder("labelInfo", bQueryBuilder));
}