操作几个方面
结果过滤, 对于返回结果做过滤, 主要是优化返回内容.
直接操作 Elasticsearch 对象, 处理一些简单的索引信息. 一下几个方面都是建立在 es 对象的基础上.
Indices, 关于索引的细节操作, 比如创建自定义的 mappings.
Cluster, 关于集群的相关操作.
Nodes, 关于节点的相关操作.
Cat API, 换一种查询方式, 一般的返回都是 JSON 类型的, cat 提供了简洁的返回结果.
Snapshot, 快照相关, 快照是从正在运行的 Elasticsearch 集群中获取的备份. 我们可以拍摄单个索引或整个群集的快照, 并将其存储在共享文件系统的存储库中, 并且有一些插件支持 S3,HDFS,Azure,Google 云存储等上的远程存储库.
Task Management API, 任务管理 API 是新的, 仍应被视为测试版功能. API 可能以不向后兼容的方式更改.
结果过滤
filter_path 参数用于过滤减少 es 返回信息, 可以指定返回相关的内容, 还支持一些通配符的操作 *
- body = {
- "query": {
- "match": {
- "name": "成都"
- }
- }
- }
- # print(es.search(index="p1", body=body))
- print(es.search(index="p1", body=body, filter_path=["hits.hits"]))
- print(es.search(index="p1", body=body, filter_path=["hits.hits._source"]))
- print(es.search(index="p1", body=body, filter_path=["hits.hits._source", "hits.total"]))
- print(es.search(index="p1", body=body, filter_path=["hits.*"]))
- print(es.search(index="p1", body=body, filter_path=["hits.hits._*"]))
- Elasticsearch(es 对象 )
es.index 向指定索引添加更新文档 如果索引不存在就会创建, 然后执行添加更新等操作
- # print(es.index(index="p2", doc_type="doc", id=1, body={"name": "棒槌", "age": "18"})) # 正常
- # print(es.index(index="p2", doc_type="doc", id=2, body={"name": "棒棒哒", "age": 20})) # 正常
- # print(es.index(index="p2", doc_type="doc", body={"name": "熊大", "age": "10"})) # 如果添加文档不带 id 自动会创建一个
es.get 查询索引中指定文档
- # get(self, index, id, doc_type="_doc", params=None):
- # print(es.get(index='p2', doc_type='doc', id=1)) # 正常
- # print(es.get(index='p2', doc_type='doc')) # TypeError: get() missing 1 required positional argument: 'id' 只能取单个文档对象
- # print(es.get(index='p2', id=2)) # 要指定文档类型 Elasticsearch.exceptions.NotFoundError: NotFoundError(404, '{"_index":"p2","_type":"_doc","_id":"2","found":false}')
es.search 执行搜索查询并获取其匹配它可以跟复杂的查询条件
index 要搜索的以逗号分隔的索引名称列表; 使用_all 或空字符串对所有索引执行操作.
doc_type 要搜索的以逗号分隔的文档类型列表; 留空以对所有类型执行操作.
body 使用 Query DSL(QueryDomain Specific Language 查询表达式)的搜索定义.
_source 返回_source 字段的 true 或 false, 或返回的字段列表, 返回指定字段.
_source_exclude 要从返回的_source 字段中排除的字段列表, 返回的所有字段中, 排除哪些字段.
_source_include 从_source 字段中提取和返回的字段列表, 跟_source 差不多.
- # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}))
- # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source=['name', 'age']))
- # print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source_exclude=['age']))
- print(es.search(index='p2', body={"query": {"match": {"age": "10"}}}, _source_include=['age']))
排除结果
{'took': 3, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 2, 'max_score': 0.2876821, 'hits': [{'_index': 'p2', '_type': 'doc', '_id': 'lEP_u2wBsrL_vQjNIMFf', '_score': 0.2876821, '_source': {'name': '熊大'}}, {'_index': 'p2', '_type': 'doc', '_id': 'W0MjvGwBsrL_vQjNNd0x', '_score': 0.2876821, '_source': {'sex': '男', 'name': '熊大', 'desc': '卡通智障儿童'}}]}}
es.get_source, 通过索引, 类型和 ID 获取文档的来源, 其实, 直接返回想要的字典.
1 print(es.get_source(index='p2', doc_type='doc', id='1'))
es.count, 执行查询并获取该查询的匹配数. 比如查询年龄是 18 的文档.
- body = {
- "query": {
- "match": {
- "age": 18
- }
- }
- }
- # print(es.count(index='p2', doc_type='doc', body=body))
- # print(es.count(index='p2', doc_type='doc', body=body)['count'])
- print(es.count(index='p2')) # 查在 p2 索引中总的文档个数{'count': 4, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
- print(es.count(index='p2', doc_type='doc')) # {'count': 4, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}}
es.create, 创建索引 (索引不存在的话) 并新增一条数据, 索引存在仅新增(只能新增, 重复执行会报错), 还是觉得 index 好用些
- # print(es.create(index='p2', doc_type='doc', id=3, body={"city": "成都", "desc": "旅游的地方颇多, 小吃出名"}))
- print(es.get_source(index='p2', doc_type='doc', id=3))
es.delete, 删除指定的文档. 比如删除文章 id 为 4 的文档, 但不能删除仅只删除索引, 如果想要删除索引, 还需要 es.indices.delete 来处理
es.delete_by_query, 删除与查询匹配的所有文档.
- # print(es.delete(index='p2', doc_type='doc', id=1))
- # print(es.delete_by_query(index='p2', body={"query": {"match": {"age": 20}}}))
- # print(es.search(index='p2'))
es.exists, 查询 Elasticsearch 中是否存在指定的文档, 返回一个布尔值.
1 print(es.exists(index='p2', doc_type='doc', id='1'))
es.info, 获取当前集群的基本信息.
1 print(es.info())
es.ping, 如果集群已启动, 则返回 True, 否则返回 False.
- 1 print(es.ping())
- Indices(es.indices 索引相关)
创建索引
- body = {
- "mappings": {
- "doc": {
- "dynamic": "strict",
- "properties": {
- "title": {
- "type": "text",
- "analyzer": "ik_max_word"
- },
- "url": {
- "type": "text"
- },
- "action_type": {
- "type": "text"
- },
- "content": {
- "type": "text"
- }
- }
- }
- }
- }
- print(es.indices.create(index='p3', body=body))
创建结果
1 {'acknowledged': True, 'shards_acknowledged': True, 'index': 'p3'}
es.indices.analyze, 返回分词结果.
1 print(es.indices.analyze(body={'analyzer': 'ik_max_word', 'text': "皮特和茱丽当选" 年度模范情侣 "Brad Pitt and Angelina Jolie"}))
es.indices.delete, 在 Elasticsearch 中删除索引.
- print(es.indices.delete(index='p3'))
- print(es.indices.delete(index='p2')) # {'acknowledged': True}
es.indices.put_alias, 为一个或多个索引创建别名, 查询多个索引的时候, 可以使用这个别名.
- print(es.indices.put_alias(index='p3', name='p3_alias')) # 为单个索引创建别名
- print(es.indices.put_alias(index=['p3', 'p2'], name='p23_alias')) # 为多个索引创建同一个别名, 联查用
es.indices.delete_alias, 删除一个或多个别名.
- print(es.indices.delete_alias(index='p1'))
- print(es.indices.delete_alias(index=['p1, p2']))
es.indices.get_mapping, 检索索引或索引 / 类型的映射定义.
1 print(es.indices.get_mapping(index='p3'))
es.indices.get_settings, 检索一个或多个 (或所有) 索引的设置.
1 print(es.indices.get_settings(index='p3'))
es.indices.get, 允许检索有关一个或多个索引的信息.
- print(es.indices.get(index='p2')) # 查询指定索引是否存在
- print(es.indices.get(index=['p2', 'p3']))
es.indices.get_alias, 检索一个或多个别名.
- print(es.indices.get_alias(index='p2'))
- print(es.indices.get_alias(index=['p2', 'p3']))
es.indices.get_field_mapping, 检索特定字段的映射信息.
1 print(es.indices.get_field_mapping(fields='url', index='
来源: http://www.bubuko.com/infodetail-3167646.html