Elasticsearch DSL的使用

来自CloudWiki
跳转至: 导航搜索

Elasticsearch DSL的使用

Elasticsearch DSL是一个高级库,其目的是帮助编写和运行针对Elasticsearch的查询。它建立在官方低级客户端(elasticsearch-py)之上。

它提供了一种更方便和习惯的方式来编写和操作查询。它接近Elasticsearch JSON DSL,反映了它的术语和结构。它直接使用定义的类或类似查询集的表达式来暴露从Python的DSL的整个范围。

Elasticsearch DSL的安装

pip3 install elasticsearch-dsl==5.0.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

基本操作

导入和连接ES

创建dsl_test.py文件:

# 导入包
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q

es = Elasticsearch(hosts="http://127.0.0.1:9200/")  # 连接es

新建索引

#index和doc_type自己指定一个值即可,id可以指定,如果没有指定elasticsearch会随机分配,body即为索引的内容
dict_1 = {"any": "test", "timepstamp": "ddd"}
es.index(index="bank", doc_type="account", id="qwe", body=dict_1)

创建索引时还有一种先定义映射的方法,可以参加参考文档1和2

也可以不定义,但是如果想要使用join功能必须手动定义

查询文档

# 通过id获取特定文档,各参数的意义同上
res = es.get(index="bank", doc_type="account", id="qwe")
print(res)

更新文档

通过id更新文档:

es.update(index='bank', doc_type='account', id='qwe', body={"doc":{"any": "hello"}})

删除文档

es.delete(index='bank', doc_type='account', id='qwe')

查询所有

查询所有:

s = Search(using=es, index="ott1")   #using: 指定es 引擎  index:指定索引

response = s.params(size=1000).filter("match_all").execute()  # 查询1000条数据 并根据_id进
行排序

#注意: 如果不指定条数 默认只查询10条数据

#print(response)
print(response.to_dict())

python3 dsl_test.py:

[root@localhost py_test]# python3 dsl_test
{'took': 7, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'failed': 0}, 'hits': {'total': 6, 'max_score': 0.0, 'hits': [{'_index': 'ott1', '_type': 'ott_type1', '_id': '2', '_score': 0.0, '_source': {'date': '2017-09-13', 'source': '中国文明网', 'link': 'http://www.wenming.cn/xj_pd/yw/201709/t20170913_4421323.shtml', 'keyword': '电视', 'title': '电视 专题片《巡视利剑》广获好评:铁腕反腐凝聚党心民心'}}, {'_index': 'ott1', '_type': 'ott_type1', '_id': '4', '_score': 0.0, '_source': {'date': '2017-09-13', 'source': '站长之家', 'link': 'http://www.chinaz.com/news/2017/0913/804263.shtml', 'keyword': '电视', 'title': '电视 盒子 哪个牌子好? 吐血奉献三大选购秘笈'}}, {'_index': 'ott1', '_type': 'ott_type1', '_id': 'AWxhMSpVp5xr6Yl4nhAw', '_score': 0.0, '_source': {'date': '2017-09-13', 'source': '慧聪网', 'link': 'http://info.broadcast.hc360.com/2017/09/130859749974.shtml', 'keyword': '电视', 'title': '付费 电视 行业面临的转型和挑战'}}, {'_index': 'ott1', '_type': 'ott_type1', '_id': '1', '_score': 0.0, '_source': {'date': '2017-09-13', 'source': '慧聪网', 'link': 'http://info.broadcast.hc360.com/2017/09/130859749974.shtml', 'keyword': '电视', 'title': '付费 电视 行业面临的转型和挑战'}}, {'_index': 'ott1', '_type': 'ott_type1', '_id': '3', '_score': 0.0, '_source': {'date': '2017-09-13', 'source': '人民电视', 'link': 'http://tv.people.com.cn/BIG5/n1/2017/0913/c67816-29533981.html', 'keyword': '电视', 'title': '中国第21批赴刚果(金)维和部隊启程--人民 电视 --人民网'}}, {'_index': 'ott1', '_type': 'ott_type1', '_id': 'AWxiX4Zeqllbwy8yHqAB', '_score': 0.0, '_source': {'title': '网易财经-有态度的财经门户', 'link': 'https://money.163.com/', 'date': '2019-08-07', 'source': '网易', 'keyword': '实时股票行情,基金估值'}}]}}


参考文档:

[1] https://www.cnblogs.com/damon-/p/9647081.html

[2] https://blog.csdn.net/yaohuan2017/article/details/85338508