SpringBoot -ElasticSearch RestHighLevelClient 高级客户端使用(3) 文档操作

前一章讲过如何将SpringBoot -ElasticSearch的高级客户端RestHighLevelClient 的Index操作

这一章是使用RestHighLevelClient 进行document操作

之前说过es7.X会移除type,所以以下操作中 index和type保持一致,为后续升级提供便利

插入

 public Integer documentInsert(String indexName, Map<String, Object> data) {
        IndexRequest indexRequest = new IndexRequest(indexName, indexName);
        indexRequest.source(JSON.toJSONString(data), XContentType.JSON);
        try {
            rhlClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }

更新

    public Integer documentUpdate(String indexName, Map<String, Object> data, String id) {
        UpdateRequest updateRequest = new UpdateRequest(indexName, indexName, id);
        updateRequest.doc(data);
        try {
            rhlClient.update(updateRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }

删除

 public Integer documentDelete(String indexName, String id) {
        SysTable table = tableMapper.select(tableId);
        DeleteRequest deleteRequest = new DeleteRequest(indexName, indexName, id);
        try {
            rhlClient.delete(deleteRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }

查询

public List<Map<String, Object>> documentQuery(String indexName, Map<String, Object> query) {
        List<Map<String, Object>> result = Lists.newArrayList();
        SearchRequest searchRequest = new SearchRequest(indexName);
        searchRequest.types(indexName);
        queryBuilder(null, null, query, indexName, searchRequest);
        try {
            SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : response.getHits().getHits()) {
                Map<String, Object> map = hit.getSourceAsMap();
                map.put("id", hit.getId());
                result.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

private void queryBuilder(Integer pageIndex, Integer pageSize, Map<String, Object> query, 
String indexName, SearchRequest searchRequest) {
        if (query != null && !query.keySet().isEmpty()) {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            if (pageIndex != null && pageSize != null) {
                searchSourceBuilder.size(pageSize);
                if (pageIndex <= 0) {
                    pageIndex = 0;
                }
                searchSourceBuilder.from((pageIndex - 1) * pageSize);
            }
            BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
            query.keySet().forEach(key -> {
                ESDataTypeEnum type = ESDataTypeEnum.TEXT;
                boolBuilder.must(QueryBuilders.matchQuery(key, query.get(key)));
                
            });
            searchSourceBuilder.query(boolBuilder);
            searchRequest.source(searchSourceBuilder);
        }
    }

分页查询

public PageInfo<Map<String, Object>> documentPage(Integer pageIndex, Integer pageSize, String indexName, Map<String, Object> data) {
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>();
        List<Map<String, Object>> result = Lists.newArrayList();
        SearchRequest searchRequest = new SearchRequest(indexName);
        searchRequest.types(indexName);
        queryBuilder(pageIndex, pageSize, data, indexName, searchRequest);
        try {
            SearchResponse response = rhlClient.search(searchRequest, RequestOptions.DEFAULT);
            for (SearchHit hit : response.getHits().getHits()) {
                Map<String, Object> map = hit.getSourceAsMap();
                map.put("id", hit.getId());
                result.add(map);
            }
            pageInfo.setPageSize(pageSize);
            pageInfo.setPageNum(pageIndex);
            pageInfo.setTotal(response.getHits().getTotalHits());
            pageInfo.setList(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

批量插入

 public Integer documentBatchInsert(String indenName, List<Map<String, Object>> data) {
        BulkRequest bulkRequest = new BulkRequest();
        data.forEach(datum -> {
            IndexRequest indexRequest = new IndexRequest(indenName, indenName);
            indexRequest.source(JSON.toJSONString(datum), XContentType.JSON);
            bulkRequest.add(indexRequest);
        });
        try {
            rhlClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data.size();
    }

你可能感兴趣的:(java)