python使用elasticsearch模块操作elasticsearch

1、创建索引

命令如下

  from elasticsearch import Elasticsearch
    es = Elasticsearch([{"host":"10.87.6.3","port":9200},])

    s = "test" + "python"
    try:
        ret = es.indices.create(index=s)
    except Exception as e:
        print(e)
    else:
        print("创建成果")
        print(ret)

返回的结果如下,证明创建成果

{‘acknowledged‘: True, ‘shards_acknowledged‘: True, ‘index‘: ‘testpython‘}

返回的结果如下,证明创建失败

RequestError(400, ‘resource_already_exists_exception‘, ‘index [testpython/yEzYUZEiTjaZWxtN0RM_Uw] already exists‘)

2、删除索引

代码如下

    try:
        ret = es.indices.delete(index=s)
    except Exception as e:
        print(e)
    else:
        print("删除成果")
        print(ret)

返回结果,证明删除成果

{‘acknowledged‘: True}

返回结果,证明删除失败

NotFoundError(404, ‘index_not_found_exception‘, ‘no such index‘)

原文地址:https://www.cnblogs.com/bainianminguo/p/10419848.html

时间: 2024-07-31 06:37:22

python使用elasticsearch模块操作elasticsearch的相关文章

临时数据库之python用sqlite3模块操作sqlite

SQLite是一个包含在C库中的轻量级数据库.它并不需要独立的维护进程,并且允许使用非标准变体(nonstandard variant)的SQL查询语句来访问数据库. 一些应用可是使用SQLite保存内部数据.它也可以在构建应用原型的时候使用,以便于以后转移到更大型的数据库. SQLite的主要优点: 1. 一致性的文件格式: 在SQLite的官方文档中是这样解释的,我们不要将SQLite与Oracle或PostgreSQL去比较,与我们自定义格式的数据文件相比,SQLite不仅提供了很好的 移

python用ibm_db模块操作db2

安装模块的教程已经在本博客的其它文章中介绍,这里不再详述. 连接db2 conn=ibm_db.connect("DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s"%(database,hostname,port,user,password),"","") 查询语句 result = ibm_db.exec_immediate(conn,"select name,a

Python使用xlwt模块 操作Excel文件

导出Excel文件     1. 使用xlwt模块 import xlwt import xlwt    # 导入xlwt # 新建一个excel文件 file = xlwt.Workbook() #注意这里的Workbook首字母是大写,无语吧 # 新建一个sheet   table = file.add_sheet('sheet name') # 写入数据table.write(行,列,value) table.write(0,0,'test') # 如果对一个单元格重复操作,会引发 ret

python中os模块操作目录与文件名小结

(1). 创建目录: SigleDir = 'sigle_layer' MultiDir = 'D:\\Web\\multi_layer' 创建单层目录: os.mkdir(SigleDir) 创建多层目录: os.makedirs(MultiDir) (2). 文件路径和文件名的折分与合并 拆分:  dirname, filename = os.path.split('D:\\python_code\\split_functon.py') print 'dirname=',dirname,';

python之openpyxl模块操作

import openpyxl #写 # book = openpyxl.Workbook() # sheet = book.active #默认的sheet # #sheet2 = book.get_sheet_by_name('sheet1') # # sheet.append( ['id','username','password','error_count']) # # sheet.append( [1,'wyj','123456',0]) # # sheet.append( [2,'w

使用python操作elasticsearch实现数据插入分析

前言: 例行公事,有些人可能不太了解elasticsearch,下面搜了一段,大家瞅一眼. Elasticsearch是一款分布式搜索引擎,支持在大数据环境中进行实时数据分析.它基于Apache Lucene文本搜索引擎,内部功能通过ReST API暴露给外部.除了通过HTTP直接访问Elasticsearch,还可以通过支持Java.JavaScript.Python及更多语言的客户端库来访问.它也支持集成Apache Hadoop环境.Elasticsearch在有些处理海量数据的公司中已经

python操作elasticsearch

Python Elasticsearch  api(官方文档) 安装Elasticsearch模块 pip install elasticsearch 添加数据 from elasticsearch import Elasticsearch # 默认host为localhost,port为9200.但也可以指定host与port es = Elasticsearch("http://localhost:9200") # 添加或更新数据,index,doc_type名称可以自定义,id可

python elasticsearch模块使用

环境 : pyhton3 加载模块 from elasticsearch import Elasticsearch 连接ES es = Elasticsearch(["172.30.6.12"]) 查询 res = es.search(index="test-index", body={"query":{"match_all":{}}}) # 查询请求主机是ai.baidu.com 所有信息 res = es.search(i

java操作elasticsearch实现批量添加数据(bulk)

java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOException { //1.指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application"