elasticsearch API使用方法備忘(Python)
安裝模組
pip install elasticsearch
建立連線
from elasticsearch import Elasticsearch es = Elasticsearch(['192.168.1.1:9200']) |
多節點
es = Elasticsearch(['192.168.1.1:9200',’192.168.1.2:9200’]) |
支援SSL的連線
es = Elasticsearch( ['localhost:443', 'other_host:443'], # turn on SSL use_ssl=True, # make sure we verify SSL certificates (off by default) verify_certs=True, # provide a path to CA certs on disk ca_certs='/path/to/CA_certs' ) |
建立索引
es.indices.create(index='test_index', ignore=400) es.indices.delete(index='test_index', ignore=[400, 404]) |
ignore可以忽略異常,其值是需要忽略的異常對應的返回碼,常見的有400表示索引已存在,404表示索引沒找到。
使用自定義對映建立索引:
mapping = { "mappings": { "test_type": { "properties": { "name": { "type": "string", "index": "not_analyzed" }, "phone": { "type": "string", "index": "not_analyzed" } } } } } es.indices.create(index='test_index',ignore=400,body=mapping) |
檢視索引的對映:
es.indices.get_mapping("test_index") |
寫入資料
插入一條:
es.index(index='test_index’,doc_type='test_type',body={“key”:”value”}) |
批次寫入:
doc = [ {"index": {}}, {'name': 'Jack', 'phone': '123456'}, {"index": {}}, {'name': 'Joe', 'phone': '321456' }, {"index": {}}, {'name': 'Nicole', 'phone': '654321'}, {"index": {}}, {'name': 'Lucy', 'phone': '456123'}, ] es.bulk(index='test_index',doc_type='test_type',body=doc) |
刪除資料
根據id刪除一條資料
es.delete(index="test_index",doc_type="test_type",id="ZTg5IGMBxTpLs9ylvHBz") |
根據查詢條件刪除資料:
body = { "query":{ "match":{ "name": "Joe" } } } es.delete_by_query(index='test_index',doc_type='test_type',body=body) |
查詢
查詢所有資料
body = { "query":{ "match_all":{} } } es.search(index="test_index",doc_type="test_type",body=body) |
或者
es.search(index="test_index",doc_type="test_type") |
完全匹配term:
#搜尋name欄位為Nicole的資料 body = { "query":{ "term":{ "name": "Nicole" } } } es.search(index="test_index",doc_type="test_type",body=body) |
關鍵字匹配match:
#搜尋name欄位包含Nicole關鍵字的資料 body = { "query":{ "match":{ "name": "Nicole" } } } es.search(index="test_index",doc_type="test_type",body=body) |
bool聯合查詢,bool有3類查詢關係,must(都滿足),should(其中一個滿足),must_not(都不滿足)
#搜尋name為Nicole並且phone為123456的資料 body = { "query":{ "bool":{ "must":[ { "term":{ "name": "Nicole" } }, { "term":{ "phone": “123456” } } ] } } } es.search(index="test_index",doc_type="test_type",body=body) |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2035/viewspace-2804807/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 備忘錄Python
- python編譯備忘Python編譯
- Python設計模式-備忘錄模式Python設計模式
- ElasticSearch—— Java APIElasticsearchJavaAPI
- Elasticsearch Search APIElasticsearchAPI
- ElasticSearch Java APIElasticsearchJavaAPI
- Python中備忘功能和裝飾器Python
- RunLoop備忘OOP
- Vuepress 備忘Vue
- MySQL備忘MySql
- JS備忘JS
- SQL備忘SQL
- 備忘錄
- API文件使用方法API
- ElasticSearch Java API使用ElasticsearchJavaAPI
- elasticsearch(八)---search apiElasticsearchAPI
- Elasticsearch(二)——Rest APIElasticsearchRESTAPI
- ElasticSearch – API ConventionsElasticsearchAPI
- elasticsearch api client使用ElasticsearchAPIclient
- HTTPS備忘HTTP
- 備忘錄模式模式
- Git使用備忘Git
- iOS備忘錄iOS
- JUnit備忘錄
- CSS備忘錄CSS
- Oracle job備忘Oracle
- VITUALBOX備忘
- ApplicationDomain 備忘APPAI
- jsfl 備忘JS
- tmux使用備忘UX
- Elasticsearch cat api的用法ElasticsearchAPI
- ElasticSearch之基本用法APIElasticsearchAPI
- 使用 Java API 操作 elasticsearchJavaAPIElasticsearch
- Elasticsearch的Bulk API使用ElasticsearchAPI
- lldb常用操作備忘LLDB
- Java備忘錄《集合》Java
- 網站備忘錄網站
- Activity相關備忘