栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch索引、文档通用类

elasticsearch索引、文档通用类

index操作类
package com.keyrus.search.controller;

import com.keyrus.commonutils.exception.ApiException;
import com.keyrus.commonutils.model.ResultResponse;
import com.keyrus.commonutils.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.unit.Timevalue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "es索引操作")
@RestController
@RequestMapping(value = "/elasticsearch")
public class ElasticsearchIndexController {

	// spring集成的elasticsearch工具类(也可以用elasticsearch依赖自己封装)
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    
    @ApiOperation(value = "索引模糊查询", response = ResultResponse.class)
    @GetMapping(value = "/queryIndexAll", produces = {"application/json"})
    public ResultResponse queryIndexAll(
            @ApiParam(value = "索引名称") String index
    ) {
        if (index == null) {
            index = "**";
        } else {
            index = "*" + index + "*";
        }
        log.info("索引index:" + index);
        ResultResponse response = new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        GetIndexRequest request = new GetIndexRequest(index);
        try {
            GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
            response.setData(getIndexResponse.getIndices());
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        return response;
    }

    
    @ApiOperation(value = "添加索引", response = ResultResponse.class)
    @GetMapping(value = "/addIndex", produces = {"application/json"})
    public ResultResponse addIndex(
            @ApiParam(value = "索引名称", required = true) String index
    ) {
        log.info("索引index:" + index);
        if (index == null || "".equals(index)) {
            throw new ApiException("索引不可为空");
        }
        // 检索索引是否已存在
        GetIndexRequest existsRequest = new GetIndexRequest("twitter");
        try {
            boolean exists = restHighLevelClient.indices().exists(existsRequest, RequestOptions.DEFAULT);
            if (exists) {
                throw new ApiException("索引已存在");
            }
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest(index);
        request.setTimeout(Timevalue.timevalueMinutes(2));
        try {
            restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "OK");
    }

    
    @ApiOperation(value = "删除索引", response = ResultResponse.class)
    @GetMapping(value = "deleteIndex", produces = {"application/json"})
    public ResultResponse deleteIndex(
            @ApiParam(value = "索引名称", required = true) String index
    ) {
        log.info("索引index:" + index);
        DeleteIndexRequest request = new DeleteIndexRequest(index);
        try {
            restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "OK");
    }
}

document操作类
package com.keyrus.search.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.keyrus.commonutils.exception.ApiException;
import com.keyrus.commonutils.model.ResultResponse;
import com.keyrus.commonutils.response.ResultCode;
import com.keyrus.commonutils.util.ElasticsearchQuery;
import com.keyrus.commonutils.util.ElasticsearchUnit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Timevalue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Api(tags = "es文档操作")
@RestController
@RequestMapping(value = "/elasticsearchdocument")
public class ElasticsearchdocumentController {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    
    @ApiOperation(value = "增加文档", response = ResultResponse.class)
    @PostMapping(value = "/adddocument", produces = {"application/json"})
    public ResultResponse adddocument(
            @ApiParam(value = "增加信息", required = true) @RequestBody ElasticsearchUnit unit
    ) {
        if (unit == null) {
            throw new ApiException("服务器错误");
        }
        if (unit.getIndex() == null || "".equals(unit.getIndex())) {
            throw new ApiException("请输入正确的索引");
        }
        if (unit.getList() == null || unit.getList().size() <= 0) {
            throw new ApiException("文档内容为空");
        }
        // 批量操作
        BulkRequest request = new BulkRequest();
        unit.getList().forEach((t -> request.add(new IndexRequest(unit.getIndex()).source(JSON.toJSonString(t), XContentType.JSON))));
        try {
            restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "ERROR");
        }
        return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "OK");
    }

    
    @ApiOperation(value = "删除文档", response = ResultResponse.class)
    @PostMapping(value = "/deletedocument", produces = {"application/json"})
    public ResultResponse deletedocument(
            @ApiParam(value = "删除信息", required = true) @RequestBody ElasticsearchUnit unit
    ) {
        if (unit == null) {
            throw new ApiException("服务器错误");
        }
        if (unit.getIndex() == null || "".equals(unit.getIndex())) {
            throw new ApiException("请输入正确的索引");
        }
        if (unit.getdocumentIds() == null || unit.getdocumentIds().size() <= 0) {
            throw new ApiException("文档为空");
        }
        // 批量操作
        BulkRequest request = new BulkRequest();
        unit.getdocumentIds().forEach((id -> request.add(new DeleteRequest(unit.getIndex(), id + ""))));
        try {
            restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "ERROR");
        }
        return new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), "OK");
    }

    
    @ApiOperation(value = "根据文档id获取文档内容", response = ResultResponse.class)
    @GetMapping(value = "/getBydocumentId", produces = {"application/json"})
    public ResultResponse getBydocumentId(
            @ApiParam(value = "索引", required = true) String index,
            @ApiParam(value = "文档id", required = true) String documentId
    ) {
        log.info("index:" + index + " ==== documentId:" + documentId);
        ResultResponse response = new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
        GetRequest getRequest = new GetRequest(index, documentId);
        try {
            GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            response.setData(getResponse.getSource());
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        return response;
    }

    
    @ApiOperation(value = "根据关键字获取文档", response = ResultResponse.class)
    @PostMapping(value = "/retrievaldocuments", produces = {"application/json"})
    public ResultResponse retrievaldocuments(
            @RequestBody ElasticsearchQuery query
    ) {
        log.info("index:" + query.getIndex() + " ==== documentId:" + query.getKeyword());
        ResultResponse response = new ResultResponse(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());

        SearchRequest searchRequest = new SearchRequest(query.getIndex());

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 其他查询条件
        searchSourceBuilder.from((query.getPageNum() - 1) * query.getPageSize()); // 从page开始
        searchSourceBuilder.size(query.getPageSize()); // 查询pageSize条数据
        searchSourceBuilder.timeout(new Timevalue(60 , TimeUnit.MINUTES)); // 设置超时时间
        searchRequest.source(searchSourceBuilder);

        try {
            PageInfo> pageInfo = new PageInfo<>();
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            List> list = new ArrayList<>();
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                Map map = hit.getSourceAsMap();
                map.put("documentId", hit.getId());
                list.add(map);
            }
            pageInfo.setList(list);
            pageInfo.setTotal(hits.length);
            pageInfo.setPageNum(query.getPageNum());
            pageInfo.setPageSize(query.getPageSize());
            response.setData(pageInfo);
        } catch (Exception e) {
            return new ResultResponse(ResultCode.NETWORK_ERROR.getCode(), ResultCode.NETWORK_ERROR.getMsg(), "ERROR");
        }
        return response;
    }
}

ElasticsearchUnit
package com.keyrus.commonutils.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@ApiModel(value = "elasticsearch")
@Getter
@Setter
public class ElasticsearchUnit {

    @ApiModelProperty(value = "索引")
    private String index;

    @ApiModelProperty(value = "文档id")
    private List documentIds;

    @ApiModelProperty(value = "文档集合")
    private List list;
}

ElasticsearchQuery
package com.keyrus.commonutils.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@ApiModel(value = "elasticsearch查询条件")
@Getter
@Setter
public class ElasticsearchQuery {

    @ApiModelProperty(value = "pageNum")
    private Integer pageNum;

    @ApiModelProperty(value = "pageSize")
    private Integer pageSize;

    @ApiModelProperty(value = "索引")
    private String index;

    @ApiModelProperty(value = "搜索关键字")
    private String keyword;
}

pagehelper
com.github.pagehelper
这个是github上的分页插件,可以自己封装一个分页通用实体类来替换(pageNum、pageSize、total三个字段)
maven
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号