分布式搜索引擎Elasticsearch非常受欢迎,对应Python库为elasticsearch;
from elasticsearch import Elasticsearch
# 连接引擎
es = Elasticsearch(hosts=['127.0.0.1:9200'],http_auth=('elastic','password'))
# 创建索引
es.indices.create(index='test-index', ignore=400)
# 删除索引
es.indices.delete(index='test-index', ignore=[400,404])
下段为对引擎进行增加和删除数据操作部分;
msg = {'author': 'user','text': 'this is a test msg'}
# 对索引添加数据,无需设置ID,会自动添加
res = es.index(index="test-index", document=msg)
# 对索引添加数据,需要设置ID,ID不能重复
res = es.create(index="test-index", id=1314, document=msg)
# 删除索引中对应ID数据
res = es.delete(index="test-index", id=1314)
# 删除索引中对应author为user的数据
query = {"query": {"match": {'author': 'user'}}}
res = es.delete_by_query(index="test-index", body=query)
下段为使用引擎进行简单搜索的操作部分;
# 查询索引中对应author为user的数据
query = {"match": {'author': 'user'}}
res = es.search(index="test-index", query=query)
# 查询索引中所有数据
query = {"match_all":{}}
res = es.search(index="test-index", query=query)
# 查询索引的数据量
res = es.count(index="test-index")
# 查询索引中对应author为user的数据量
query = {"query": {"match": {'author': 'user'}}}
res = es.count(index="test-index", body=query)