常用命令
查看版本信息
curl -XGET http://localhost:9200/?pretty
查看集羣狀態
curl -XGET http://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
1547914366 16:12:46 fl-elastic-cluster green 2 2 0 0 0 0 0 0 - 100.0%
查看節點信息
curl -XGET http://localhost:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.100.111 32 75 0 0.00 0.01 0.05 mdi * node1
192.168.100.112 28 71 0 0.00 0.01 0.05 mdi - node2
查看主節點
curl -XGET http://localhost:9200/_cat/master?v
查看分區狀態
curl -XGET http://localhost:9200/_cat/shards?v
查看磁盤使用
可以在瀏覽器上訪問下面這個網址
http://host:9200/_cat/allocation?v&hh=shards,disk.indices,disk.used,disk.avail
查看內存使用
可以在瀏覽器上訪問下面這個網址
http://host:9200/_cat/nodes?v&h=id,port,v,m,fdp,mc,mcs,sc,sm,qcm,fm,im,siwm,svmm
elastic默認mapping字段有1000個的限制,可以使用下面的命令進行修改(如修改爲5000)
curl -XPUT http://localhost:9200/my_index/_setting -d ‘{“index.mapping.total_fields.limit”: 5000}’
創建索引(索引必須是小寫)
例如創建一個my_index索引,默認爲會5個分片,一個副本
curl -XPUT localhost:9200/my_index?pretty
查看索引
curl -XGET http://localhost:9200/_cat/indices?v
查看索引設置
curl -XGET localhost:9200/my_index/_settings?pretty
查看索引狀態
curl -XGET http://localhost:9200/_cluster/health/my_index?pretty
刪除索引
curl -XDELETE http://localhost:9200/my_index
手動創建mapping
curl -XPUT http://localhost:9200/my_index/_mapping/my_type?pretty -d '{
"properties":{
"name":{
"type": "keyword"
},
"age":{
"type": "keyword"
}
}
}'
使用腳本做mapping初始化時,可以下面的方式
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/my_index/_mapping/my_type -d "$mapping_data"
當mapping數據量較多時,上面的方式curl會報參數過長,可以使用提交文件的方式
curl -H "Content-Type: application/json" -XPUT http://localhost:9200/my_index?pretty -d @mapping.json
查看mapping
curl -XGET http://localhost:9200/my_index/_mapping/my_type?pretty
寫入文檔
curl -H 'Content-Type:application/json' -XPUT 'localhost:9200/my_index/vip/1?pretty' -d '
{
"name":"zhangsan"
}'
插入成功會返回如下信息
{
"_index" : "customer",
"_type" : "vip",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查看操作
查詢所有文檔
kiabana上使用如下查詢
GET _search
{
"query": {
"match_all": {}
}
}
也可以使用curl進行查詢
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"match_all": {}
}
}'
使用term進行精確查詢
如查找my_index索引中name是zhangsan的文檔
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"constant_score": {
"filter": {
"term": {
"name" "zhangsan"
}
}
}
}
}'
使用terms查找多個值
如查找my_index索引中name是zhangsan或lisi的文檔
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"terms": {
"name" : ["zhangsan","lisi"]
}
}
}'
使用bool進行邏輯查詢
布爾過濾器
一個bool過濾器由三個部分組成
{
“bool”: {
“must”: [],
“should”: [],
“must_not”: [],
}
}
must: 所有條件都必須匹配,與AND等價
should: 至少有一個條件要匹配,與OR等價
must_not: 所有條件都不能匹配,與NOT等價
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"bool": {
"should": [
{"term": {"age": "18"}},
{"term": {"age": "20"}}
],
"must_not": [
"term" : {"name": "zhangsan"}
]
}
}
}'
使用range進行範圍查詢
range 查詢可同時提供包含(inclusive)和不包含(exclusive)這兩種範圍表達式,可供組合的選項如下:
gt: 大於
lt: 小於
gte: 大於或等於
lte: 小於或等於
如查詢my_index中age大於等於18,小於等於26的文檔
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"range": {
"age": {
"gte": 18,
"lte": 26
}
}
}
}'
match查詢
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"match": {
"name" : "張三"
}
}
}'
正則表達式查詢
curl -H "Content-Type: application/json" -XGET http://localhost:9200/my_index/_search?pretty -d '{
"query": {
"wildcard": {
"name" : "張三*"
}
}
}'