日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python操作Elasticsearch7.17.0

發(fā)布時(shí)間:2025/5/22 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python操作Elasticsearch7.17.0 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1 介紹
    • 2 安裝 連接
    • 3 索引操作
      • 3.1 創(chuàng)建索引
      • 3.2 判斷索引是否存在
    • 4 新增數(shù)據(jù)
    • 5 刪除數(shù)據(jù)
    • 5 修改數(shù)據(jù)
    • 6 查詢數(shù)據(jù)
      • 6.1 查詢所有數(shù)據(jù)
      • 6.2 search數(shù)據(jù)

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

異步 async/await

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

3 索引操作

3.1 創(chuàng)建索引

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

3.2 判斷索引是否存在

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

4 新增數(shù)據(jù)

def add_to_es(index, doc, id):# 重復(fù)添加,數(shù)據(jù)覆蓋try:client.index(index=index, document=doc, id=id)return '1'except Exception as e:print(e)return '0' doc = {"name": "灰太狼","age": 22,"birthday":"2000-02-02","tags": ["男"] } res = add_to_es("test1", doc, "10")

5 刪除數(shù)據(jù)

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' # 刪除數(shù)據(jù) if delete_by_index_and_id("test1", "1") == "1":print("刪除成功") else:print("刪除失敗或不存在")

5 修改數(shù)據(jù)

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' 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("數(shù)據(jù)不存在")

6 查詢數(shù)據(jù)

6.1 查詢所有數(shù)據(jù)

def find_by_index_and_id(index, id):try:res = client.get(index=index, id=id)return resexcept Exception as e:print(e)return '0' res = find_by_index_and_id("test1", "1") print(res)

6.2 search數(shù)據(jù)

def find_search_article(index, key, source, highlight=None):""":param index: 索引 source = ["a_title"]:param query: query:param source: 取出的字段:param highlight: 高亮:return:"""# should: 條件滿足一個(gè)即可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 resexcept Exception as e:print(e)return '0' 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"]

參考文檔:
https://www.cnblogs.com/lshan/p/15510018.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的python操作Elasticsearch7.17.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。