python中的Elasticsearch操作汇总

yipeiwu_com5年前Python基础

这篇文章主要介绍了python中的Elasticsearch操作汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

导入包

from elasticsearch import Elasticsearch

本地连接

es = Elasticsearch(['127.0.0.1:9200'])

创建索引

es.indices.create(index="python_es01",ignore=400)

ingore=400 ingore是忽略的意思,400是未找到

删除索引

es.indices.delete(index="python_es01")

检查索引是否存在

es.indices.exists(index="python_es01")

插入数据

es.index(index="python_es01",doc_type="doc",id=1,body={"name":"kitty","age":50})

同时也可以不加id,即

es.index(index="python_es01",doc_type="doc",body={"name":"kitty","age":10})

查询操作

按id查询

result = es.get(index="python_es01",doc_type="doc",id=1)

会有一个返回值

全查

body= {
    "query":{
      "match_all":{}
    }
  }
result = es.search(index="python_es01",body=body)

使用id的用GET,其他search

删除操作

result = es.delete(index="goods",doc_type="type1",id=2)

按查询结果删除

result = es.delete_by_query(index="goods",body=body)

建立mapping

body = {
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "price": {
        "type": "long"
      }
    }
  }
}
result = es.indices.create(index="shang",body=body)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python XlsxWriter模块创建aexcel表格的实例讲解

python XlsxWriter模块创建aexcel表格的实例讲解

安装使用pip install XlsxWriter来安装,Xlsxwriter用来创建excel表格,功能很强大,下面具体介绍: 1.简单使用excel的实例: #coding:u...

Linux下升级安装python3.8并配置pip及yum的教程

Linux下升级安装python3.8并配置pip及yum的教程

服务器的CentOS 7中自带的python版本是python-2.7.5,需要再安装一个 python-3.8.1 一、查看版本 安装前查看一下是否已经安装过python,我...

Python中一些深不见底的“坑”

Python是一门清晰简洁的语言,如果你对一些细节不了解的话,就会掉入到那些深不见底的“坑”里,下面,我就来总结一些Python里常见的坑。 列表创建和引用 嵌套列表的创建 使用*号...

使用Selenium破解新浪微博的四宫格验证码

使用Selenium破解新浪微博的四宫格验证码

在我们爬虫的时候经常会遇到验证码,新浪微博的验证码是四宫格形式。 可以采用模板验证码的破解方式,也就是把所有验证码的情况全部列出来,然后拿验证码的图片和这所有情况中的图片进行对比,然后获...

Python打开文件,将list、numpy数组内容写入txt文件中的方法

python保存numpy数据: numpy.savetxt("result.txt", numpy_data); 保存list数据: file=open('data.txt'...