ElasticSearch 連載一 基礎入門


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

應用場景

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

安裝並運行ES

Java環境安裝

Elastic 需要 Java 8 環境。如果你的機器還沒安裝 Java,可以參考JAVA安裝

ElasticSearch安裝

安裝完Java環境后,我們可以開始以下ElasticSearch安裝或者根據官方文檔安裝

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

進入解壓目錄之后,運行下面命令,啟動ElasticSearch

./bin/elasticsearch

如果此時報以下錯誤

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

打開: elasticsearch-5.5.1/config/jvm.options

在末尾添加:

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

先執行:

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_list的 Index

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://github.com/WilburXu/b...


免責聲明!

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



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