Python操作Elasticsearch
参考整理了一下,当做学习笔记,记录一下。
安装模块
pip install elasticsearch # 6.x版本
Python操作Elasticsearch
创建索引
from elasticsearch import Elasticsearch
es = Elasticsearch()
result = es.indices.create(index='news', ignore=400)
print(result) # {'acknowledged': True, 'index': 'news', 'shards_acknowledged': True} acknowledged 为True表示创建成功
删除索引
result = es.indices.delete("news", ignore=[400, 404])
print(result) # {'acknowledged': True}
插入一条document
# 法一
es.create("news", "politics", body=content, id=1)
# 法二
es.index("news", doc_type="politics", body=data) # ok 可以不用指定id, 参数id默认为随机创建
更新数据
# 法一
data = {'date': '2018-01-05 12:30:00',
'title': 'asd123',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm'}
result = es.update(index='news', doc_type='politics', body=data, id=1) # error
print(result)
data_doc = {'doc': {'date': '2018-01-05 12:30:00',
'title': 'asd123',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm'}}
result = es.update(index='news', doc_type='politics', body=data, id=1) # ok
print(result)
# 法二
data = {'date': '2018-01-05 12:30:00',
'title': 'asd123',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm'} # ok 使用data_doc也ok
es.index(index='news', doc_type='politics', body=data, id=1)
删除数据
result = es.delete("news", "politics", id='vNaqc2cBE_LRbsBxQ94C') # ok
查询数据
# 指定分词器 安装一个分词插件,这里使用的是 elasticsearch-analysis-ik
# mapping 信息中指定了分词的字段,指定了字段的类型 type 为 text,分词器 analyzer 和 搜索分词器 search_analyzer 为 ik_max_word
mapping = {
'properties': {
'title': {
'type': 'text',
'analyzer': 'ik_max_word',
'search_analyzer': 'ik_max_word'
}
}
}
query = {
'query': {
'match': {
'title': '中国 领事馆'
}
}
}
es = Elasticsearch()
result = es.search(index='news', doc_type='politics', body=query)
print(result)
查询并删除
# 匹配大于21岁的文档
query = {"query": {"range": {"age": {"gt": 21}}}}
es.delete_by_query(index='indexName', body=query, doc_type='typeName')
查询相关
datas = [{'age': 34,'date': '2018-01-01','sex': '男', 'title': '镖旗标题啊111,Python ElasticSearch基础教程'}, {'age': 10, 'date': '2017-05-13', 'sex': '男', 'title': '标题2'},{'age': 24, 'date': '2019-01-01', 'sex': '女', 'title': 'haha'}]
# 查询所有
query = {"query": {"match_all":{}}}
# 提供boost参数可以修改_score
query = {"query": {"match_all": {"boost": 1.2}}}
# 查询所有相反操作, 不匹配任何文档
query = {'query': {'match_none': {}}}
## 全文查询相关
#(1)匹配查询
query = {"query": {"match" : {"title" : "Python"}}}
# (2) 多匹配查询
query = {'query': {'multi_match': {'fields': ['title', 'sex'], 'query': '标题 男'}}}
# (3) term
body = {"query":{"term":{"title":"python"}}} # 查询title包含"python"的所有数据
es.search(index="bbs",doc_type="user",body=body)
# (4) terms
body = {"query":{"terms":{"title":["python","标题"]}}}
es.search(index="bbs",doc_type="user",body=body) # 搜索出title包含"python"或包含"标题"的所有数据
# (5) ids
body = {"query":{"ids":{"type":"user","values":["1", "z9Yrd2cBE_LRbsBxdt7t"]}}}
# 搜索出id为1或对应id的数据
es.search(index="bbs",doc_type="user",body=body)
# (6) 复合查询bool
# bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)
body = {'query': {'bool': {'should': [{'term': {'title': 'python'}},{'term': {'age': 24}}]}}}
es.search("bbs", doc_type="user", body=body)
# (7) 切片查询
body = {'from': 2, 'query': {'match_all': {}}, 'size': 4} # from 从第二条开始查询 size查询4条记录
# (8) 范围查询
body = {'query': {'range': {'age': {'gte': 10, 'lte': 32}}}} # 大于等于10,小于等于32
es.search("bbs", doc_type="user", body=body)
# (9) 前缀查询
body = {'query': {'prefix': {'title': '标'}}}
# (10) 通配符查询
body = {'query': {'wildcard': {'title': 'python*'}}}
# (11) 排序
body = {'query': {'match_all': {}}, 'sort': {'age': {'order': 'desc'}}} # 升序asc 降序desc
# (12) 相应过滤
es.search("bbs", "user", filter_path=["hits.hits._id", "hits.hits._source.title"]) # 获取id和对应的title
es.search("bbs", "user", filter_path=["hits.hits.*"]) # 获取所有数据
# (13) 执行查询并获取查询匹配数
es.count(index="bbs", doc_type="user") # {'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5}, 'count': 3}
参考文档:
https://elasticsearch-py.readthedocs.io/en/master/api.html#global-options
https://blog.csdn.net/u013429010/article/details/81746179