【ELK】7. elasticsearch linux上操做es命令詳解

==========html

1.檢查ES節點是否正常啓動java

curl http://192.168.6.16:9200

 

正常狀態:json

 

非正常狀態:瀏覽器

  1>確保服務是否是正常啓動了,端口用的是哪一個curl

  2>防火牆是否關閉或者端口是否開放elasticsearch

  3>你的curl命令是否有問題,curl命令可能致使服務沒法訪問,能夠嘗試重啓服務後,在外部瀏覽器訪問URL地址便可。不必定非得用curlide

 

 

2.cat檢測集羣健康情況測試

curl http://192.168.6.16:9200/_cat/health?v

綠色表示一切正常, 黃色表示全部的數據可用可是部分副本尚未分配,紅色表示不可用url

 

3.查詢es中全部索引,全部已存在的索引spa

curl http://192.168.6.16:9200/_cat/indices?v

 

 

4.建立新的索引【索引要求是全小寫字符,能夠有下劃線隔開】

curl -XPUT http://192.168.6.16:9200/my_new_index?pretty

 再查看:

curl http://192.168.6.16:9200/_cat/indices?v

 

 

 

5.對新增的索引,插入一條數據

type是user, id指定爲1

curl -XPUT http://192.168.6.16:9200/my_new_index/user/1?pretty -d  '{"name":"張三","age":"23"}'

 

 

6.根據ID,獲取剛剛索引中新增的數據

curl -XGET http://192.168.6.16:9200/my_new_index/user/1?pretty

 

 

7.修改數據

7.1先新增一條數據

curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四","age":"25"}'

 

7.2 根據ID查詢這條數據

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

7.3修改id爲2的數據

curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四修改","age":"28"}'

即便用相同的新增命令操做  相同的ID,數據不一樣

 

 

7.4查詢修改結果

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

 

 

8.更新數據,使用POST請求,注意請求體,格式

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230"}}'

 

查看更新後的數據:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

 

9.更新數據的同時,新增列

就是將doc中的json數據列增長便可

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230","address":"北京東直門"}}'

 

 

 查看:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

 

10.將age字段字符串類型,修改成數字類型,並使用簡單腳本對其操做

10.1 查看數據

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

10.2 將age類型由字符串更改成數值

就是將json中的age的值的引號去掉

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":230,"address":"北京東直門"}}'

 

 

10.3 查看修改後數據

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

10.4使用簡單腳本,對年齡增長5

若是報錯。解決方法:http://www.javashuo.com/article/p-ngkvzmwb-dk.html

curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"script" : "ctx._source.age += 5"}'

 

查看:

curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

 

11.刪除數據,根據ID刪除

curl -XDELETE http://192.168.6.16:9200/my_new_index/user/2?pretty

 

 

 

13.批量插入 bulk

【注意JSON字符串格式】

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"index":{"_id":"3"}}
{"name":"趙思","age":12}
{"index":{"_id":"4"}}
{"name":"錢三一","age":13}
'

 

 

想要看插入之後索引下的數據,查詢在後面16

 

 

14.批處理語句,bulk,更新id爲1的數據,刪除id爲3的數據 

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"update":{"_id":"1"}}
{"doc": {"name":"張三變李四","age":25}}
{"delete":{"_id":"3"}}
'

 

 

15.導入批量數據集文件json文件【使用bulk批量導入】

測試的json批量數據集文件,java生成代碼:

public static void main(String[] args) {
        File file   = new File("E:\\1\\myjson.json");
        FileWriter writer = null;
        int size = 200;

        try {
            writer =new FileWriter("E:\\1\\myjson.json");
            for (int i = 10; i < size+10; i++) {
                writer.write("{\"index\":{\"_id\":\""+i+"\"}}"+"\r\n"+"{\"name\":\"張三"+i+"\",\"age\": "+i+",\"address\":\"北京"+i+"\"}"+"\r\n");
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
View Code

 

若是報錯,解決方案:http://www.javashuo.com/article/p-gxdwxzkv-du.html

指定要導入的  索引、type、使用bulk命令  @符號後面跟json文件的絕對路徑

curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty --data-binary @/cjf/es/elasticsearch-2.3.3/data/myjson.json

 

 

查看index詳情:

 

curl http://192.168.6.16:9200/_cat/indices?v

能夠看到成功批量插入了200條

 

 

 

 

===================================下來看查詢(刪除索引在最後)========================================= 

16.查詢某個索引中的全部數據

curl http://192.168.6.16:9200/my_new_index/_search?q=*&pretty

等價於

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    }
}
'

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '{"query":{ "match_all":{}}}'

 

 

 

17.查詢指定索引下的數據

【若是不指定size,默認返回10條】

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "size":10
}
'

 

 

 

18.分頁查詢,從第10條,返回10條

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "from": 10,
    "size": 10
}
'

 

 

19.按照age字段倒序排序 sort,取出20條

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "sort":{
        "age":{
            "order":"desc"
        }
    },
    "from": 0,
    "size": 20
}
'

 

 

20.只返回想查詢的部分字段

只返回name和address列

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_all":{

        }
    },
    "_source":[
        "name",
        "address"
    ]
}
'

 

21.條件匹配查詢 

 21.1查詢age=200的數據

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "age":200
        }
    }
}
'

 

 

21.2 查詢address中包含 「北京」 的數據

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "address":"北京"
        }
    }
}
'

 

21.3 查詢 address中 包含「北京」 或 「西安」的全部數據 【匹配單個詞語  空格分隔】

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match":{
            "address":"北京 西安"
        }
    }
}
'

 

 

21.4 查詢address中包含「北京 西安」 完整詞語的【短語匹配,「北京 西安」做爲一個完整詞語查詢】、

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "match_phrase":{
            "address":"北京 西安"
        }
    }
}
'

 

 

22.布爾查詢 bool

22.1布爾查詢bool   and查詢,必須同時知足 address中包含「北京」,又要知足address中包含「西安」

 

must表示全部查詢必須都爲真才被認爲匹配

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'

 

 

22.2 布爾查詢bool  or查詢 address中包含「北京」 或者 address中包含「西安」 均可以

should 表示查詢列表中只要有任何一個爲真則認爲匹配

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'

 

 

 

22.3  布爾查詢bool   都不能知足的   既不能包含這個,也不能包含那個

must_not表示查詢列表中沒有爲真的(也就是全爲假)時則認爲匹配

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must_not":[
                {
                    "match":{
                        "address":"北京"
                    }
                },
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'

 

 

 

22.4 這樣,就能夠布爾查詢  多條件組合  查詢

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "age":200
                    }
                }
            ],
            "must_not":[
                {
                    "match":{
                        "address":"西安"
                    }
                }
            ]
        }
    }
}
'

 

 

23. 範圍查詢 range 查詢年齡25-30之間的

curl -XPOST  http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "query":{
        "range":{
            "age":{
                "gte":25,
                "lte":30
            }
        }
    }
}
'

 

 

 

 

 

 

24.聚合查詢 aggs

按照name進行聚合分組,而後按照記錄數,從大到小排序,默認返回前10條

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name"
            }
        }
    }
}
'

 

 25. 聚合查詢 aggs ,求age的平均值

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "average_age":{
            "avg":{
                "field":"age"
            }
        }
    }
}
'

 

 

 按name分組,求age的平均值

curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name"
            },
            "aggs":{
                "average_age":{
                    "avg":{
                        "field":"age"
                    }
                }
            }
        }
    }
}
'

 

 

 

 

 

100.刪除索引

 100.1 查看全部索引信息

curl http://192.168.6.16:9200/_cat/indices?v

 

100.2  刪除指定索引

curl -XDELETE http://192.168.6.16:9200/my_new_index?pretty

 

 

100.3 再次查看

curl http://192.168.6.16:9200/_cat/indices?v

 

 

==========================================

相關文章
相關標籤/搜索