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