python下的Elasticsearch操作

Posted simplekinght

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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下的Elasticsearch操作的主要内容,如果未能解决你的问题,请参考以下文章

python使用elasticsearch模块操作elasticsearch

python操作elasticsearch

elasticsearch之python操作(非原生)

elasticsearch之python操作(非原生)

python3 操作elasticsearch

python对接elasticsearch的基本操作