原文链接

文档:http://elasticsearch-py.readthedocs.io/en/master/
Elasticsearch官方API文档:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
两种方式现实Elasticsearch API操作
方式一:安装elasticsearch模块,通过它操作Elasticsearch,代码示例如下

from elasticsearch import Elasticsearchclass ElasticSearchClass(object):def __init__(self, host, port, user, passwrod):self.host = hostself.port = portself.user = userself.password = passwrodself.connect()def connect(self):self.es = Elasticsearch(hosts=[{'host': self.host, 'port': self.port}],http_auth=(self.user, self.password ))def count(self, indexname):""":param indexname::return: 统计index总数"""return self.es.count(index=indexname)def delete(self, indexname, doc_type, id):""":param indexname::param doc_type::param id::return: 删除index中具体的一条"""self.es.delete(index=indexname, doc_type=doc_type, id=id)def get(self, indexname, id):return self.es.get(index=indexname, id=id)def search(self, indexname, size=10):try:return self.es.search(index=indexname, size=size, sort="@timestamp:desc")except Exception as err:print(err)

方式二:安装requests模块,通过GET、POST方式操作Elasticsearch

class RequestsElasticSearchClass(object):def __init__(self, host, port, user, passwrod):self.url = 'http://' + host + ':' + str(port)basicpwd = base64.b64encode((user + ':' + passwrod).encode('UTF-8'))self.headers = {"User-Agent": "shhnwangjian","Content-Type": "application/json","Authorization": "Basic {}".format(basicpwd.decode('utf-8'))}def search(self, indexname, size=10):gettdata = {"sort": "@timestamp:desc","size": size}url = self.url + '/' + indexname + '/_search'ret = requests.get(url, headers=self.headers, timeout=10, params=gettdata)print(ret.text)

python elasticsearch相关推荐

  1. python elasticsearch 入门教程(二) ---全文搜索

    python elasticsearch 入门教程(二) ---全文搜索 截止目前的搜索相对都很简单:单个姓名,通过年龄过滤.现在尝试下稍微高级点儿的全文搜索--一项 传统数据库确实很难搞定的任务. ...

  2. Python + ElasticSearch:有了这个超级武器,你也可以报名参加诗词大会了! | 博文精选...

    来源 | CSDN 博客 作者 | 天元浪子 责编 | Carol 出品 | CSDN云计算(ID:CSDNcloud) 意犹未尽的诗词大会 正月十六,中国诗词大会第五季落下帷幕.从2016年2月12 ...

  3. python elasticsearch.exceptions.ConnectionError: ConnectionError(check_hostname requires server_host

    代码 from elasticsearch import Elasticsearch from elasticsearch import RequestsHttpConnection from ssl ...

  4. python Elasticsearch 排序

    sort 与query是同级的 Elasticsearch python sort {"sort": [{"_score": {"order" ...

  5. python elasticsearch模块_Python 操作 ElasticSearch

    Python操作ElasticSearch Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/ ...

  6. python elasticsearch查询_python 查询Elasticsearch的小例子

    #!/usr/bin/env python # -*- coding: utf-8 -*- from sfo_common.agent import Agent from sfo_common.imp ...

  7. python elasticsearch update_使用python的elasticsearch部分更新

    我有以下格式的elasticsearch文档.我需要部分更新"x"字段并在其中添加python dict.{ "_index": "gdata34&q ...

  8. python elasticsearch模块_Python3 操作 elasticsearch

    elasticsearch 模块 Elasticsearch低级客户端.提供从Python到ES REST端点的直接映射. 连接集群节点 指定连接 es = Elasticsearch( ['172. ...

  9. python elasticsearch bulk_关于ElasticSearch Bulk的用法

    Background 刚接触ElasticSearch不久,公司让我帮忙去导一下数据,刚开始数量并不是很大,我都是用Elasticsearch python的api接口,一条一条数据去往新的index ...

  10. python elasticsearch 提示 ‘update‘ API and will be removed

    环境: Elasticsearch 版本:7.10.1 elasticsearch-analysis-ik 版本:7.10.1 Elasticsearch 操作的 Python 库版本:7.16.1 ...

最新文章

  1. recyclerview滑动到顶部和底部监听+上滑下滑监听
  2. mybatis中getMapper是怎么通过动态代理得到dao接口的实现类并执行mapper文件sql语句的
  3. 如果拿到私钥的话,https会被攻击吗?
  4. E104-BT02蓝牙无线传输模块物联网芯片的智能控制、采集
  5. 部署 Job (第二部分)
  6. IDEA build时出现Artifact contains illegal characters的解决
  7. 路由器和交换机的区别,太经典了
  8. 山东计算机科学自考科目,山东自考本科专业考试科目(动画设计、电脑艺术、计算机、电子工程、)...
  9. 自考CISSP信息分享
  10. 二元二次方程例题_二元二次方程组练习题及答案.doc
  11. Nebula Graph - 集群模式部署
  12. 为什么计算机打不开优盘,电脑显示u盘但打不开 为什么u盘连电脑有显示但打不开...
  13. 计算机档案管理系统论文,学生档案管理系统计算机论文.doc
  14. 基于JavaWeb的订餐管理系统的设计与实现
  15. 【论文笔记】Understanding Software Changes: Extracting, Classifying, and Presenting Fine-Grained Source Co
  16. 粗略的学习一下结巴分词
  17. 手持「前症保障」的百惠保,号称最容易理赔的重疾险?
  18. macOS卸载Karabiner-Elements
  19. Scratch3.0中保存项目时,建议使用的扩展名是sb3
  20. 度小满金融踩雷中粮信托近5000万,融资企业母公司为短融网大股东

热门文章

  1. 聊聊hystrix的execution.isolation.semaphore.maxConcurrentRequests属性
  2. [Erlang 0014]Erlang垃圾回收机制
  3. mysql 忘记root密码的解决
  4. ThinkPhp学习13
  5. Exchange Server 2013就地电子数据展示
  6. iOS开发- UICollectionView详解+实例
  7. c#实现数据集合转换为csv文本
  8. 802.11ac/ax (wifi6)中的Beamforming技术介绍
  9. SVN版本控制如何删除文件或者文件夹?
  10. CSP认证201604-3 路径解析[C++题解]:字符串处理、模拟、vector切分字符串