2021-09-15

springboot整合es

1.pom版本需要与es版本一致,避免不必要的问题
2.pom,在你的项目上加上以下依赖

	<properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.14.1</elasticsearch.version>
    </properties>


	<dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
  1. yml配置
	spring:
		elasticsearch:
    		rest:
    			//集群url1:port,url2:port,url3:port
      			uris: 127.0.0.1:9200
      			

4.现在已经整好了,为了方便写一个utils,如工具类不够自己补足自己业务常用的简单封装即可

package com.hzy.work.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 胡智阳
 * @date 2021-09-14 11:44
 * @desc es工具类  
 */
@Component
public class ESUtil {

    @Autowired
    private RestHighLevelClient client;

    /**
     * 索引新增
     *
     * @param indexName
     * @throws IOException
     */
    public void indexAdd(String indexName) throws IOException {
        CreateIndexRequest indexRequest = new CreateIndexRequest(indexName);
        client.indices().create(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 校验索引是否存在
     *
     * @param indexName
     * @return
     * @throws IOException
     */
    public boolean indexExist(String indexName) throws IOException {
        GetIndexRequest indexRequest = new GetIndexRequest(indexName);
        return client.indices().exists(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 删除索引
     *
     * @param indexName
     * @throws IOException
     */
    public void indexDelete(String indexName) throws IOException {
        DeleteIndexRequest indexRequest = new DeleteIndexRequest(indexName);
        client.indices().delete(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 增加一条记录
     *
     * @param indexName
     * @param obj
     * @throws IOException
     */
    public void docAddOne(String indexName, Object obj) throws IOException {
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(JSON.toJSONString(obj), XContentType.JSON);
        client.index(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 增加一条记录
     *
     * @param indexName
     * @param id
     * @param obj
     * @throws IOException
     */
    public void docAddOne(String indexName, String id, Object obj) throws IOException {
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.id(id);
        indexRequest.source(JSON.toJSONString(obj), XContentType.JSON);
        client.index(indexRequest, RequestOptions.DEFAULT);
    }

    /**
     * 判断文档是否存在
     *
     * @param indexName
     * @param id
     * @return
     * @throws IOException
     */
    public boolean docExist(String indexName, String id) throws IOException {
        GetRequest request = new GetRequest(indexName, id);
        request.fetchSourceContext(new FetchSourceContext(false));
        return client.exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 根据索引、id获取文档
     *
     * @param indexName
     * @param id
     * @return
     * @throws IOException
     */
    public Map<String, Object> docGetById(String indexName, String id) throws IOException {
        GetRequest request = new GetRequest(indexName, id);
        return client.get(request, RequestOptions.DEFAULT).getSource();
    }

    /**
     * 更新文档
     *
     * @param indexName
     * @param id
     * @param obj
     * @throws IOException
     */
    public void docUpdateById(String indexName, String id, Object obj) throws IOException {
        UpdateRequest updateRequest = new UpdateRequest(indexName, id);
        updateRequest.doc(JSON.toJSONString(obj), XContentType.JSON);
        client.update(updateRequest, RequestOptions.DEFAULT);
    }

    /**
     * 删除文档
     *
     * @param indexName
     * @param id
     * @throws IOException
     */
    public void docDelete(String indexName, String id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
        client.delete(deleteRequest, RequestOptions.DEFAULT);
    }

    /**
     * 批量插入
     * id随机
     *
     * @param indexName
     * @param list
     * @throws IOException
     */
    public void docAddList(String indexName, List<Object> list) throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        for (int i = 0; i < list.size(); i++) {
            bulkRequest.add(new IndexRequest(indexName).source(JSON.toJSONString(list.get(i)), XContentType.JSON));
        }
        client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }


    /**
     * 查找
     *
     * @param searchRequest
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> docFind(SearchRequest searchRequest) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值