SpringBoot 整合 ElasticSearch 创建自定

2019-01-26  本文已影响0人  qiyubing

开发环境:

  1. 所用SpringBoot版本:2.0.8.RELEASE
  2. 构建工具: Gradle

首先添加依赖:

implementation('org.springframework.boot:spring-boot-starter-data-elasticsearch')

然后调出依赖列表,查看对应的elasticsearch版本

image.png

能够看到我需要安装的是5.6.14版本的ES

去官网下载对应版本

附上官网各版本的下载页面:
https://www.elastic.co/downloads/past-releases

安装中文分词器ik

官方GitHub的安装教程已经很详细了,附上链接:
https://github.com/medcl/elasticsearch-analysis-ik

注意:

  1. 下载ik插件的zip后用elasticsearch-plugin install安装会报错,在线安装没问题
  2. 用mac的同学需要注意的是苹果的.DS_Store会让elasticsearch-plugin报错,尽量在命令行里操作
  3. 安装完后可以用elasticsearch-plugin list命令查看是否安装成功

GET localhost:9200 查看ES基本信息:

{
    "name": "G0DVm1w",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "ai_dxFHoRjGBnsSxvfHWBw",
    "version": {
        "number": "5.6.14",
        "build_hash": "f310fe9",
        "build_date": "2018-12-05T21:20:16.416Z",
        "build_snapshot": false,
        "lucene_version": "6.6.1"
    },
    "tagline": "You Know, for Search"
}

cluster_name为elasticsearch

之后在SpringBoot中配置

spring:
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
      repositories:
        enabled: true
      cluster-name: elasticsearch

此处的cluster-name就是刚刚查到的

之后编写实体类

@Data
// 指定根据文件构建映射
@Mapping(mappingPath = "mapping/SpuDocMapping.json")
// indexName一般为项目名,type为实体类名,注解的createIndex属性默认为ture,
// SpringBoot启动时会自动创建映射,但要注意如果已经存在相同的index,必须先删除
@Document(indexName = "ynec", type = "spu")
public class SpuDoc implements Serializable {

    private static final long serialVersionUID = -472259879967511922L;

    // 注意一定要加id注解
    @Id
    private Long spu_id;

    private String name;

    private List<String> cat_list;

    private List<String> prop_list;
}

这篇文章对注解的讲解很多,推荐:https://blog.csdn.net/kingice1014/article/details/73166686?utm_source=blogxgwz6

附上我的映射文件:

{
  "mappings": {
    "spu": {
      "properties": {
        "spu_id": {
          "type": "long"
        },
        "name": {
          "type": "text",
          "analyzer": "ik_smart"
        },
        "cat_list": {
          "type": "text",
          "analyzer": "ik_smart"
        },
        "prop_list": {
          "type": "text",
          "analyzer": "ik_smart"
        }
      }
    }
  }
}

因为ES总是存在前后api不兼容,推荐直接去官网学习Mapping写法:
https://www.elastic.co/guide/en/elasticsearch/reference/index.html
这篇中文教程也不错:
https://blog.csdn.net/zx711166/article/details/81667862

编写数据访问层

public interface SpuDocRepo extends ElasticsearchRepository<SpuDoc, Long> {

}

这是JPA提供的通用的接口,还有一种是用ElasticSearchTemplate,这里有教程:
https://blog.csdn.net/tianyaleixiaowu/article/details/76149547/

编写Service和实现类

service接口层省略,下面是实现类:

@Service
public class SearchServiceImpl implements SearchService {

    // 刚刚创建的数据访问层
    @Autowired
    SpuDocRepo spuDocRepo;

    @Override
    public List<Long> searchSpu(String keyword, Integer page, Integer size) {
        // queryStringQuery是匹配_all的全文搜索 
        QueryBuilder queryBuilder = QueryBuilders.queryStringQuery(keyword);
        // 构建分页对象
        Pageable pageable = PageRequest.of(page, size);
        Page<SpuDoc> spuDocPage = spuDocRepo.search(queryBuilder, pageable);
        // 此处我返回的是商品的id列表,可以根据业务自定义
        return spuDocPage.getContent().stream().map(SpuDoc::getSpu_id).collect(Collectors.toList());
    }
}

OK,完啦

上一篇下一篇

猜你喜欢

热点阅读