ElasticSearch初試(一)
阿新 • • 發佈:2020-08-07
測試環境:debian 9
官網提供了 deb,rpm,原始碼下載
官方下載地址:https://www.elastic.co/downloads/elasticsearch
通過原始碼安裝會遇到一些小問題,為了方便,我直接下載deb安裝(需要提前安裝jdk)。
可以通過serviceelasticsearch start/stop啟動關閉服務,預設監聽了 9200埠,可以更改配置檔案
通過deb安裝的配置檔案在:/etc/elasticsearch/elasticsearch.yml
如果要在localhost外連線elasticsearch ,更改配置檔案中的 network.host:0.0.0.0
如果一起順利就可以開始測試了
檢視es基本資訊 curl localhost:9200 列出所有的Index curl -X GET 'http://localhost:9200/_cat/indices?v' 列舉每個Index下的Type curl 'localhost:9200/_mapping?pretty=true' 新增Index curl -X PUT 'localhost:9200/weather' 刪除Index curl -X DELETE 'localhost:9200/weather' 安裝中文分詞外掛ik (安裝完需要重啟es) elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip 建立一個Index,並設定其結構和分詞 curl -X PUT -H 'Content-Type: application/json' '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" } } } } }' 向Index增加記錄 PUT方式 curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d ' { "user": "張三", "title": "工程師" }' POST方式(POST方式不需要傳id,id隨機生成) curl -X POST -H 'Content-Type: application/json' 'localhost:9200/accounts/person' -d ' { "user": "李四", "title": "工程師" }' 注意:如果沒有先建立 Index(這個例子是accounts),直接執行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。所以,打字的時候要小心,不要寫錯 Index 的名稱。 檢視指定條目的記錄 curl 'localhost:9200/accounts/person/1?pretty=true' 刪除一條記錄 curl -X DELETE 'localhost:9200/accounts/person/1' 更新一條記錄 curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/accounts/person/1' -d ' { "user" : "張三", "title" : "軟體開發" }' 查詢所有記錄 curl 'localhost:9200/accounts/person/_search?pretty=true' 簡單查詢 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query" : { "match" : { "title" : "工程" }}, "from": 1, #0開始 "size": 1, #返回幾條資料 }' OR查詢 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query" : { "match" : { "title" : "工程 哈哈" }} }' AND查詢 curl -H 'Content-Type: application/json' 'localhost:9200/accounts/person/_search?pretty=true' -d ' { "query": { "bool": { "must": [ { "match": { "title": "工程" } }, { "match": { "title": "哈哈" } } ] } } }'