Elasticsearch學習筆記:整理


下載Elasticsearch

mkdir /home/es

cd /home/es

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.zip

解壓:

unzip elasticsearch-6.0.0.zip

因為是開箱即用,所以不需要安裝
切換到bin目錄下

cd /home/es/elasticsearch6.0.0/bin

執行

./elasticsearch -d (-d是后台運行,如果不輸入,當前Console會被鎖死,不能進行其他操作,執行Ctrl + c,可關閉Elasticsearch;如果執行-d,則無法看見執行的詳細信息)

即可運行

如果需要覆蓋集群或者節點的名字,則在啟動時,添加參數:
./elasticsearch --cluster.name my_cluster_name --node.name my_node_name -d

要檢查集群健康,我們將使用_cat API。需要事先記住的是,我們的節點HTTP的端口是9200:

curl 'localhost:9200/_cat/health?v'

如下:
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1512571194 22:39:54 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
[root@localhost ~]#
============================================================

綠色:一切正常(集群功能齊全)
黃色:所有的數據都是可用的,但是某些復制沒有被分配(集群功能是完備的)
紅色:因為某些原因,某些數據不可用。
注意,即使是集群狀態是紅色的,集群仍然是部分可用的(它仍然會利用可用的分片來響應搜索請求),但是可能你需要盡快修復它,因為你有丟失的數據

獲得節集群中的節點列表:

curl 'localhost:9200/_cat/nodes?v'
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 11 96 1 0.00 0.04 0.05 mdi * qnnd6ME
============================================================


顯示索引

curl 'localhost:9200/_cat/indices?v'
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer n8UenwhWTr-8Y84WPsU92g 5 1 1 0 4.7kb 4.7kb
============================================================
創建索引
curl -XPUT 'localhost:9200/[索引名]?[返回json格式:pretty 美化的格式]'
例如curl -XPUT 'localhost:9200/myindex?pretty'
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "myindex"
}
============================================================

插入數據
curl -XPUT 'localhost:9200/[索引名]/[類型名]/[指定id]' -d '{"key":"value","key":"value",...}'
例如:
curl -XPUT 'localhost:9200/myindex/external/1' -d '{"firstName":"John","LastName":"Smith"}'
注意:-d之前,必需要空格!!!!
但是執行之后,出現錯誤:
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex/external/1' -d '{"firstName":"John","LastName":"Smith"}'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}[root@localhost ~]#
============================================================

格式化輸入依然報錯

============================================================
bash-4.2$ curl -XPUT 'localhost:9200/myindex/external/1?pretty' -d '
{
"firstName":"John"
"LastName":"Smith"
}'
{
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
"status" : 406
}
============================================================

這個問題,是在報文Content-type的參數:application/x-www-form-urlencoded不支持Json發送。需要改成application/Json
所以需要添加參數 ; -H 'Content-Type: application/json'

:::::Content-Type現有類型不支持JSON等格式傳遞
curl -XPUT 'localhost:9200/myindex/external/1' -H 'Content-Type: application/json' -d '{"firstName":"John","LastName":"Smith"}'

curl -XPUT 'localhost:9200/myindex/external/1?pretty' -H 'Content-Type: application/json' -d '
{
"firstName":"John",
"LastName":"Smith"
}'
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex/external/1?pretty' -H 'Content-Type: application/json' -d '
> {
> "firstName":"John",
> "LastName":"Smith"
> }'
{
"_index" : "myindex",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}

============================================================
取出索引:
curl -XGET 'localhost:9200/myindex/external/1?pretty'
============================================================
[root@localhost ~]# curl -XGET 'localhost:9200/myindex/external/1?pretty'
{
"_index" : "myindex",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"firstName" : "John",
"LastName" : "Smith"
}
}
============================================================
_source部分,就是我們添加的數據
_id是之前添加的索引id

刪除索引 curl -XDELETE 'localhost:9200/myindex?pretty'
============================================================
[root@localhost ~]# curl -XDELETE 'localhost:9200/myindex?pretty'
{
"acknowledged" : true
}
============================================================


免責聲明!

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



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