当前位置:   article > 正文

java-es 搜索条件拼接 SearchSourceBuilder

searchsourcebuilder

1,根据字段数据去重

CollapseBuilder collapseBuilder = new CollapseBuilder("?");
sourceBuilder.collapse(collapseBuilder);

2,排序

super.sourceBuilder.sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC));

3,设置搜索分页

sourceBuilder.from((searchDto.getPageIndex() - 1) * searchDto.getPageSize()).size(searchDto.getPageSize());

4,搜索条件拼接

.boost() 设置条件等级    .caseInsensitive(true)忽略查询大小写
  1. BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder()
  2. .must(QueryBuilders.boolQuery()
  3. .must(QueryBuilders.boolQuery()
  4. .should(QueryBuilders.wildcardQuery("title.keyword", searchDto.getKeyword()).boost(5).caseInsensitive(true))
  5. .should(QueryBuilders.wildcardQuery("title.keyword", key).caseInsensitive(true)))
  6. .must(QueryBuilders.boolQuery()
  7. .should(QueryBuilders.boolQuery()
  8. .must(QueryBuilders.termQuery("historyIsDelete", false))
  9. .must(QueryBuilders.termQuery("typeId", "1"))
  10. .must(QueryBuilders.termQuery("playBackRecording", 1))
  11. .must(QueryBuilders.rangeQuery("saveRecordingTime").gte(time))
  12. .must(QueryBuilders.termQuery("transcodingStatus", 1)))
  13. .should(QueryBuilders.boolQuery()
  14. .must(QueryBuilders.termQuery("channelIsDelete", false))
  15. // .must((QueryBuilders.rangeQuery("beginTime").gte(date)))
  16. .must((QueryBuilders.boolQuery().should((QueryBuilders.rangeQuery("liveBeginTime").gte(date)))
  17. .should(QueryBuilders.termQuery("isReserve", 1))))))
  18. );
  19. sourceBuilder.query(boolQueryBuilder);

效果如下

{"from":0,"size":4,"query":{"bool":{"must":[{"bool":{"must":[{"bool":{"should":[{"wildcard":{"title.keyword":{"wildcard":"2023","case_insensitive":true,"boost":5.0}}},{"wildcard":{"title.keyword":{"wildcard":"*2023*","case_insensitive":true,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"bool":{"should":[{"bool":{"must":[{"term":{"historyIsDelete":{"value":false,"boost":1.0}}},{"term":{"typeId":{"value":"1","boost":1.0}}},{"term":{"playBackRecording":{"value":1,"boost":1.0}}},{"range":{"saveRecordingTime":{"from":1676451404,"to":null,"include_lower":true,"include_upper":true,"boost":1.0}}},{"term":{"transcodingStatus":{"value":1,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},{"bool":{"must":[{"term":{"channelIsDelete":{"value":false,"boost":1.0}}},{"bool":{"should":[{"range":{"liveBeginTime":{"from":"2023-02-15T08:56:44.723Z","to":null,"include_lower":true,"include_upper":true,"boost":1.0}}},{"term":{"isReserve":{"value":1,"boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"sort":[{"createTime":{"order":"desc"}}]}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/68274
推荐阅读
相关标签
  

闽ICP备14008679号