当前位置:首页 > 数据库 > es > 正文内容

python 操作es

root2年前 (2022-07-20)es657
pip install elasticsearch

连接建立

from elasticsearch import Elasticsearch

es = Elasticsearch([{
    'host': "127.0.0.1", 'port': 1234}],
    timeout=30, max_retries=10, retry_on_timeout=True,
    http_auth=("user", "password"))

es 一般都是集群方式,第一个变量是一个集群ip:port的数组,当然还有其他的写法

http_auth 是开启http认证的服务,需要填上用户密码


创建索引

es.indices.create(index="indexName",
         body={"settings": {"number_of_replicas": 1, "number_of_shards": 5, "max_result_window": 50000}})

number_of_replicas指定有多少个副本 ,number_of_shards索引要做多少个分片

body 可以是空字典,都采用默认值


删除索引

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

判断索引是否存在

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


设置mapping

mapping = {
    "properties": {
        "name": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "password": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        }
    } 
}
es.indices.put_mapping(body=mapping, index="indexName")


设置别名

es.indices.put_alias(index="indexName", name="indexAlias")

删除别名

es.indices.delete_alias(index="indexName", name="indexAlias")


查询数据

query = {
        "query": {
            # 范围查询
            "range": {
                "字段1": {}
            },

            # 关键字匹配查询
            "match": {
                "字段1": ""
            },

            # 查询所有文档
            "match_all": {
                "字段1": ""
            },
            # 多字段搜索
            "multi_match": {
                "query": "",
                "fields": ["字段1", "字段2"]
            },

            # 多字段搜索,与multi_match区别是真正的包含搜索的目标,
            "match_phrase": {
                "字段": ""
            },

            # 查询满足条件的文档,是相等关系不是包含关系
            "term": {
                "字段1": ""
            },
            # 多个条件 terms: 查询 xx = “xx” 或 xx = “yy”
            "terms": {
                "字段1": ["xx", "yy"]
            },

            # bool 查询 must, must_not, should
            "bool": {
                "must": [
                    {"term条件1"}, {"term条件2"}
                ],
                "must_not": [],

                "should": [],

                "filter": []
            }
        },
        "_source": {"includes": []},
        "from": 0,
        "size": 10
    }
 es.search(index="indexName", body=query)

查询的query 只需要其中的一两项就行,根据实际情况选择查询的条件方式


插入数据

message={
    "_index": es_index,
    "_type": es_type,
    "_id": id,
    "_source":{
        "字段1":"123",
        "字段2":"223",
        "字段3":"323",
        }
}
es.index(index=index, doc_type=type, body=message["_source"])


# 批量更新 常用

from elasticsearch import helpers

action = []
action.append(
    {"_index": "",
     "_type": "",
     "_source": {}}
)
helpers.bulk(es, action)

更新数据

es.update(index=index, doc_type=type, body=message["_source"])

删除数据

es.delete_by_query(index='indexName', body=query)


扫描二维码推送至手机访问。

版权声明:本文由一叶知秋发布,如需转载请注明出处。

本文链接:https://zhiqiu.top/?id=238

分享给朋友:
返回列表

没有更早的文章了...

下一篇:es集群搭建 docker方式

相关文章

es集群搭建 docker方式

参考连接:开启认证 集群搭建并开启认证 docker-compose方式搭建集群 遇见问题的并解决 matser 搭建脚本映射数据目录需要给外边文件夹777权限 docker内容数据文件地址:/usr/share/elasticsearch...