全文搜索引擎 Elasticsearch 入門


1. 百科

ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放源碼發布,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。

2. 安裝

依賴Java8,本文在Linux上運行

下載、解壓

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5.1.zip
$ cd elasticsearch-5.5.1/ 

啟動

curl localhost:9200

返回

$curl localhost:9200
{
  "name" : "Jt1bemT",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "I_R2FCz8SUypT80rkbLeKw",
  "version" : {
    "number" : "5.5.1",
    "build_hash" : "19c13d0",
    "build_date" : "2017-07-18T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}  

 

默認是只能本地調用,設置遠程訪問:

修改config/elasticsearch.yml

network.host: 0.0.0.0

重啟,還是訪問不了,報錯

ERROR: [1] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk  

原因:這是在因為Centos6不支持SecComp,而ES5.2.0默認bootstrap.system_call_filter為true進行檢測,所以導致檢測失敗,失敗后直接導致ES不能啟動

修改

在elasticsearch.yml中配置bootstrap.system_call_filter為false

bootstrap.memory_lock: false
bootstrap.system_call_filter: false

啟動,就可以通過:ip:port訪問了

3. 基本概念

Node與Cluster

Elastic本質是一個分布式數據庫,允許多台服務器協同工作,每台機器可以運行多個Elastic實例。

單個Elastic實例稱為一個節點(Node),一組節點構成一個集群(Cluster)

Index

Elastic會索引所有的字段,經處理后寫入一個反向索引。查找數據時,直接查找該索引。

所以Elastic數據管理的頂層單位交Index,它是單個數據庫的同義詞(注:每個索引必須是小寫)

查看當前節點所有的Index

curl -X GET 'http://localhost:9200/_cat/indices?v'

Document

Index里面的記錄稱為Document,許多Document構成一個Index

Document采用的設計Json格式

同一個Index里的Document不要求有相同的結構,但是最好一樣,這樣利於提高搜索效率。

Type

Document分組,比如地體有1號線、2號線。不同的Type里應有相同的結構

可以通過以下命令查看一個Index中所有的Type

curl 'localhost:9200/_mapping?pretty=true'

存儲結構和關系數據庫對比

關系數據庫       ⇒ 數據庫        ⇒ 表          ⇒ 行             ⇒ 列(Columns)

Elasticsearch  ⇒ 索引(Index)   ⇒ 類型(type)  ⇒ 文檔(Docments)  ⇒ 字段(Fields)     

4. 增加和刪除Index

curl -X PUT 'localhost:9200/subway'

新建了一個叫subway的Index,返回

{"acknowledged":true,"shards_acknowledged":true}  

刪除叫subway的Index

curl -X DELETE 'localhost:9200/subway'

5. 中文分詞設置

安裝中文分詞插件,如ik

$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip

重啟Elastic,就可以加載安裝的ik了。

下面新建一個Index,指定需要分詞的字段。

$ curl -X PUT 'localhost:9200/accounts' -d '
{
  "mappings": {
    "person": {
      "properties": {
        "user": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "desc": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        }
      }
    }
  }
}'

上述代碼新建了名叫accounts的Index,名叫person的Index,person里有3個字段

  • user
  • title
  • desc

這3個都是中文,並且都是文本(text),所以需要指定中文分詞,而不使用英文分詞器

search_analyzer指定:ik_max_word,指明實用ik對文本進行最大數量的分詞

6. 增刪改

新增3條記錄

curl -X PUT 'http://10.125.15.70:9200/accounts/person/zs' -d '{"user":"張三","title":"數據庫工程師","desc":"數據庫管理"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ls' -d '{"user":"李四","title":"運維工程師","desc":"系統維護"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ww' -d '{"user":"王五","title":"開發工程師","desc":"軟件開發"}'

curl -X DELETE 'http://10.125.15.70:9200/accounts/person/zs'

curl -X PUT 'localhost:9200/accounts/person/ls' -d '{"user" : "李四2","title" : "工程師","desc" : "軟件開發"}'

7. 查

返回所有記錄

curl 'localhost:9200/accounts/person/_search'

結果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "accounts",
        "_type" : "person",
        "_id" : "ls",
        "_score" : 1.0,
        "_source" : {
          "user" : "李四2",
          "title" : "工程師",
          "desc" : "軟件開發"
        }
      },
      {
        "_index" : "accounts",
        "_type" : "person",
        "_id" : "ww",
        "_score" : 1.0,
        "_source" : {
          "user" : "王五",
          "title" : "開發工程師",
          "desc" : "軟件開發"
        }
      }
    ]
  }
}

全文搜索

curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"title":"工程"}}}

結果

{
    "took": 21,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 0.5649868,
        "hits": [{
            "_index": "accounts",
            "_type": "person",
            "_id": "ww",
            "_score": 0.5649868,
            "_source": {
                "user": "王五",
                "title": "開發工程師",
                "desc": "軟件開發"
            }
        }, {
            "_index": "accounts",
            "_type": "person",
            "_id": "ls",
            "_score": 0.5063205,
            "_source": {
                "user": "李四2",
                "title": "工程師",
                "desc": "軟件開發"
            }
        }]
    }
}

邏輯運算

OR

curl 'localhost:9200/accounts/person/_search'  -d '{"query" : { "match":{ "desc":"系統 開發" }}}'

AND

curl 'localhost:9200/accounts/person/_search'  -d '"query": {"bool": {"must": [{ "match": { "desc": "軟件" } }, { "match": { "desc": "開發" } }]}}}'

 

參考

Elasticsearch5.2.0部署過程的坑

全文搜索引擎 Elasticsearch 入門教程

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM