1.查詢所有的documents
http://192.168.43.45:9200/_search
boost parameter 細粒度搜索條件權重控制
如:組裝多個查詢條件,其中一個匹配的想要優先查詢顯示出來,需要使用權重控制提升相似度排名
2.查看 elasticsearch的 健康狀態
http://192.168.43.45:9200/_cat/health?v
紅:數據不可訪問
綠:集群完全起作用
黃:一些數據不可訪問
3. 查看集群中節點情況:
http://192.168.43.45:9200/_cat/nodes?v
4.查看集群下所有索引
http://192.168.43.45:9200/_cat/indices?v
5.創建一個索引
從put請求可以看出es是rest 的api ,默認put就是增加
put http://192.168.43.45:9200/testcreate?pretty
索引的名稱必須全部是小寫
否則報錯
修改索引為小寫,必須使用put請求
然后查看所有索引:
http://192.168.43.45:9200/_cat/indices?v
從圖中可以看出有5個主分區和1個副本(5 primary shards and 1 replica)
目前看到的節點的顏色是黃色,是因為單節點的es和es默認的shard 10個分區不匹配,現在只有5個分區,只有當節點增加了es節點,才會在另一個節點進行副本復制,那么顏色才會變成綠色
6.向索引中添加文檔
put http://192.168.43.45:9200/testcreate/doc/1?pretty
{
"name": "John Doe"
}
查詢一下剛才放入的數據:
http://192.168.43.45:9200/testcreate/doc/1?pretty
7.刪除索引
delete http://192.168.43.45:9200/testcreate?pretty
再次查看所有索引:
http://192.168.43.45:9200/_cat/indices?v
8.更新文檔
如果在刪除了索引之后,又進行了向索引中添加doc,那么默認會把索引創建
如果繼續在添加的基礎上面,修改添加的數據內容,則會對當前文檔進行修改
put http://192.168.43.45:9200/testcreate/doc/2?pretty
{
"name": "John CCC"
}
查詢id=2的文檔
如果不指定id向分區中插入數據,es會隨機生成id
post http://192.168.43.45:9200/testcreate/doc?pretty
{
"name": "John DDD"
}
查詢該測試索引下的所有文檔:
http://192.168.43.45:9200/testcreate/_search
es 更新文檔,本質就是刪除舊的文檔,然后放入新的文檔
下面是插入,更新看下下一個:因為少了id
post http://192.168.43.45:9200/testcreate/doc/_update?pretty
{
"doc": {"name" : "test update doc"}
}
更新:
post http://192.168.43.45:9200/testcreate/doc/1/_update?pretty
{
"doc": {"name" : "test update doc"}
}
查看索引下更新的文檔:
更新的時候多增加1個字段
post http://192.168.43.45:9200/testcreate/doc/1/_update?pretty
{
"doc": {"name" : "test update doc", "age": 20}
}
使用腳本對字段值進行更新
post http://192.168.43.45:9200/testcreate/doc/1/_update?pretty
{
"script": "ctx._source.age += 100"
}
即當前上下文context,當前上下文中doc的source字段的屬性
查看結果:
http://192.168.43.45:9200/testcreate/_search
9.批量處理
①、批量插入多個doc,目前只能在命令行中寫腳本執行
curl -H "Content-Type: application/x-ndjson" -XPOST '192.168.43.45:9200/testcreate/doc/_bulk?pretty' --data-binary @request
request腳本:
{"index":{"_id":"3"}}
{"name":"test batch insert 3"}
{"index":{"_id":"4"}}
{"name":"test batch insert 4"}
②、批量進行更新、刪除、
curl -H "Content-Type: application/x-ndjson" -XPOST '192.168.43.45:9200/testcreate/doc/_bulk?pretty' --data-binary @updateanddelete.txt
多個操作,每一個操作不影響別的操作,當一個動作執行失敗,其他操作仍然繼續執行,批處理的返回結果,可以看到所有的處理結果
10.導入數據集
curl -H "Content-Type: application/json" -XPOST '192.168.43.45:9200/testcreate/doc/_bulk?pretty&refresh' --data-binary @updateanddelete.txt
curl "192.168.43.45:9200/_cat/indices?v"
updateanddelete.txt:
{"update":{"_id":"2"}}
{"doc":{"name":"test update and test delete"}}
{"delete":{"_id":"3"}}
批處理數據,最優數目可以是1000 - 5000,大小為5M-15M
查看刪除后剩余的doc:
http://192.168.43.45:9200/testcreate/_search
查看某一個具體的doc:
get http://192.168.43.45:9200/testcreate/doc/2
查看索引中文檔個數:
http://172.22.64.45:9200/_cat/indices?v
11.查詢 es 中所有的 文檔, 並按照某一個條件排序
get http://172.22.64.45:9200/testcreate/_search?q=*&sort=_id:asc&pretty
查詢索引庫 testcreate 中所有數據,並按照_id 升序排序
參考:https://segmentfault.com/a/1190000017136282?utm_source=tag-newest
get http://172.22.64.45:9200/testcreate/_search?q=*&sort=_id:asc&pretty
解釋:所有結果(q=*)按照_id升序排序
下面的查詢與上面的含義一致:
GET /testcreate/_search { "query": { "match_all": {} }, "sort": [ { "_id": "asc" } ] }
按條件查詢 es 中的 文檔, 並按照某一個條件排序:
(如果es官方文檔中url是get請求,但是postman又模擬不出來,可以使用post來,參考文章:es 的 http請求中攜帶參數問題 解釋)
http://172.22.64.45:9200/testcreate/_search
{
"query": { "match": { "name": "test" } },
"sort": {"_id" : "desc"}
}
解釋:查詢索引testcreate中name 中包含 test ,且按照 _id排序
①、其中match是匹配test或者別的name , 如果match 寫成 “match”:{"name":"test hello"},則表示匹配name是 test 或者 hello 的數據
②、其中match_phrase是精確匹配,即只匹配
http://172.22.64.45:9200/testcreate/_search
{
"query": { "match_phrase": { "name": "test update" } },
"sort": {"_id" : "desc"}
}
組合查詢:通過使用pool
解釋:查詢索引testcreate中文檔,匹配name中帶有test,且不匹配 id是9FSQTW0Bk8G6zS0U的文檔
http://172.22.64.45:9200/testcreate/_search
{
"query": {
"bool":{
"must":[{
"match":{"name":"test"}
}],
"must_not":[{
"match":{"_id":"9FSQTW0Bk8G6zS0U"}
}]
}
}
}
按照分頁進行獲取數據:
解釋:查詢索引testcreate中文檔,匹配name中帶有test,且不匹配 id是9FSQTW0Bk8G6zS0U的文檔,按照2條數據分頁,獲取第二頁的數據
http://172.22.64.45:9200/testcreate/_search
list數據默認是從0開始
比如匹配特殊的屬性字段:
http://172.22.64.45:9200/testcreate/_search
解釋:匹配屬性列 name 是 test 的文檔
{
"query":{
"match": { "name": "test" }
}
}
當指定返回的文檔是空的,只需要得到總數,則使用size=0
http://172.22.64.45:9200/testcreate/_search
{
"query":{
"match": { "name": "test" }
},
"size":0
}
12.聚合查詢
對某一個field屬性列進行統計
http://172.22.64.45:9200/testcreate/_search
{
"size": 0,
"aggs": {
"group_by_name": {
"terms": {"field": "name.keyword"}
}
}
}
解釋:對索引testcreate中的字段name進行分組操作,並統計name關鍵字的個數,使默認的全部數據不顯示(size=0),只返回聚合的結果
{
"took": 1045,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{"key": "John DDD","doc_count": 1},
{"key": "test batch insert 4","doc_count": 1},
{"key": "test update and test delete","doc_count": 1},
{"key": "test update doc","doc_count": 1}]}
}
}
目前看到的統計的都是1條數據,沒有相同的數據,現在添加文檔
http://172.22.64.45:9200/testcreate/doc/5?pretty
{
"name": "test batch insert 4"
}
再進行查詢:name為test batch insert 4 有兩條數據
首先去掉id不是 數字的 文檔
http://172.22.64.45:9200/testcreate/_search
{
"query" : {
"bool": {
"must_not": [{
"match":{"_id":"9FSQTW0Bk8G6zS0U"}
}]
}
},
"aggs": {
"group_by_name": {"terms": {"field": "name.keyword"}}
}
}
再插入age數據
http://172.22.64.45:9200/testcreate/doc/6?pretty
{
"name": "test update doc",
"age": 150
}
復合聚合,嵌套聚合:
http://172.22.64.45:9200/testcreate/_search
{
"query" : {
"bool": {
"must_not": [
{ "match":{"_id":"9FSQTW0Bk8G6zS0U"}},
{ "match":{"_id":"4"}},
{ "match":{"_id":"5"}},
{ "match":{"_id":"9FSQTW0Bk8G6zS0U-t9f"}}
]}
},
"aggs": {
"group_by_name": {
"terms": {"field": "name.keyword"},
"aggs": {
"average_age": {
"avg": {"field": "age"}
}
}
}
}
}
解釋:過濾掉_id中的文檔沒有包含age屬性的字段,如果不過濾掉,則會出現統計是null的數據
按照文檔中屬性的name進行分組,然后在分組的結果上面進行求平均值
聚合進行求和:
{
"query" : {
"bool": {
"must_not": [
{ "match":{"_id":"9FSQTW0Bk8G6zS0U"}},
{ "match":{"_id":"4"}},
{ "match":{"_id":"5"}},
{ "match":{"_id":"9FSQTW0Bk8G6zS0U-t9f"}}
]
}
},
"aggs": {
"group_by_name": {
"terms": {"field": "name.keyword"},
"aggs": {
"sum_age": {"sum": {"field": "age"}}
}
}
}
}
按照年齡大小排序:
{
"query" : {
"bool": {
"must_not": [
{ "match":{"_id":"9FSQTW0Bk8G6zS0U"}},
{ "match":{"_id":"4"}},
{ "match":{"_id":"5"}},
{ "match":{"_id":"9FSQTW0Bk8G6zS0U-t9f"}}]
}
},
"aggs": {
"group_by_name": {
"terms": {
"field": "name.keyword",
"order":{"sum_age": "asc"}
},
"aggs": {
"sum_age": {"sum": {"field": "age"}}
}
}
}
}
13. 安裝時候自動創建索引
action.auto_create_index in elasticsearch.yml 配置為:
action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*
殺死進程:pkill -F pid
es本身是用Java編譯的