安裝 6.0.0版本
## pip install elasticsearch
==
6.0
.0
# 導包
import
json
from
elasticsearch
import
Elasticsearch
創建Index – PUT /news?pretty
es
=
Elasticsearch
(
hosts
=
"ip:port"
)
# 創建一個名為news的索引
result
=
es
.
indices
.
create
(
index
=
'news'
,
ignore
=
400
)
print
(
result
)
# 創建成功
#
{
'acknowledged'
:
True
,
'shards_acknowledged'
:
True
,
'index'
:
'news'
}
# 如果再把代碼執行一次的話,就會返回
:
status為
400
,
原因是Index已經存在
#
{
'error'
:
{
'root_cause'
:
[
{
'type'
:
'resource_already_exists_exception'
,
'reason'
:
'index [news/RfnIRUh9Qxi-5aYlNEYilg] already exists'
,
'index_uuid'
:
'RfnIRUh9Qxi-5aYlNEYilg'
,
'index'
:
'news'
}
]
,
'type'
:
'resource_already_exists_exception'
,
'reason'
:
'index [news/RfnIRUh9Qxi-5aYlNEYilg] already exists'
,
'index_uuid'
:
'RfnIRUh9Qxi-5aYlNEYilg'
,
'index'
:
'news'
}
,
'status'
:
400
}
刪除Index – DELETE /news?pretty
result
=
es
.
indices
.
delete
(
index
=
'news'
,
ignore
=
[
400
,
404
]
)
print
(
result
)
# # 刪除成功
# #
{
'acknowledged'
:
True
}
# # 如果再把代碼執行一次的話,就會返回
:
status為
404
,
原因是Index已經被刪除
# #
{
'error'
:
{
'root_cause'
:
[
{
'type'
:
'index_not_found_exception'
,
'reason'
:
'no such index'
,
'resource.type'
:
'index_or_alias'
,
'resource.id'
:
'news'
,
'index_uuid'
:
'_na_'
,
'index'
:
'news'
}
]
,
'type'
:
'index_not_found_exception'
,
'reason'
:
'no such index'
,
'resource.type'
:
'index_or_alias'
,
'resource.id'
:
'news'
,
'index_uuid'
:
'_na_'
,
'index'
:
'news'
}
,
'status'
:
404
}
刪除Index下的type
POST
news
/
politics
/
_delete_by_query
?
conflicts
=
proceed
{
"query"
:
{
"match_all"
:
{
}
}
}
body
=
{
"query"
:
{
"match_all"
:
{
}
}
}
conflicts
=
"proceed"
result
=
es
.
delete_by_query
(
index
=
"news"
,
body
=
body
,
doc_type
=
"politics"
,
conflicts
=
conflicts
)
快速檢查集群的健康狀況 GET /_cat/health?v
# result
=
es
.
cluster
.
health
(
wait_for_status
=
'yellow'
,
request_timeout
=
1
)
result
=
es
.
cluster
.
health
(
)
# 或者
result
=
es
.
cat
.
health
(
)
""
"
了解集群的健康狀況?green、yellow、red?
green:每個索引的primary shard和replica shard都是active狀態的
yellow:每個索引的primary shard都是active狀態的,但是部分replica shard不是active狀態,處于不可用的狀態
red:不是所有索引的primary shard都是active狀態的,部分索引有數據丟失了
為什么現在會處于一個yellow狀態?
本人一臺服務器,就啟動了一個es進程,相當于就只有一個node。現在es中有一個index,就是kibana自己內置建立的index。由于默認的配置是給每個index分配
5
個primary shard和
5
個replica shard,而且primary shard和replica shard不能在同一臺機器上(為了容錯)。現在kibana自己建立的index是
1
個primary shard和
1
個replica shard。當前就一個node,所以只有
1
個primary shard被分配了和啟動了,但是一個replica shard沒有第二臺機器去啟動。
""
"
插入數據 – PUT /index/type/id
PUT
/
news
/
politics
/
1
{
'title'
:
'美國留給伊拉克的是個爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
}
# 在插入數據的時候可以直接插入結構化字典數據
data
=
{
'title'
:
'美國留給伊拉克的是個爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
}
# # index 參數代表了索引名稱,doc_type 代表了文檔類型,body 則代表了文檔具體內容,id 則是數據的唯一標識
ID
result
=
es
.
create
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
,
body
=
data
)
print
(
result
)
# result 字段為 created,代表該數據插入成功
#
{
'_index'
:
'news'
,
'_type'
:
'politics'
,
'_id'
:
'1'
,
'_version'
:
1
,
'result'
:
'created'
,
'_shards'
:
{
'total'
:
2
,
'successful'
:
1
,
'failed'
:
0
}
,
'_seq_no'
:
0
,
'_primary_term'
:
1
}
# 也可以使用
index
(
)
方法來插入數據,但與
create
(
)
不同的是,create
(
)
方法需要我們指定 id 字段來唯一標識該條數據,而
index
(
)
方法則不需要,如果不指定 id,會自動生成一個 id
#
create
(
)
方法內部其實也是調用了
index
(
)
方法,是對
index
(
)
方法的封裝
# es
.
index
(
index
=
'news'
,
doc_type
=
'politics'
,
body
=
data
)
查找 – GET /index/type/id
# 查詢index
=
news
,
type
=
politics
GET
/
news
/
politics
/
_search
# 查詢所有index
=
news
,
type
=
politics
,
id
=
1
GET
/
news
/
politics
/
1
result
=
es
.
get
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
)
print
(
result
)
更新數據 – POST /news/politics/1/_update
#修改:更新文檔
POST
/
news
/
politics
/
1
/
_update
{
"doc"
:
{
'date'
:
'2011-12-16'
}
}
data
=
{
"doc"
:
{
'date'
:
'2011-12-16'
}
}
# Validation Failed
:
1
:
script or doc is missing
;
# 數據增加了一個日期字段,然后調用了
update
(
)
方法
result
=
es
.
update
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
,
body
=
data
)
print
(
result
)
# ## result 字段為 updated,即表示更新成功,另外我們還注意到有一個字段 _version,這代表更新后的版本號數,
2
代表這是第二個版本,因為之前已經插入過一次數據,所以第一次插入的數據是版本
1
,可以參見上例的運行結果,這次更新之后版本號就變成了
2
,以后每更新一次,版本號都會加
1
# #
{
'_index'
:
'news'
,
'_type'
:
'politics'
,
'_id'
:
'1'
,
'_version'
:
2
,
'result'
:
'updated'
,
'_shards'
:
{
'total'
:
2
,
'successful'
:
1
,
'failed'
:
0
}
,
'_seq_no'
:
1
,
'_primary_term'
:
2
}
#修改:替換文檔
PUT
/
news
/
politics
/
1
data
=
{
{
'title'
:
'美國留給伊拉克的是個爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
,
'date'
:
'2011-12-16'
}
##
index
(
)
方法可以代替我們完成兩個操作,如果數據不存在,那就執行插入操作,如果已經存在,那就執行更新操作,非常方便
0
#es
.
index
(
index
=
'news'
,
doc_type
=
'politics'
,
body
=
data
,
id
=
1
)
刪除相應的數據 – DELETE /news/politics/1
result
=
es
.
delete
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
)
print
(
result
)
## result 字段為 deleted,代表刪除成功,_version 變成了
3
,又增加了
1
#
{
'_index'
:
'news'
,
'_type'
:
'politics'
,
'_id'
:
'1'
,
'_version'
:
3
,
'result'
:
'deleted'
,
'_shards'
:
{
'total'
:
2
,
'successful'
:
1
,
'failed'
:
0
}
,
'_seq_no'
:
2
,
'_primary_term'
:
2
}
# 刪除滿足該條件的數據
query
=
{
'query'
:
{
'match'
:
{
"_id"
:
"BxCklGwBt0482SoSeXuE"
}
}
}
result
=
es
.
delete_by_query
(
index
=
'news'
,
body
=
query
,
doc_type
=
'politics'
)
print
(
result
)
對于中文來說,需要安裝一個分詞插件,這里使用的是 elasticsearch-analysis-ik,GitHub 鏈接為:https://github.com/medcl/elasticsearch-analysis-ik,這里我們使用 Elasticsearch 的另一個命令行工具 elasticsearch-plugin 來安裝,這里安裝的版本是 6.0.0
在安裝這個分詞插件之后,需要重啟elasticsearch
進入到安裝elasticsearch的目錄 cd /opt/elasticsearch-6.0.0/bin
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.0/elasticsearch-analysis-ik-6.0.0.zip
# 新建一個索引并指定需要分詞的字段
mapping = {
'properties': {
'title': {
'type': 'text',
'analyzer': 'ik_max_word',
'search_analyzer': 'ik_max_word'
}
}
}
# 先刪除之前的索引
## mapping 信息中指定了分詞的字段,指定了字段的類型 type 為 text,分詞器 analyzer 和 搜索分詞器 search_analyzer 為 ik_max_word,即使用我們剛才安裝的中文分詞插件。如果不指定的話則使用默認的英文分詞器。
es.indices.delete(index='news', ignore=[400, 404])
es.indices.create(index='news', ignore=400)
result = es.indices.put_mapping(index='news', doc_type='politics', body=mapping)
插入數據操作
datas
=
[
{
"name"
:
"gaolujie yagao"
,
"desc"
:
"gaoxiao meibai"
,
"price"
:
30
,
"producer"
:
"gaolujie producer"
,
"tags"
:
[
"meibai"
,
"fangzhu"
]
}
,
{
"name"
:
"jiajieshi yagao"
,
"desc"
:
"youxiao fangzhu"
,
"price"
:
25
,
"producer"
:
"jiajieshi producer"
,
"tags"
:
[
"fangzhu"
]
}
,
{
"name"
:
"zhonghua yagao"
,
"desc"
:
"caoben zhiwu"
,
"price"
:
40
,
"producer"
:
"zhonghua producer"
,
"tags"
:
[
"qingxin"
]
}
,
{
"name"
:
"special yagao"
,
"desc"
:
"special meibai"
,
"price"
:
50
,
"producer"
:
"special yagao producer"
,
"tags"
:
[
"qingxin"
]
}
,
]
# 批量插入
def
gendata
(
)
:
for
idx
,
da
in
enumerate
(
datas
)
:
idx
+=
1
yield
{
"_index"
:
"ecommerce"
,
"_type"
:
"product"
,
"_id"
:
idx
,
"_source"
:
da
}
# 插入幾條新的數據
#
for
data
in
datas
:
# es
.
index
(
index
=
'ecommerce'
,
doc_type
=
'product'
,
body
=
data
)
# 批量插入
from
elasticsearch
import
helpers
result
=
helpers
.
bulk
(
es
,
gendata
(
)
)
print
(
result
)
# 批量插入數據庫的時候可能會遇到的問題
#
'reason'
:
'blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];'
}
,
body
=
{
"index.blocks.read_only_allow_delete"
:
"false"
}
result
=
es
.
indices
.
put_settings
(
body
=
body
)
print
(
result
)
全文檢索
dsl
=
{
'query'
:
{
'match'
:
{
'name'
:
'zhonghua'
}
}
}
result
=
es
.
search
(
index
=
'ecommerce'
,
doc_type
=
'product'
,
body
=
dsl
)
print
(
json
.
dumps
(
result
,
indent
=
2
,
ensure_ascii
=
False
)
)
# 多個字段查詢
aim_kw
=
"zhonghua"
query
=
{
"query"
:
{
"multi_match"
:
{
"query"
:
aim_kw
,
"fields"
:
[
"name"
,
"producer"
]
}
}
}
result
=
es
.
search
(
index
=
'ecommerce'
,
doc_type
=
'product'
,
body
=
query
)
print
(
json
.
dumps
(
result
,
indent
=
2
,
ensure_ascii
=
False
)
)
擴展
如何獲取_id的最大值,注意_id為string類型
GET
/
index
/
doc_type
/
_search
{
"stored_fields"
:
[
"_id"
]
,
"query"
:
{
"match_all"
:
{
}
}
,
"sort"
:
{
"_id"
:
"desc"
}
,
"size"
:
1
}
“”"
參考鏈接 https://cuiqingcai.com/6214.html
參考鏈接 https://www.cnblogs.com/liuxiaoming123/p/8124969.html
參考鏈接 https://blog.csdn.net/xuezhangjun0121/article/details/80745575
參考鏈接 https://blog.csdn.net/liuzemeeting/article/details/80708035
參考鏈接 https://www.jianshu.com/p/969e70782d1a
“”"
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
