Elastic Search常用命令
ES的基本指令:
1. 檢視es的叢集狀態:
curl 'IP:9200/_cat/health?v'
註釋:?v表示格式化輸出
2. 檢視節點列表
curl 'IP:9200/_cat/nodes?v'
3.查詢所有索引及資料大小
curl 'IP:9200/_cat/indices?v'
4.建立索引(名稱為studentIndex)並指定分片數和備份數
curl -XPUT http://localhost:9200/studentIndex -d'
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
5.建立索引studentIndex,並新增型別student,並指定分詞是studentname
curl -XPUT 'http://localhost:9200/studentIndex' -d '
{
"mappings": {
"student": {
"properties": {
"studentname": {
"type": "string"
}
}
}
}
}'
ES和mysql的對應關係
MySql ElasticSearch
database -- index
table -- type
row -- document
cloumn -- field
schema -- mapping
index -- Everything is indexed
slect * from... --- get http://...
update talbe set... -- put http://...
6.查詢索引是studentIndex,type是student,studentname欄位的值是“李”的資訊,預設返回第一頁,10條資料
http://localhost:9200/studentIndex/student/_search?q=studentname:李
{
"took": 32,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 645161,
"max_score": 13.882782,
"hits": [{
"_index": "studentIndex",
"_type": "student",
"_id": "AWNIsqEX4aqkPyNgQr06",
"_score": 13.882782,
"_source": {
"studentname": "李李四"
}
},
{
"_index": "studentIndex",
"_type": "student",
"_id": "AWNIsqEX4aqkPyNgQr06",
"_score": 13.23425,
"_source": {
"studentname": "李五"
}
}
]
}
}
7.使用post查詢elastic search
curl -X POST \
http://1ip:9200/studentIndex/student/_search
-d '{
"query": {
"match": {
"studentname": "李"
}
},
"from": 100, // 第幾頁開始
"size": 10 // 每頁的大小
}'
返回結果同上:
註釋:
在url中的q=* 相等於body中的
{
"query": { "match_all": {} }
}
8.查詢資料總數:http://localhost:9200/studentIndex/student/_count
9.
a.插入資料:
curl -X POST 'localhost:9200/studentIndex/student?pretty' -d' {"studentname": "Jane Doe" }'
b.指定資料的Id是id1
curl -X POST 'localhost:9200/studentIndex/student/id1?pretty' -d' {"studentname": "John Doe" }'
10 刪除資料
a.刪除studentIndex中ID為2的資料
curl -X DELETE 'localhost:9200/studentIndex/student/2?pretty'
b.根據條件刪除資料
curl -X POST 'localhost:9200/studentIndex/student/_delete_by_query?pretty' -d '{
"query": {
"match": {
"studentname": "John"
}
}
}'
11.
修改id=1的name屬性,並直接增加屬性和屬性值
curl -X POST 'localhost:9200/studentIndex/student/1/_update?pretty' -d ' {
"doc": {
"studentname": "xyd"
}
}'