版本
- SpringBoot:2.2.5.RELEASE
- Elasticsearch:6.8.8
- Jdk:1.8
- Maven:3.5.2
- Idea:2019.3
选型
Spring Data Elasticsearch | Elasticsearch | Spring Boot |
---|---|---|
3.2.x | 6.8.4 | 2.2.x |
3.1.x | 6.2.2 | 2.1.x |
3.0.x | 5.5.0 | 2.0.x |
2.1.x | 2.4.0 | 1.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