全文搜索引擎 Elasticsearch 入門

1. 百科

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

2. 安裝

依賴Java8,本文在Linux上運行
git

下載、解壓github

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

啓動web

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"
}  

 

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

修改config/elasticsearch.ymlbash

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不能啓動app

修改運維

在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 入門教程

相關文章
相關標籤/搜索