Elasticsearch初探 python實現

2022-06-21 09:36:11 字數 4578 閱讀 4319

elasticsearch 是乙個全文搜尋引擎,可以快速地儲存、搜尋和分析海量資料。

它是乙個開源的搜尋引擎,建立在乙個全文搜尋引擎庫 apache lucene™ 基礎之上。但它又不僅僅是 lucene,它可以被下面這樣準確的形容:

在 elasticsearch 中有幾個基本的概念,如節點、索引、文件等等。下面簡單介紹一下。

node 和 cluster

elasticsearch 本質上是乙個分布式資料庫,允許多台伺服器協同工作,每台伺服器可以執行多個 elasticsearch 例項。

單個 elasticsearch 例項稱為乙個節點(node)。一組節點構成乙個集群(cluster)。

index

elasticsearch 資料管理的頂層單位就叫做 index(索引),其實就相當於 mysql、mongodb 等裡面的資料庫的概念。注意每個 index (即資料庫)的名字必須是小寫。

document

index 裡面單條的記錄稱為 document(文件)。許多條 document 構成了乙個 index。同乙個 index 裡面的document,不要求有相同的結構(scheme),但是最好保持相同,這樣有利於提高搜尋效率。

type

type是虛擬的邏輯分組,用來過濾 document,類似 mysql 中的資料。不同的 type 應該有相似的結構(schema)。根據規劃,elastic 6.x 版只允許每個 index 包含乙個 type,7.x 版將會徹底移除 type。

fields

每個 document 都類似乙個 json 結構,它包含了許多字段,每個欄位都有其對應的值,多個字段組成了乙個 document,其實就可以模擬 mysql 資料表中的字段。

elasticsearch 實際上提供了一系列 restful api 來進行訪問和查詢操作,我們可以使用 curl 等命令來進行操作,但畢竟命令列模式沒那麼方便,所以這裡我們就直接介紹利用 python 來對接 elasticsearch 的相關方法。

首先是python 中的es庫安裝

pip3 install elasticsearch
同時對於中文來說,我們需要安裝乙個分詞外掛程式,這裡使用的是 elasticsearch-analysis-ik,github 鏈結為:

這裡也可以用另乙個命令列工具elasticsearch-plugin install來安裝,注意版本號要和elasticsearch的版本號對應

elasticsearch-plugin install /releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip
接著介紹python操作es的方法,官方文件是:所有的用法都可以在裡面查到,文章後面的內容也是基於官方文件來的。

(1)建立索引

這裡我們建立乙個名為 news 的索引:

from elasticsearch import elasticsearch

es = elasticsearch()

result = es.indices.create(index='news', ignore=400)

print(result)

注意,這裡的 ignore 引數為 400,表示當索引若已經存在,就忽略這個報錯,不拋異常。

如果執行成功,返回

(2)刪除索引

刪除索引類似,可呼叫delete()方法

from elasticsearch import elasticsearch

es = elasticsearch()

result = es.indices.delete(index='news', ignore=[400, 404])

print(result)

注意這裡的ignore引數加了404,表示若索引本來不存在,則忽略這個報錯不拋異常。

(3)插入資料

1.es可以插入結構化字典資料,可以呼叫create()方法

from elasticsearch import elasticsearch

es = elasticsearch()

es.indices.create(index='news', ignore=400)

data =

result = es.create(index='news', doc_type='politics', id=1, body=data)

print(result)

我們傳入了四個引數,index 引數代表了索引名稱,doc_type 代表了文件型別,body 則代表了文件具體內容,id 則是資料的唯一標識 id。

執行結果為:

, '_seq_no': 0, '_primary_term': 1}
2.可以使用 index() 方法來插入資料,而可以不指定id,自動生成id。

es.index(index='news', doc_type='politics', body=data)
(4) 更新資料

1.es更新資料用update方法,制定具體的id和data即可。

from elasticsearch import elasticsearch

es = elasticsearch()

data =

result = es.update(index='news', doc_type='politics', body=data, id=1)

print(result)

2.也可以使用index()方法

es.index(index='news', doc_type='politics', body=data, id=1)
index方法若資料存在則更新,若資料不存在則插入。

返回結果

, '_seq_no': 1, '_primary_term': 1}
注意到_version這個字段,代表資料的版本,第一次插入是1,第二次更新為2。

(5)刪除資料

如果想刪除一條資料可以呼叫 delete() 方法,指定需要刪除的資料 id 即可。

from elasticsearch import elasticsearch

es = elasticsearch()

result = es.delete(index='news', doc_type='politics', id=1)

print(result)

返回

, '_seq_no': 2, '_primary_term': 1}
可以看到返回結果_version又變成了3。

(6) 查詢資料

from elasticsearch import elasticsearch

es = elasticsearch()

'properties': 

}}es.indices.delete(index='news', ignore=[400, 404])

es.indices.create(index='news', ignore=400)

print(result)

為了實驗檢索功能,我們先插入一批資料。

datas = [,,

,]

for data in datas:

es.index(index='news', doc_type='politics', body=data)

現根據關鍵字查詢資訊

1.查詢index和type下的所有資料,使用search()方法,指定索引和型別。

result = es.search(index='news', doc_type='politics')

print(result)

可以看到,剛剛插入的4條資料返回,返回結果會出現在 hits 字段裡面,然後其中有 total 字段標明了查詢的結果條目數,還有 max_score 代表了最大匹配分數。

,

"hits":

},},},}

]}}

2.全文檢索

elasticsearch 支援的 dsl 語句來進行查詢,使用 match 指定全文檢索,檢索的字段是 title,內容是「中國領事館」。

dsl = 

}} es = elasticsearch()

result = es.search(index='news', doc_type='politics', body=dsl)

print(json.dumps(result, indent=2, ensure_ascii=false))

返回結果如下:

,

"hits": },}

]}}

另外 elasticsearch 還支援非常多的查詢方式,詳情可以參考官方文件:

泛統計理論初探 初探XGBoost方法

初探xgboost方法 在本文中將會繼續介紹整合學習裡的boosting思路,並且會選用xgboost方法來介紹。xgboost是一種優化過的提公升學習方法,該方法在一些競賽或者專案中經常被使用,它的 正確率是比較高的,總體來說效能不錯。其實從該方法的名字我們可以發現,這是一種在gbdt方法的基礎上...

elasticsearch配置詳解

elasticsearch的config資料夾裡面有兩個配置檔案 elasticsearch.yml和logging.yml,第乙個是es的基本配置檔案,第二個是日誌配置檔案,es也是使用log4j來記錄日誌的,所以logging.yml裡的設定按普通log4j配置檔案來設定就行了。下面主要講解下e...

誰在使用Elasticsearch

github github使用elasticsearch搜尋20tb的資料,包括13億的檔案和1300億行的 這個不用介紹了吧,碼農們都懂的,github在2013年1月公升級了他們的 搜尋,由solr轉為elasticsearch,目前集群規模為26個索引儲存節點和8個客戶端節點 負責處理搜尋請求...