python操作Elasticsearch7.17.0

x33g5p2x  于2022-06-27 转载在 Python  
字(3.0k)|赞(0)|评价(0)|浏览(511)

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 安装 连接

  1. pip install elasticsearch==7.17.0

异步 async/await

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

3 索引操作

3.1 创建索引

  1. def create_index(index, doc, index_id):
  2. client.create(index=index, document=doc, id=index_id)
  1. doc = {
  2. "mappings": {
  3. "properties": {
  4. "name": {
  5. "type": "text"
  6. },
  7. "age": {
  8. "type": "long"
  9. },
  10. "birthday": {
  11. "type": "date"
  12. }
  13. }
  14. }
  15. }
  16. create_index("test6", doc, "1")

3.2 判断索引是否存在

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

4 新增数据

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

5 删除数据

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

5 修改数据

  1. def update_by_index_and_id(index, id, doc):
  2. try:
  3. client.update(index=index, id=id, doc=doc)
  4. return '1'
  5. except Exception as e:
  6. print(e)
  7. return '0'
  1. doc = {
  2. "name": "有勇气的牛排",
  3. "age": 22,
  4. "birthday": "2000-05-20",
  5. "tags": ["男"]
  6. }
  7. res = update_by_index_and_id("test1", "1", doc=doc)
  8. if res == "1":
  9. print("更新成功")
  10. else:
  11. print("数据不存在")

6 查询数据

6.1 查询所有数据

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

6.2 search数据

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

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

相关文章