elasticsearch API使用方法备忘(Python)
2018-05-03 17:39
816 查看
安装模块
pipinstallelasticsearch创建连接
fromelasticsearchimportElasticsearch es=Elasticsearch(['192.168.1.1:9200']) |
es=Elasticsearch(['192.168.1.1:9200',’192.168.1.2:9200’]) |
es=Elasticsearch(['localhost:443','other_host:443'],#turnonSSLuse_ssl=True,#makesureweverifySSLcertificates(offbydefault)verify_certs=True,#provideapathtoCAcertsondiskca_certs='/path/to/CA_certs') |
创建索引
es.indices.create(index='test_index',ignore=400)es.indices.delete(index='test_index',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") |
#搜索name字段为Nicole的数据 body={ "query":{ "term":{ "name":"Nicole" } } } es.search(index="test_index",doc_type="test_type",body=body) |
#搜索name字段包含Nicole关键字的数据 body={ "query":{ "match":{ "name":"Nicole" } } } es.search(index="test_index",doc_type="test_type",body=body) |
#搜索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) |
相关文章推荐
- 【备忘】python2和python3同时安装使用的方法
- p4.pm p4python p4perl p4api 的使用方法
- Python Selenium 部分API详细使用方法(三)
- Python/C API使用方法简介 (在C/C++中嵌入Python)
- Elasticsearch上手——Python API的简单使用
- 【原】Python/C API使用方法简介 (在C/C++中嵌入Python)
- maya python api中引用作为形参的使用方法
- Python Selenium 部分API详细使用方法(二)
- Python Selenium 部分API详细使用方法(一)
- IP Helper API 使用方法
- DWRUtils API 使用方法
- DWREngine API 使用方法
- 使用J2SE API读properties文件的六种方法
- 不使用 java.awt.Desktop API,打开默认浏览器访问指定链接的方法
- 51地图使用方法,及API
- 在.NET中使用API的方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 在Python DB API中使用存储过程