当前位置:   article > 正文

python操作Elasticsearch7.17.0_python elasticsearch7

python elasticsearch7

1 介绍

官方文档:
https://www.elastic.co/guide/en/enterprise-search-clients/python/7.17/index.html

pypi文档:
https://pypi.org/project/elasticsearch/7.17.0/

2 安装 连接

pip install elasticsearch==7.17.0
  • 1

异步 async/await

pip install elasticsearch[async]==7.17.0
  • 1
from elasticsearch import Elasticsearch
client = Elasticsearch(hosts=['http://192.168.56.20:9200'],
                       http_auth=("elastic", "密码"))
  • 1
  • 2
  • 3

3 索引操作

3.1 创建索引

def create_index(index, doc, index_id):
    client.create(index=index, document=doc, id=index_id)
  • 1
  • 2
'
运行
doc = {
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            },
            "age": {
                "type": "long"
            },
            "birthday": {
                "type": "date"
            }
        }
    }
}

create_index("test6", doc, "1")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.2 判断索引是否存在

def index_id_exists(index, id):
    return client.exists(index=index, id=id)
  • 1
  • 2
'
运行
if index_id_exists("test1", "2") == True:
    print("索引存在")
else:
    print("索引不存在")
  • 1
  • 2
  • 3
  • 4

4 新增数据

def add_to_es(index, doc, id):
    # 重复添加,数据覆盖
    try:
        client.index(index=index, document=doc, id=id)
        return '1'
    except Exception as e:
        print(e)
        return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
'
运行
doc = {
    "name": "灰太狼",
    "age": 22,
    "birthday":"2000-02-02",
    "tags": ["男"]
}
res = add_to_es("test1", doc, "10")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5 删除数据

def delete_by_index_and_id(index, id):
    try:
        res = client.delete(index=index, id=id)
        print(res['_shards']['successful'])
        return '1'
    except Exception as e:
        print(e)
        return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
'
运行
# 删除数据
if delete_by_index_and_id("test1", "1") == "1":
    print("删除成功")
else:
    print("删除失败或不存在")
  • 1
  • 2
  • 3
  • 4
  • 5

5 修改数据

def update_by_index_and_id(index, id, doc):
    try:
        client.update(index=index, id=id, doc=doc)
        return '1'
    except Exception as e:
        print(e)
        return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
'
运行
doc = {
    "name": "有勇气的牛排",
    "age": 22,
    "birthday": "2000-05-20",
    "tags": ["男"]
}

res = update_by_index_and_id("test1", "1", doc=doc)
if res == "1":
    print("更新成功")
else:
    print("数据不存在")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6 查询数据

6.1 查询所有数据

查询 通过 索引->_id

def find_by_index_and_id(index, id):
    try:
        res = client.get(index=index, id=id)
        return res
    except Exception as e:
        print(e)
        return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
'
运行
res = find_by_index_and_id("test1", "1")
print(res)
  • 1
  • 2

查询所有数据

# 查询 索引index 所有数据
async def find_by_index_all(self, index):
	client = UtilES().es_connect()
	body = {}
	# item = ["uid", "ip"]
	
	try:
	   # res = client.get(index=index, id=id)
	   res = client.search(index=index, body=body, scroll='5m', size=1000)
	   return res
	except Exception as e:
	   print(e)
	   return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6.2 search数据

def find_search_article(index, key, source, highlight=None):
    """
    :param index: 索引        source = ["a_title"]
    :param query: query
    :param source: 取出的字段
    :param highlight: 高亮
    :return:
    """
    # should: 条件满足一个即可
    query = {"bool": {"should": [{"match": {"a_title": key}}, {"match": {"a_content": key}}]}}
    # query = {"bool": {"should": [{"match": key}]}}
    client = connect_elk()
    try:
        res = client.search(index=index, query=query, _source=source, highlight=highlight)
        return res
    except Exception as e:
        print(e)
        return '0'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
'
运行
res = find_search_article("article", "人民教育", ["a_title","a_html"], highlight_red())
if res != 0:
    print(res["hits"])
    total = res["hits"]["total"]["value"]
    res = res["hits"]["hits"]
  • 1
  • 2
  • 3
  • 4
  • 5

参考文档:
https://www.cnblogs.com/lshan/p/15510018.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/893610
推荐阅读
相关标签
  

闽ICP备14008679号