安裝 6.0.0版本
## pip install elasticsearch
==
6.0
.0
# 導(dǎo)包
import
json
from
elasticsearch
import
Elasticsearch
創(chuàng)建Index – PUT /news?pretty
es
=
Elasticsearch
(
hosts
=
"ip:port"
)
# 創(chuàng)建一個(gè)名為news的索引
result
=
es
.
indices
.
create
(
index
=
'news'
,
ignore
=
400
)
print
(
result
)
# 創(chuàng)建成功
#
{
'acknowledged'
:
True
,
'shards_acknowledged'
:
True
,
'index'
:
'news'
}
# 如果再把代碼執(zhí)行一次的話,就會(huì)返回
:
status為
400
,
原因是Index已經(jīng)存在
#
{
'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
}
# # 如果再把代碼執(zhí)行一次的話,就會(huì)返回
:
status為
404
,
原因是Index已經(jīng)被刪除
# #
{
'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:每個(gè)索引的primary shard和replica shard都是active狀態(tài)的
yellow:每個(gè)索引的primary shard都是active狀態(tài)的,但是部分replica shard不是active狀態(tài),處于不可用的狀態(tài)
red:不是所有索引的primary shard都是active狀態(tài)的,部分索引有數(shù)據(jù)丟失了
為什么現(xiàn)在會(huì)處于一個(gè)yellow狀態(tài)?
本人一臺(tái)服務(wù)器,就啟動(dòng)了一個(gè)es進(jìn)程,相當(dāng)于就只有一個(gè)node。現(xiàn)在es中有一個(gè)index,就是kibana自己內(nèi)置建立的index。由于默認(rèn)的配置是給每個(gè)index分配
5
個(gè)primary shard和
5
個(gè)replica shard,而且primary shard和replica shard不能在同一臺(tái)機(jī)器上(為了容錯(cuò))。現(xiàn)在kibana自己建立的index是
1
個(gè)primary shard和
1
個(gè)replica shard。當(dāng)前就一個(gè)node,所以只有
1
個(gè)primary shard被分配了和啟動(dòng)了,但是一個(gè)replica shard沒(méi)有第二臺(tái)機(jī)器去啟動(dòng)。
""
"
插入數(shù)據(jù) – PUT /index/type/id
PUT
/
news
/
politics
/
1
{
'title'
:
'美國(guó)留給伊拉克的是個(gè)爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
}
# 在插入數(shù)據(jù)的時(shí)候可以直接插入結(jié)構(gòu)化字典數(shù)據(jù)
data
=
{
'title'
:
'美國(guó)留給伊拉克的是個(gè)爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
}
# # index 參數(shù)代表了索引名稱,doc_type 代表了文檔類型,body 則代表了文檔具體內(nèi)容,id 則是數(shù)據(jù)的唯一標(biāo)識(shí)
ID
result
=
es
.
create
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
,
body
=
data
)
print
(
result
)
# result 字段為 created,代表該數(shù)據(jù)插入成功
#
{
'_index'
:
'news'
,
'_type'
:
'politics'
,
'_id'
:
'1'
,
'_version'
:
1
,
'result'
:
'created'
,
'_shards'
:
{
'total'
:
2
,
'successful'
:
1
,
'failed'
:
0
}
,
'_seq_no'
:
0
,
'_primary_term'
:
1
}
# 也可以使用
index
(
)
方法來(lái)插入數(shù)據(jù),但與
create
(
)
不同的是,create
(
)
方法需要我們指定 id 字段來(lái)唯一標(biāo)識(shí)該條數(shù)據(jù),而
index
(
)
方法則不需要,如果不指定 id,會(huì)自動(dòng)生成一個(gè) id
#
create
(
)
方法內(nèi)部其實(shí)也是調(diào)用了
index
(
)
方法,是對(duì)
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
)
更新數(shù)據(jù) – 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
;
# 數(shù)據(jù)增加了一個(gè)日期字段,然后調(diào)用了
update
(
)
方法
result
=
es
.
update
(
index
=
'news'
,
doc_type
=
'politics'
,
id
=
1
,
body
=
data
)
print
(
result
)
# ## result 字段為 updated,即表示更新成功,另外我們還注意到有一個(gè)字段 _version,這代表更新后的版本號(hào)數(shù),
2
代表這是第二個(gè)版本,因?yàn)橹耙呀?jīng)插入過(guò)一次數(shù)據(jù),所以第一次插入的數(shù)據(jù)是版本
1
,可以參見上例的運(yùn)行結(jié)果,這次更新之后版本號(hào)就變成了
2
,以后每更新一次,版本號(hào)都會(huì)加
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'
:
'美國(guó)留給伊拉克的是個(gè)爛攤子嗎'
,
'url'
:
'http://view.news.qq.com/zt2011/usa_iraq/index.htm'
,
'date'
:
'2011-12-16'
}
##
index
(
)
方法可以代替我們完成兩個(gè)操作,如果數(shù)據(jù)不存在,那就執(zhí)行插入操作,如果已經(jīng)存在,那就執(zhí)行更新操作,非常方便
0
#es
.
index
(
index
=
'news'
,
doc_type
=
'politics'
,
body
=
data
,
id
=
1
)
刪除相應(yīng)的數(shù)據(jù) – 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
}
# 刪除滿足該條件的數(shù)據(jù)
query
=
{
'query'
:
{
'match'
:
{
"_id"
:
"BxCklGwBt0482SoSeXuE"
}
}
}
result
=
es
.
delete_by_query
(
index
=
'news'
,
body
=
query
,
doc_type
=
'politics'
)
print
(
result
)
對(duì)于中文來(lái)說(shuō),需要安裝一個(gè)分詞插件,這里使用的是 elasticsearch-analysis-ik,GitHub 鏈接為:https://github.com/medcl/elasticsearch-analysis-ik,這里我們使用 Elasticsearch 的另一個(gè)命令行工具 elasticsearch-plugin 來(lái)安裝,這里安裝的版本是 6.0.0
在安裝這個(gè)分詞插件之后,需要重啟elasticsearch
進(jìn)入到安裝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
# 新建一個(gè)索引并指定需要分詞的字段
mapping = {
'properties': {
'title': {
'type': 'text',
'analyzer': 'ik_max_word',
'search_analyzer': 'ik_max_word'
}
}
}
# 先刪除之前的索引
## mapping 信息中指定了分詞的字段,指定了字段的類型 type 為 text,分詞器 analyzer 和 搜索分詞器 search_analyzer 為 ik_max_word,即使用我們剛才安裝的中文分詞插件。如果不指定的話則使用默認(rèn)的英文分詞器。
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)
插入數(shù)據(jù)操作
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
}
# 插入幾條新的數(shù)據(jù)
#
for
data
in
datas
:
# es
.
index
(
index
=
'ecommerce'
,
doc_type
=
'product'
,
body
=
data
)
# 批量插入
from
elasticsearch
import
helpers
result
=
helpers
.
bulk
(
es
,
gendata
(
)
)
print
(
result
)
# 批量插入數(shù)據(jù)庫(kù)的時(shí)候可能會(huì)遇到的問(wèn)題
#
'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
)
)
# 多個(gè)字段查詢
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
)
)
擴(kuò)展
如何獲取_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
“”"
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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