7、Spring Boot检索

1.ElasticSearch简介

  Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也是采用了ElasticSearch作为其搜索服务。

  我们经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。因为它可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持。

2.ElasticSearch概念  

 

 

3.ElasticSearch搭建

(1).docker搭建ElasticSearch

  docker上搭建ElasticSearch环境

[root@hosystem ~]# docker search elasticsearch

NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED

elasticsearch                        Elasticsearch is a powerful open source sear…   4675                [OK]                

[root@hosystem ~]# docker pull elasticsearch:6.8.4

6.8.4: Pulling from library/elasticsearch

d8d02d457314: Pull complete

d3dfcf2fdd68: Pull complete

c8ffe81b87d5: Pull complete

3fdfbe1a7641: Pull complete

dba32acd6977: Pull complete

cf006e61e0b6: Pull complete

a9425533f3f3: Pull complete

Digest: sha256:f4be4f53677e009df3db81f3f6a98908040968bd9a27bdfdf936c651d78e0319

Status: Downloaded newer image for elasticsearch:6.8.4

docker.io/library/elasticsearch:6.8.4

[root@hosystem ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

redis               latest              62f1d3402b78        5 days ago          104MB

rabbitmq            3-management        68898be27496        4 weeks ago         186MB

rabbitmq            latest              ea2bf0a30abf        4 weeks ago         156MB

hello-world         latest              bf756fb1ae65        10 months ago       13.3kB

elasticsearch       6.8.4               25476914cc66        12 months ago       955MB

注:若出现manifest for elasticsearch:latest not found: manifest unknown: manifest unknown问题;

只需要指定版本号即可 elasticsearch:latest

(2).启动ElasticSearch

  启动ElasticSearch的时候我们需要指定内存大小,否则会出现错误.

  由于elasticsearch默认分配jvm空间大小为2g,内存不足分配导致错误.max virtual memory areas vm.max_count(65530) is too low…

[root@hosystem ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

redis               latest              62f1d3402b78        5 days ago          104MB

rabbitmq            3-management        68898be27496        4 weeks ago         186MB

rabbitmq            latest              ea2bf0a30abf        4 weeks ago         156MB

hello-world         latest              bf756fb1ae65        10 months ago       13.3kB

elasticsearch       6.8.4               25476914cc66        12 months ago       955MB

[root@hosystem ~]# docker run -e  ES_JAVA_OPTS=”-Xms256m -Xmx256m” -d -p 9200:9200 -p 9300:9300 –name ES01 25476914cc66

6c91eee08c02a2c3eeda3264d215c73678518881011dc494c121dc4fb658ed06

[root@hosystem ~]# docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                                                         NAMES

6c91eee08c02        25476914cc66            “/usr/local/bin/dock…”   10 seconds ago      Up 7 seconds        0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp                                                                ES01

(3).访问ElasticSearch管理界面

[1].防火墙设置

  如果访问出现我们需要检查下是否配置成功,启动是否成功。还有就是防火墙是否已经配置可允许通过9200端口.

[root@hosystem ~]# firewall-cmd –zone=public –add-port=9200/tcp –permanent

 

[2].vm.max_map_count

[root@hosystem ~]# docker ps -a

CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS                       PORTS                                                                                                         NAMES

7ed1a0faa39a        25476914cc66            “/usr/local/bin/dock…”   About a minute ago   Exited (78) 55 seconds ago                                                                                                                 ES01

[root@hosystem ~]# docker logs -f -t –tail 10 7ed1a0faa39a

#内存不足 //blog.csdn.net/qq_34756221/article/details/105550037

2020-11-02T14:58:05.423337218Z [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

2020-11-02T14:58:05.502461403Z [2020-11-02T14:58:05,497][INFO ][o.e.n.Node               ] [5KFPGEX] closed

2020-11-02T14:58:05.506628526Z [2020-11-02T14:58:05,506][INFO ][o.e.x.m.p.NativeController] [5KFPGEX] Native controller process has stopped – no new native processes can be started

 

4.ElasticSearch使用

  官方文档://www.elastic.co/guide/index.html

(1).下载postmanCanary

(2).创建数据

  通过postman的put请求增加员工数据

 

 

PUT /megacorp/employee/1

{

    “first_name” : “John”,

    “last_name” :  “Smith”,

    “age” :        25,

    “about” :      “I love to go rock climbing”,

    “interests”: [ “sports”, “music” ]

}

 

PUT /megacorp/employee/2

{

    “first_name” :  “Jane”,

    “last_name” :   “Smith”,

    “age” :         32,

    “about” :       “I like to collect rock albums”,

    “interests”:  [ “music” ]

}

 

PUT /megacorp/employee/3

{

    “first_name” :  “Douglas”,

    “last_name” :   “Fir”,

    “age” :         35,

    “about”:        “I like to build cabinets”,

    “interests”:  [ “forestry” ]

}

(3).检索文档

 

   HTTP 命令由 PUT 改为 GET 可以用来检索文档,同样的,可以使用 DELETE 命令来删除文档,以及使用 HEAD 指令来检查文档是否存在。如果想更新已存在的文档,只需再次 PUT .

[1].获取员工

  使用GET请求

 

[2].检验员工是否存在

  使用HEAD请求

 

(4).轻量搜索

[1].搜索所有雇员

GET /megacorp/employee/_search

 

[2].搜索姓氏为Smith的雇员

GET /megacorp/employee/_search?q=last_name:Smith

 

(5).查询表达式

 

参考文档:

//www.elastic.co/guide/index.html

//www.elastic.co/guide/cn/elasticsearch/guide/current/index.html