SpringBoot整合Spring Data Elasticsearch

版本
  • SpringBoot:2.2.5.RELEASE
  • Elasticsearch:6.8.8
  • Jdk:1.8
  • Maven:3.5.2
  • Idea:2019.3
选型
Spring Data ElasticsearchElasticsearchSpring Boot
3.2.x6.8.42.2.x
3.1.x6.2.22.1.x
3.0.x5.5.02.0.x
2.1.x2.4.01.5.x
依赖
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
部署

部署Elasticsearch及Kibana(选配)参考下面链接快速开始

食用

0:创建Elasticsearch实体对象

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.io.Serializable;

/**
 * @author liujiazhong
 */
@Data
@Mapping(mappingPath = "/mapping/mapping.json")
@Setting(settingPath = "/mapping/setting.json")
@Document(indexName = OrderEntity.INDEX, type = OrderEntity.TYPE)
public class OrderEntity implements Serializable {

    public static final String INDEX = "demo_order";
    public static final String TYPE = "order";

    @Id
    @Field(type = FieldType.Long)
    private Long id;

    @Field(type = FieldType.Long)
    private Long createTime;

    @Field(type = FieldType.Long)
    private Long updateTime;

    @Field(type = FieldType.Keyword)
    private String orderCode;

    @Field(type = FieldType.Long)
    private Long sellerId;

    @MultiField(
            mainField = @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart"),
            otherFields = {
                    @InnerField(suffix = "keyword", type = FieldType.Keyword)
            }
    )
    private String storeName;
    
}
  • 分片数可以直接在@Document注解中进行指定,也可以写在setting.json文件中,然后使用@Setting引入;mapping同理
  • 自定义分词器可以写在setting.json文件中
  • 单字段属性直接使用@Field注解,指定字段类型、是否索引等;多字段属性可以使用@MultiField注解,指定不同字段类型、分词器等
  • Elasticsearch7.0开始正式废除索引下的type属性,这里使用的6.x所以需要指定type

1:使用Spring Data Elasticsearch提供的Repository操作Elasticsearch

import com.liu.gardenia.demo.domain.es.OrderEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * @author liujiazhong
 */
public interface OrderEsRepository extends ElasticsearchRepository<OrderEntity, Long> {

}

  • Spring Data Elasticsearch封装了对Elasticsearch的一些基本操作,直接定义一个接口来继承Repository即可

2:使用High Level REST Client操作Elasticsearch (推荐)

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liujiazhong
 */
@Slf4j
public abstract class AbstractCommonService {

    @Autowired
    private RestHighLevelClient client;

    <S, T> List<T> mapByCopy(List<S> source, Class<T> clazz) {
        if (CollectionUtils.isEmpty(source)) {
            return Collections.emptyList();
        }
        List<T> target = new ArrayList<>(source.size());
        try {
            for (S s : source) {
                T t = clazz.newInstance();
                BeanUtils.copyProperties(s, t);
                target.add(t);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return target;
    }

    BulkResponse bulk(BulkRequest bulkRequest) {
        try {
            return client.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

注入RestHighLevelClient

import com.alibaba.fastjson.JSON;
import com.liu.gardenia.demo.domain.es.OrderEntity;
import com.liu.gardenia.demo.external.OrderDataClient;
import com.liu.gardenia.domain.data.vo.OrderInfoBO;
import com.liu.gardenia.domain.data.vo.query.OrderInfoQueryBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liujiazhong
 */
@Slf4j
@Service
public class OrderService extends AbstractCommonService {

    private final OrderDataClient orderDataClient;

    public OrderService(OrderDataClient orderDataClient) {
        this.orderDataClient = orderDataClient;
    }

    public void syncOrderToEs(OrderInfoQueryBO query) {
        List<OrderInfoBO> orders = orderDataClient.listOrder(query);
        List<OrderEntity> entities = mapByCopy(orders, OrderEntity.class);
        if (CollectionUtils.isEmpty(entities)) {
            log.info("no data to sync...");
            return;
        }
        log.info("begin to sync...");
        BulkRequest bulkRequest = new BulkRequest();
        for (OrderEntity entity : entities) {
            IndexRequest indexRequest = new IndexRequest(OrderEntity.INDEX, OrderEntity.TYPE, entity.getId().toString())
                    .source(JSON.toJSONString(entity), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        BulkResponse responses = bulk(bulkRequest);
        if (responses.hasFailures()) {
            log.info("response:{}", responses.buildFailureMessage());
        }
        log.info("sync success...size:{}", entities.size());
    }

}

使用bulk api批量导入数据到Elasticsearch

总结

SpringBoot整合Spring Data Elasticsearch

链接

Elasticsearch快速开始:https://blog.csdn.net/momo57l/article/details/104293479
Kibana快速开始:https://blog.csdn.net/momo57l/article/details/104294114
Spring Data Elasticsearch:https://spring.io/projects/spring-data-elasticsearch#overview
Github:https://github.com/spring-projects/spring-data-elasticsearch

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值