ES在java中的使用

ES

查询单个(String)

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.filter(QueryBuilders.termQuery("id", test.getId()));

查询多个(List)

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termsQuery("status", test.getStatus()));

模糊查询

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.wildcardQuery("value", "*" + test.getKeyword() + "*"));

大于

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.filter(QueryBuilders.rangeQuery("createDt").gt(test.getCreateDtStart()));

小于

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();           boolQueryBuilder.filter(QueryBuilders.rangeQuery("createDt").lt(test.getCreateDtEnd()));

查询

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);
sourceBuilder.storedField("id");
SearchRequest searchRequest = new SearchRequest("testIndex");
searchRequest.source(sourceBuilder);
try {
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    List ids = new ArrayList<>();
    searchResponse.getHits().forEach(hit -> {
        String id = hit.field("id").getValue();
        if (StringUtils.isNotEmpty(id)) {
            ids.add(id);
        }
    });
} catch (Exception e) {
    throw new Exception();
}    

分页

sourceBuilder.from(Math.max(0, test.getPageIndex() - 1) * test.getPageSize());
sourceBuilder.size(test.getPageSize());

排序

// 正序
sourceBuilder.sort("create_dt", SortOrder.ASC);
// 倒序
sourceBuilder.sort("create_dt", SortOrder.DESC);

你可能感兴趣的:(Spring,elasticsearch,大数据,搜索引擎)