SpringBoot與搜索框架elasticsearch

搜索知識和搜索框架elasticsearch介紹

    1.常見的搜索方法

  •         mysql:like 模糊,性能問題,
  •         solr:針對企業,Lucene
  •         elasticsearch:針對數據量特別大,PB,TB
                                   純java開發,springboot使用,5.6版本
            es升級4->5版本,改動大,但是5版本後,改動不大

    2.elasticSearch主要特點

  •         特點:全文檢索,結構化檢索,數據統計、分析,接近實時處理,分佈式搜索(可部署數百臺服務器),處理PB級別的數據,搜索糾錯,自動完成
  •         使用場景:日誌搜索,數據聚合,數據監控,報表統計分析
  •         國內外使用者:維基百科,Stack Overflow,GitHub

    3.elasticSearch新特性講解

  •        6.2.x版本基於Lucene 7.x,更快,性能進一步提升,對應的序列化組件,升級到Jackson 2.8
                mysql:database   table   rocord
                es   : index      type(只能存在一個)    document
  •       推薦使用5.0版本推出的Java REST/HTTP客戶端,依賴少,比Transport使用更方便,在基準測試中,性能並不輸於Transport客戶端,
          在5.0到6.0版本中,每次有對應的API更新, 文檔中也說明,推薦使用這種方式進行開發使用,所有可用節點間的負載均衡
          在節點故障和特定響應代碼的情況下進行故障轉移,失敗的連接處罰(失敗的節點是否重試取決於失敗的連續次數;失敗的失敗次數越多,客戶端在再次嘗試同一節點之前等待的時間越長)     
  •        (重要)不再支持一個索引庫裏面多個type,6.x版本已經禁止一個index裏面多個type,所以一個index索引庫只能存在1個type

     4. 官方文檔:
        6.0更新特性:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/release-notes-6.0.0.html#breaking-java-6.0.0
        6.1更新特性:https://www.elastic.co/guide/en/elasticsearch/reference/6.1/release-notes-6.1.0.html

快速部署ElastcSearch5.6.x      

    1.配置JDK1.8
        使用wget 下載elasticsearch安裝包
        wget  https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.tar.gz
    2.解壓
        tar -zxvf elasticsearch-5.6.8.tar.gz
       官網:https://www.elastic.co/products/elasticsearch 
    3.外網訪問配置:    
        config目錄下面elasticsearch.yml
        修改爲 network.host: 0.0.0.0
    4.配置es出現相關問題處理(阿里雲、騰訊雲,亞馬遜雲安裝問題集合):
        ①問題一

            Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
            #
            # There is insufficient memory for the Java Runtime Environment to continue.
            # Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
            # An error report file with more information is saved as:
            # /usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log


        解決:內存不夠,購買阿里雲的機器可以動態增加內存

        ②問題二

            [root@iZwz95j86y235aroi85ht0Z bin]# ./elasticsearch
            [2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
            org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
            at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
            at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
            at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
            at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]

        解決:用非root用戶
            添加用戶:useradd -m 用戶名  然後設置密碼  passwd 用戶名           

        ③問題三

            ./elasticsearch
            Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options

           解決:權限不夠 chmod 777 -R 當前es目錄

           常見配置問題資料:https://www.jianshu.com/p/c5d6ec0f35e0


 ElasticSearch5.6.x簡單測試

        1、步驟 https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html
        2、使用POSTMAN 工具

        基礎
            查看集羣狀態:localhost:9200/_cat/health?v
            查看索引列表:localhost:9200/_cat/indices?v

SpringBoot2.x整合elasticSearch5.6.8實戰

        Spring Data Elasticsearch文檔地址
        https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/

        版本說明:SpringBoot整合elasticsearch
            https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix

        1、添加maven依賴                   

            <dependency>  
               <groupId>org.springframework.boot</groupId>  
               <artifactId>spring-boot-starter-data-elasticsearch</artifactId>  
           </dependency>  

        2、接口繼承ElasticSearchRepository,裏面有很多默認實現
            注意點:
                 索引名稱記得小寫,類屬性名稱也要小寫
             新建實體對象article
             加上類註解 @Document(indexName = "blog", type = "article")


         3、配置文件:

             # ELASTICSEARCH (ElasticsearchProperties)
            spring.data.elasticsearch.cluster-name=elasticsearch # Elasticsearch cluster name.
            spring.data.elasticsearch.cluster-nodes=localhost:9300 # Comma-separated list of cluster node addresses.
            spring.data.elasticsearch.repositories.enabled=true # Whether to enable Elasticsearch repositories.

        4、QueryBuilder使用
        https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/query-dsl-queries.html
        //單個匹配,搜索name爲jack的文檔  
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "搜");

        4、查看es數據

            查看索引信息:http://localhost:9200/_cat/indices?v
            查看某個索引庫結構:http://localhost:9200/blog
            查看某個對象:http://localhost:9200/blog/article/1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章