python中的Elasticsearch操作汇总

yipeiwu_com6年前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实现插入排序和选择排序的方法

Python实现插入排序和选择排序的方法

话不多说,让我们从最基本的排序算法开始吧 插入排序 如下图所示,插入排序的实现思路顾名思义,就是 不断地在一个已经是有序的数组中,寻找合适位置并插入新元素 。 具体实现步骤为: 首先我...

TensorFlow Session会话控制&Variable变量详解

TensorFlow Session会话控制&Variable变量详解

这篇文章主要讲TensorFlow中的Session的用法以及Variable。 Session会话控制 Session是TensorFlow为了控制和输出文件的执行语句,运行sessi...

python 实现图片旋转 上下左右 180度旋转的示例

如下所示: #首先建好一个数据_ud文件夹 import PIL.Image as img import os path_old = "C:/Users/49691/Desktop/...

python使用邻接矩阵构造图代码示例

问题 如何使用list构造图 邻接矩阵的方式 Python代码示例 # !/usr/bin/env python # -*-encoding: utf-8-*- # author:...

python 获取等间隔的数组实例

可以使用numpy中的linspace函数 np.linspace(start, stop, num, endpoint, retstep, dtype) #start和stop为起...