一、使用_cat API檢測集羣是否健康,確保9200端口號可用: curl 'localhost:9200/_cat/health?v' 注意:綠色表示一切正常,黃色表示全部的數據可用可是部分副本尚未分配,紅色表示部分數據由於某些緣由不可用。 二、獲取集羣的節點列表 curl 'localhost:9200/_cat/nodes?v' 三、查看全部索引 curl http://localhost:9200/_cat/indices?v 四、curl用法 -X 指定http的請求方法,有HEAD GET POST PUT DELETE -d 指定要傳輸的數據 -H 指定http請求頭信息 五、新增索引 如今咱們建立一個名爲"customer"的索引,而後再查看全部的索引: curl -XPUT 'localhost:9200/customer?pretty' curl 'localhost:9200/_cat/indices?v' 六、插入和獲取 6.一、插入數據 必須給ES指定索引的類型,如索引爲customer,類型爲external,ID爲1,主體爲JSON格式的語句:{ "name": "John Doe" } curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/1?pretty' -d ' { "name": "John Doe", "age": 10 }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/3?pretty' -d ' { "name": "AAA", "age": 20 }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/4?pretty' -d ' { "name": "BBB", "age": 30 }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/5?pretty' -d ' { "name": "EEE", "age": 40 }' 返回結果爲:create:true 表示插入成功。 6.二、獲取數據 curl -XGET 'localhost:9200/customer/external/1?pretty' 其中含義爲:獲取customer索引下類型爲external,id爲1的數據,pretty參數表示返回結果格式美觀。 七、刪除索引 curl -XDELETE 'localhost:9200/customer?pretty' curl 'localhost:9200/_cat/indices?v' 八、ES基本用法 經過以上命令語句的學習,咱們發現索引的增刪改查有一個相似的格式,總結以下: curl -X<REST Verb> <ip>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST風格的語法謂詞 <ip>:節點ip <port>:節點端口號,默認9200 <Index>:索引名 <Type>:索引類型 <ID>:操做對象的ID號 ES的動做是以http方法來決定的,經常使用的http方法: GET/PUT/POST/DELETE 九、修改數據 curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/2?pretty' -d ' { "name": "aaa" }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/customer/external/2?pretty' -d ' { "name": "Java" }' 上述命令語句是:先新增id爲1,name爲aaa的數據,而後將id爲2的name修改成Java。 10.更新數據 10.1 這個例子展現如何將id爲1文檔的name字段更新爲Jane Doe: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d ' { "doc": { "name": "Jane Doe AAA" } }' 10.2 這個例子展現如何將id爲1數據的name字段更新爲Jane Doe同時增長字段age爲20: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d ' { "doc": { "name": "Jane Doe", "age": 20 } }' 10.3 也能夠經過一些簡單的scripts來執行更新。一下語句經過使用script將年齡增長5: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/1/_update?pretty' -d ' { "script" : "ctx._source.age += 5" }' 十一、刪除數據 刪除數據那是至關的直接. 下面的語句將執行刪除Customer中ID爲2的數據: curl -XDELETE 'localhost:9200/customer/external/2?pretty' 十二、批處理 下面語句將在一個批量操做中執行建立索引: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_bulk?pretty' -d ' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } ' 查看數據 curl -XGET 'localhost:9200/customer/external/2?pretty' curl -XGET 'localhost:9200/customer/external/_search?pretty' 下面語句批處理執行更新id爲1的數據而後執行刪除id爲2的數據 curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_bulk?pretty' -d ' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} ' curl -XGET 'localhost:9200/customer/external/_search?pretty' 1三、查詢 curl 'localhost:9200/customer/external/_search?q=*&pretty' 上面示例返回全部customer中的索引數據。其中 q=* 表示匹配索引中全部的數據。 等價於: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} } }' 1四、查詢語言 匹配全部數據,但只返回1個: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} }, "size": 1 }' 注意:若是siez不指定,則默認返回10條數據。 curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 0, "size": 10 }' 返回從0到10的數據。(索引下標從0開始) curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} }, "sort": { "age": { "order": "desc" } } }' curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} }, "sort": { "age": { "order": "asc" } } }' 上述示例匹配全部的索引中的數據,按照age字段降序排序,而且返回前10條(若是不指定size,默認最多返回10條)。 1五、執行搜索 下面例子展現如何返回兩個字段(name age) curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_all": {} }, "_source": ["name", "age"] }' 返回age爲20 的數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match": { "age": 20 } } }' 返回name中包含Doe的全部數據:: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match": { "name": "Doe" } } }' 返回name中包含Doe或者AAA的全部數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match": { "name": "Doe AAA" } } }' 和上面匹配單個詞語不一樣,下面這個例子是多匹配(match_phrase短語匹配),返回name字段中包含短語 「mill lane」的全部數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "match_phrase": { "name": "mill lane" } } }' 如下是布爾查詢,布爾查詢容許咱們將多個簡單的查詢組合成一個更復雜的布爾邏輯查詢。 這個例子將兩個查詢組合,返回name中含有AAA和Doe的全部記錄數據,屬於and操做: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "name": "Doe" } }, { "match": { "name": "AAA" } } ] } } }' 上述例子中,must表示全部查詢必須都爲真才被認爲匹配。 相反, 這個例子組合兩個查詢,返回name中含有Doe或者Lynch的全部記錄數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "name": "Doe" } }, { "match": { "name": "Lynch" } } ] } } }' 上述例子中,bool表示查詢列表中只要有任何一個爲真則認爲匹配。 下面例子組合兩個查詢,返回地址中既沒有mill也沒有lane的全部數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "name": "Doe" } }, { "match": { "name": "AAA" } } ] } } }' 上述例子中,must_not表示查詢列表中沒有爲真的(也就是全爲假)時則認爲匹配。 咱們能夠組合must、should、must_not來實現更加複雜的多級邏輯查詢。 下面這個例子返回年齡等於10歲、name不爲"Jane Doe AAA"的全部數據: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "10" } } ], "must_not": [ { "match": { "name": "Jane Doe AAA" } } ] } } }' 1六、過濾filter(查詢條件設置) 下面這個例子使用了布爾查詢返回age在10到30之間的全部數據。 curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} } } } }' curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "age": { "gte": 10, "lte": 20 } } } } } }' 1七、聚合 Aggregations 下面這個例子: 將全部的數據按照age分組(group),而後按照分組記錄數從大到小排序,返回前十條(默認): curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_age": { "terms": { "field": "age" } } } }' 注意:咱們設置size=0,不顯示查詢hits,由於咱們只想看返回的聚合結果。 上述語句相似於如下SQL語句: select age, count(*) from customer group by age order by count(*) desc 下面這個實例按照age分組,降序排序,返回age的平均值: curl -XPOST -H "Content-Type: application/json" 'localhost:9200/customer/external/_search?pretty' -d ' { "size": 0, "aggs": { "group_by_age": { "terms": { "field": "age" }, "aggs": { "average_age": { "avg": { "field": "age" } } } } } }' curl -XGET 'localhost:9200/mytest?pretty' 刪除索引 curl -XDELETE 'localhost:9200/mytest?pretty' 新增一條記錄,並指定爲article類型,ID爲1 curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/1?pretty' -d ' { "title": "小D課堂啦啦啦", "content":"xdclass.net 小D課堂成立於2016年的,專一互聯網在線教育,課程範圍包括前端,後端,大數據,人工智能,微信開發等" }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/2?pretty' -d ' { "title": "test", "content":"testsfsdfdsfdsf", "PV":10 }' curl -XPUT -H "Content-Type: application/json" 'localhost:9200/mytest/article/3?pretty' -d ' { "title": "test", "content":"testsfsdfdsfdsf", "PV":23 }' 搜索 curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:D&pretty' curl -XGET 'http://localhost:9200/mytest/article/_search?q=title:test&pretty' 查看數據 curl http://localhost:9200/es-message-2019.04.23/_search?pretty