Elasticsearch安装

docker 安装(Elasticsearch安装需要的内存在2G以上)
1,下载镜像:docker pull elasticsearch:7.13.2

2,运行容器:docker run -d -p 9200:9200 -p 9300:9300 —name elasticsearch-7.13.2 elasticsearch:7.13.2
进入容器:docker exec -it elasticsearch-7.13.2 /bin/bash
安装 ik 分词器:./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.13.2/elasticsearch-analysis-ik-7.13.2.zip
修改 es 配置文件:`vi ./config/elasticsearch.yml

  1. cluster.name: "docker-cluster"
  2. network.host: 0.0.0.0
  3. # minimum_master_nodes need to be explicitly set when bound on a public IP
  4. # set to 1 to allow single node clusters
  5. # Details: https://github.com/elastic/elasticsearch/pull/17288
  6. discovery.zen.minimum_master_nodes: 1
  7. # just for elasticsearch-head plugin
  8. http.cors.enabled: true
  9. http.cors.allow-origin: "*"

退出容器:exit

停止容器:docker stop elasticsearch-7.13.2

启动容器:docker start elasticsearch-7.13.2

Elasticsearch介绍

ElasticSearch 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/6.x/getting-started.html
spring-data-elasticsearch 官方文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.2.RELEASE/reference/html/

Elasticsearch是一个开源的分布式、RESTful 风格的搜索和数据分析引擎,它的底层是开源库Apache Lucene。

Elasticsearch使用

引入依赖

  1. <properties>
  2. <maven.compiler.source>8</maven.compiler.source>
  3. <maven.compiler.target>8</maven.compiler.target>
  4. <!-- 修改elasticsearch的版本 -->
  5. <elasticsearch.version>7.6.2</elasticsearch.version>
  6. </properties>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  15. </dependency>
  16. <!-- spring-boot-starter-data-elasticsearch的依赖中已经引入了当前依赖,因此只需要修改相关的版本即可
  17. <dependency>
  18. <groupId>org.elasticsearch.client</groupId>
  19. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  20. <version>7.6.2</version>
  21. </dependency>
  22. -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-test</artifactId>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>

添加配置application.yml

  1. spring:
  2. es:
  3. url: 127.0.0.1
  4. scheme: http
  5. port: 9200
  6. index: person_index

本文使用SpringBoot结合RestHighLevelClient来讲解高级客户端的使用。
配置文件写好后,使用使用一个ElasticSearchConfig实例化创建一个RestHighLevelClient的Bean实例,使用注解将配置文件的值注入到类中,创建RestHighLevelClient对象时可以看到,内部其实是创建了一个低级的客户端RestClient来实现,传入ElasticSearch的服务器地址和端口号。

  1. @Configuration
  2. public class EsClientConfig {
  3. @Value("${spring.es.url}")
  4. private String url;
  5. @Value("${spring.es.scheme}")
  6. private String scheme;
  7. @Value("${spring.es.port}")
  8. private Integer port;
  9. @Bean
  10. public RestHighLevelClient highLevelClient(){
  11. RestHighLevelClient client = new RestHighLevelClient(
  12. RestClient.builder(
  13. new HttpHost(url, port, scheme)));
  14. return client;
  15. }
  16. }

引用RestHighLevelClient对象
在需要用到ElasticSearch的类中注入索引名字、类型名字和高级客户端对象。