ElasticSearch基礎入門

ElasticSearch簡寫ES,ES是一個高擴展、開源的全文檢索和分析引擎,它能夠準實時地快速存儲、搜索、分析海量的數據。html

應用場景java

  • 咱們常見的商城商品的搜索
  • 日誌分析系統(ELK)
  • 基於大量數據(數千萬的數據)須要快速調查、分析而且並將結果可視化的業務需求

安裝並運行ES

Java環境安裝

Elastic 須要 Java 8 環境。若是你的機器還沒安裝 Java,能夠參考JAVA安裝node

ElasticSearch安裝

安裝完Java環境後,咱們能夠開始如下ElasticSearch安裝或者根據官方文檔安裝git

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

進入解壓目錄以後,運行下面命令,啓動ElasticSearchgithub

./bin/elasticsearchshell

若是此時報如下錯誤

錯誤一
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N

打開: elasticsearch-5.5.1/config/jvm.options數據庫

在末尾添加:json

-XX:-AssumeMP
錯誤二
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)

先執行:bootstrap

sysctl -w vm.max_map_count=262144

再打開elasticsearch-5.5.1/config/jvm.options數組

-Xmx512m
-Xms512m
錯誤三
[2019-06-27T15:01:43,165][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root

緣由:elasticsearch自5版本以後,處於安全考慮,不容許使用root用戶運行。

解決:建立一個普通用戶,將elasticsearch 安裝目錄權限修改一下,切換至普通用戶運行elasticsearch就能夠了

useradd elk
chown -R elk.elk /usr/local/share/applications/elasticsearch-5.5.1
su - elk
cd /usr/local/share/applications/elasticsearch-5.5.1

從新啓動

./bin/elasticsearch

若是一切正常,Elastic 就會在默認的9200端口運行。這時,打開另外一個命令行窗口,請求該端口,會獲得說明信息。

$ curl 'localhost:9200'
{
  "name" : "cWyaT72",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "A7akNm1SRw2Gm-BdSBkdaw",
  "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"
}

訪問配置

Elastic 默認狀況下,只容許本地訪問,若是須要遠程訪問,能夠修改 config/elasticsearch.yml文件,去掉network.host的註釋,將它的值改爲0.0.0.0,而後從新啓動 Elastic。

network.host: 0.0.0.0

上面代碼中,設成0.0.0.0讓任何人均可以訪問。線上服務不要這樣設置,要設成具體的 IP。

基本概念

Node 與 Cluster

Elastic本質上是一個分佈式數據庫,容許多臺服務器協同工做,每臺服務器能夠運行多個 Elastic 實例。

單個 Elastic 實例稱爲一個節點(node)。一組節點構成一個集羣(cluster)

查看Cluster Health

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

獲取集羣的全部節點

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

Index

Elastic會索引全部字段,通過處理後寫入一個反向索引(Inverted Index)。查找數據的時候,直接查找該索引。(一個 Index 相似於傳統關係數據庫中的一個 數據庫 ,是一個存儲關係型文檔的地方)。

因此,Elastic 數據管理的頂層單位就叫作 Index(索引)。它是單個數據庫的同義詞。每一個 Index (即數據庫)的名字必須是小寫。

下面的命令能夠查看當前節點的全部 Index。

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

Document

Index裏的單條記錄稱爲Document,多條Document構成一個Index.

Document使用JSON格式表示,如:

{
    "goods_name": "空調",
    "category_name": "家電分類",
    "price": "3999.00"
}

同一個 Index 裏面的 Document,不要求有相同的結構(scheme),可是最好保持相同,這樣有利於提升搜索效率。

Type

Document是能夠分組的,如goods_list這個Index ,能夠按照category(家電、衣服)分類,也能夠按照price(>1000、 <1000)分類。這種分組叫Type它是虛擬的邏輯分組,用於過濾Document

列出每一個Index下面的Type

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

根據規劃,Elastic 6.x 版只容許每一個 Index 包含一個 Type,7.x 版將會完全移除 Type。

Index操做

新建(Create Index)

新建 Index,能夠直接向 Elastic服務器發出 PUT 請求。下面的例子是新建一個名叫goods_listIndex

curl -X PUT 'http://localhost:9200/goods_list'

服務器返回一個 JSON 對象,裏面的acknowledged字段表示操做成功。

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

刪除(Delete Index)

curl -X DELETE 'http://localhost:9200/goods_list'
{
    "acknowledged": true
}

數據操做

上面介紹了IndexType的一些基本的概念和Index的基本操做,如今先來建立一個完整的Index結構,並對數據進行操做。

新建Index結構

curl -X PUT 'localhost:9200/goods_list' -d '
{
    "mappings": {
        "goods_info": {
            "properties": {
                "goods_name": {
                    "type": "keyword"
                },
                "category_name": {
                    "type": "keyword"
                },
                "price": {
                    "type": "float"
                }
            }
        }
    }
}
'

{
    "acknowledged": true
}

執行上面命名,從新建立一個新的Index

新增記錄

向指定的 /Index/Type 發送 PUT 請求,就能夠在 Index 裏面新增一條記錄。好比,向/goods_list/goods_info發送請求,就能夠新增一條商品記錄。

curl -X PUT 'localhost:9200/goods_list/goods_info/1' -d '
{
  "goods_name": "華爲筆記本",
  "category_name": "計算機",
  "price": "1000"
}'

服務器返回的 JSON 對象,會給出 Index、Type、Id、Version 等信息:

{
    "_index": "goods_list",
    "_type": "goods_info",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

相信細心的你會發現/goods_list/goods_info/1,後面多了一個1,這個1是該條記錄的 ID。能夠是任意字符串

新增記錄的時候,也能夠不指定 Id,這時要改爲 POST 請求。

curl -X POST 'localhost:9200/goods_list/goods_info' -d '
{
  "goods_name": "洗衣機",
  "category_name": "家電",
  "price": "899.99"
}'

若是沒有指定ID,那麼Elastic會隨機生成一串字符串做爲ID

{
    "_index": "goods_list",
    "_type": "goods_info",
    "_id": "AWub5f7FFq1D5epJJhqT",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

查看記錄

curl 'localhost:9200/goods_list/goods_info/1?pretty=true'

上面代碼請求查看/goods_list/goods_info/1這條記錄,URL 的參數pretty=true表示以易讀的格式返回。

返回的數據中,found字段表示查詢成功,_source字段返回原始記錄:

{
  "_index" : "goods_list",
  "_type" : "goods_info",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "goods_name" : "華爲筆記本",
    "category_name" : "計算機",
    "price" : "1000"
  }
}

若是 ID不正確,就查不到數據,found字段就是false

curl 'localhost:9200/goods_list/goods_info/2?pretty=true'

ID=2並不存在,因此會返回如下結果:

{
  "_index" : "goods_list",
  "_type" : "goods_info",
  "_id" : "2",
  "found" : false
}

刪除記錄

curl -X DELETE 'localhost:9200/goods_list/goods_info/1'

PS:這裏先不要刪除這條記錄,後面還要用到。

更新記錄

curl -X PUT 'localhost:9200/goods_list/goods_info/1' -d '
{
    "user" : "華爲筆記本",
    "title" : "計算機",
    "desc" : "5000"
}'

更新記錄就是使用 PUT 請求,從新發送一次數據。

{
    "_index": "goods_list",
    "_type": "goods_info",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": false
}

返回結果裏面,有幾個字段發生了變化:

"_version" : 2,
"result" : "updated",
"created" : false

數據查詢

返回全部記錄

curl 'localhost:9200/goods_list/goods_info/_search'
{
    "took": 127,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1,
        "hits": [
            {
                "_index": "goods_list",
                "_type": "goods_info",
                "_id": "AWub5f7FFq1D5epJJhqT",
                "_score": 1,
                "_source": {
                    "goods_name": "洗衣機",
                    "category_name": "家電",
                    "price": "899.99"
                }
            },
            {
                "_index": "goods_list",
                "_type": "goods_info",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "user": "華爲筆記本",
                    "title": "計算機",
                    "desc": "5000"
                }
            }
        ]
    }
}

上面代碼中,返回結果的 took字段表示該操做的耗時(單位爲毫秒),timed_out字段表示是否超時,hits字段表示命中的記錄,裏面子字段的含義以下:

  • total:返回記錄數,本例是2條。
  • max_score:最高的匹配程度,本例是1.0
  • hits:返回的記錄組成的數組。

返回的記錄中,每條記錄都有一個_score字段,表示匹配的程序,默認是按照這個字段降序排列。

總結

這裏主要介紹了Elastic的安裝、基本概念以及數據的基本操做,在下一章帶來Elastic的分詞和全文搜索以及相關的技術點。

參考連接

https://www.elastic.co/guide/...
http://www.ruanyifeng.com/blo...

原文地址

https://github.com/WilburXu/b...

相關文章
相關標籤/搜索