Elasticsearch 管理文件

2021-09-29 19:32:50 字數 1866 閱讀 7854

es支援近實時的索引、更新、查詢、刪除文件,近實時就意味著剛剛索引的資料需要1秒鐘後才能搜尋到,這也是與傳統的sql資料庫不同的地方。

之前已經試過如何索引乙個文件了,這裡再複習一下:

curl -xput '

localhost:9200/customer/external/1?pretty

' -d '

'

上面的例子中,建立了乙個索引為customer,型別為external,id為1的文件。

當再次執行命令:

curl -xput '

localhost:9200/customer/external/1?pretty

' -d '

'

之前的第乙個文件就被覆蓋掉了。

如果指定新的文件id,那麼舊的文件仍然存在:

curl -xput '

localhost:9200/customer/external/2?pretty

' -d '

'

索引的時候id是可選的,如果不指定id,es會隨機生成乙個id,並使用這個id索引文件資料。

curl -xpost '

localhost:9200/customer/external?pretty

' -d '

'

需要注意的是,如果不指定id,那麼需要使用post命令,而不是put。

除了索引和替換文件,es還支援更新文件。更新文件其實是先刪除舊的文件,再索引新的文件。

如果想要更新文件內容,可以按照下面的方式進行:

curl -xpost '

localhost:9200/customer/external/1/_update?pretty

' -d '

}'

由於是先刪除再索引,因此可以額外增加新的字段:

curl -xpost '

localhost:9200/customer/external/1/_update?pretty

' -d '

}'

當然也支援使用指令碼進行更新:

curl -xpos

t 'localhost:9200/customer/external/1/_update?pretty

' -d '

'

其中ctx._source代表了當前的文件,上面的意思 是 在當前文件的基礎上age加5.

刪除文件就很簡單了,只需要指定文件的索引、型別、id就行了:

curl -xdelete '

localhost:9200/customer/external/2?pretty

'

除了索引、替換、更新和刪除,es為了減少來回的響應資訊,可以一次性執行多個命令,最後統一返回執行結果。

例如:

curl -xpost '

localhost:9200/customer/external/_bulk?pretty

' -d '}}

'

上面的命令可以同時插入兩條資料。

_bulk命令不僅僅支援單個命令執行多條,還只是多種不同的命令執行多條。

curl -xpost '

localhost:9200/customer/external/_bulk?pretty

' -d '} }

}'

上面的命令中,先更新id為1的文件,再刪除id為2的文件。

如果bulk中的某乙個命令執行出錯,那麼會繼續執行後面的命令,最後在命令返回時,會返回每個命令的執行結果。

Elasticsearch 管理文件

es支援近實時的索引 更新 查詢 刪除文件,近實時就意味著剛剛索引的資料需要1秒鐘後才能搜尋到,這也是與傳統的sql資料庫不同的地方。更多的es文件資料參考 elasticsearch官方文件翻譯 之前已經試過如何索引乙個文件了,這裡再複習一下 curl xput localhost 9200 cu...

Elasticsearch 索引管理 一

本文翻譯自elasticsearch官方指南的索引管理 index management 一章 我們已經了解了es是如何在不需要任何複雜的計畫和安裝就能讓我們很容易地開始開發乙個新的應用的。但是,用不了多久你就會想要仔細調整索引和搜尋過程來更好的適配你的用例。幾乎所有的定製都和索引 index 以及...

Elasticsearch筆記 索引管理

一 建立索引 put http localhost 9200 是索引名,傳的json是這個索引的資料格式。1 索引名不能包含任何大寫字母 2 如果索引已存在,則丟擲索引已存在的異常 3 elasticsearch 預設給乙個索引設定5個分片1個分片,分派給定之後不再修改,副本可以隨時修改 結果 建立...