总结使用python对于elasticsearch的常用操作
- 安装
pip install elasticsearch
2. 连接
from elasticsearch import Elasticsearch es = Elasticsearch([{'host':'49.232.6.227' , 'port':9200}], timeout=3600) # 添加验证 # http_auth=('xiao', '123456')
3. 查询
1)全部查询
query = { 'query': { 'match_all': {} } } result = es.search(index=account_index, body=query) for row in result['hits']['hits']: print(row)
2)term 过滤--term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型)
query = { "query": { "term":{ 'age': 32 } } } result = es.search(index="megacorp", body=query) print(result) # first_name 可能经过切词了 query = { "query": { "term":{ 'first_name': 'Jane' } } } result = es.search(index="megacorp", body=query) print(result)
3)terms 过滤--terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配
query = { 'query': { 'terms': { 'name': ['111111', '22222'] } } }