最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python調(diào)用Elasticsearch執(zhí)行增刪改查操作

 更新時(shí)間:2025年04月28日 11:10:17   作者:呆萌的代Ma  
Elasticsearch 是一種強(qiáng)大且靈活的分布式搜索引擎,而 Python 則以其易用性和強(qiáng)大的數(shù)據(jù)處理能力,成為開發(fā)者在數(shù)據(jù)操作中的理想選擇,本文將介紹二者如何結(jié)合實(shí)現(xiàn)增刪改查操作,感興趣的可以了解下

基本操作

1.連接Elasticsearch數(shù)據(jù)庫

首先連接Elasticsearch數(shù)據(jù)庫,然后創(chuàng)建一個(gè)自定義的索引

from elasticsearch import Elasticsearch
import random
from elasticsearch import helpers

# 連接到本地的 Elasticsearch 服務(wù)
es = Elasticsearch(hosts=["http://localhost:9200"])
index_name = "my_index"

# 創(chuàng)建一個(gè)索引名為 "my_index" 的索引
if es.indices.exists(index=index_name):  # 如果索引存在,刪除
    es.indices.delete(index=index_name)
es.indices.create(index=index_name)  # 新建索引

2.新增隨機(jī)數(shù)據(jù)

這里我們創(chuàng)建隨機(jī)數(shù)據(jù)項(xiàng),包含value_num與value_choice項(xiàng),

# 新增隨機(jī)數(shù)據(jù)項(xiàng)
add_value_list: list = []
for _ in range(1000):
    num = random.randint(1, 100)
    add_value_list.append({
        "_index": index_name,  # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中
        "value_num": random.randint(1, 100),
        "value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)],
    })
# 批量插入數(shù)據(jù)
helpers.bulk(es, add_value_list)

3.查詢操作

# 查詢操作
_body_query = {
    "query": {
        "range": {
            "value_num": {
                "gte": 40,  # >= 40
                "lte": 60  # <= 60
            }
        }
    },
    "size": 20,  # 查詢20條
}
response = es.search(index=index_name, body=_body_query)
# 打印查詢的結(jié)果
for _hit in response["hits"]["hits"]:
    _value = _hit['_source']
    print("value_num:", _value["value_num"], " value_choice:", _value['value_choice'])

4.更新數(shù)據(jù)項(xiàng)

這里,我們將查詢出的數(shù)據(jù)中,通過文檔ID與修改的數(shù)據(jù)重新為數(shù)據(jù)賦值

# 更新操作
for _hit in response["hits"]["hits"]:
    update_body = {"doc": {
        "value_choice": "c4",  # 更新value_choice字段為c4
    }}
    res = es.update(index=index_name, id=_hit['_id'], body=update_body)

5.刪除數(shù)據(jù)項(xiàng)

# 刪除操作
for _hit in response["hits"]["hits"]:
    res = es.delete(index=index_name, id=_hit['_id'])

更多查詢方法

1. 查詢?nèi)繑?shù)據(jù)

_body_query = {
    "query":{
        "match_all":{}
    }
}

2. 針對某個(gè)確定的值/字符串的查詢:term、match

match會執(zhí)行多個(gè)term操作,term操作精度更高

_body_query = {
    "query": {
        "match": {
            "value_choice": "c1"
        }
    }
}
_body_query = {
    "query": {
        "term": {
            "value_choice": "c1"
        }
    }
}

3. 在多個(gè)選項(xiàng)中有一個(gè)匹配,就查出來:terms

_body_query = {
    "query": {
        "terms": {
            "value_choice": ["c1", "c2"],
        }
    }
}

4. 數(shù)值范圍查詢:range

查詢>=40且<=60的數(shù)據(jù)

_body_query = {
    "query": {
        "range": {
            "value_num": {
                "gte": 40,  # >= 40
                "lte": 60  # <= 60
            }
        }
    }
}

5. 多個(gè)條件同時(shí)觸發(fā) bool

布爾查詢可以同時(shí)查詢多個(gè)條件,也稱為組合查詢,構(gòu)造查詢的字典數(shù)據(jù)時(shí),query后緊跟bool,之后再跟bool的判斷條件,判斷條件有下面幾個(gè):

  • filter:過濾器
  • must:類似and,需要所有條件都滿足
  • should:類似or,只要能滿足一個(gè)即可
  • must_not:需要都不滿足

寫完判斷條件后,在判斷條件的list里再緊跟查詢操作的具體細(xì)節(jié)

_body_query = {
    "query": {
        "bool": {
            "should": [
                {
                    "match": {"value_choice": "c1"} # value_choice = "c1"
                },
                {
                    "range": {"value_num": {"lte": 50}} # value_num <= 50
                }
            ]
        }
    },
}

6. 指定返回值個(gè)數(shù) size

在構(gòu)造的字典中添加size關(guān)鍵字即可

_body_query = {
    "query": {
        "range": {
            "value_num": {
                "gte": 40,  # >= 40
                "lte": 60  # <= 60
            }
        }
    },
    "size": 20,
}

7. 返回指定列 _source

_body_query = {
    "query": {
        "range": {
            "value_num": {
                "gte": 40,  # >= 40
                "lte": 60  # <= 60
            }
        }
    },
     "_source": ["value_num"] # 這里指定返回的fields
}

完整示例程序

from elasticsearch import Elasticsearch
import random
from elasticsearch import helpers

# 連接到本地的 Elasticsearch 服務(wù)
es = Elasticsearch(hosts=["http://localhost:9200"])
index_name = "my_index"

# 創(chuàng)建一個(gè)索引名為 "my_index" 的索引
if es.indices.exists(index=index_name):  # 如果索引存在,刪除
    es.indices.delete(index=index_name)
es.indices.create(index=index_name)  # 新建索引

# 生成隨機(jī)數(shù)據(jù)
add_value_list: list = []
for _ in range(1000):
    num = random.randint(1, 100)
    add_value_list.append({
        "_index": index_name,  # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中
        "value_num": random.randint(1, 100),
        "value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)],
    })
# 批量插入數(shù)據(jù)
helpers.bulk(es, add_value_list)

# ================== 開始增刪改查 ==================
_body_query = {
    "query": {
        "range": {
            "value_num": {
                "gte": 40,  # >= 40
                "lte": 60  # <= 60
            }
        }
    },
    "size": 20,
}

response = es.search(index=index_name, body=_body_query)  # 查詢10條
# 打印查詢的結(jié)果
for _hit in response["hits"]["hits"]:
    _value = _hit['_source']
    print("value_num:", _value["value_num"], " value_choice:", _value['value_choice'])

# 更新操作
for _hit in response["hits"]["hits"]:
    update_body = {"doc": {
        "value_choice": "c4",  # 更新value_choice字段為c4
    }}
    res = es.update(index=index_name, id=_hit['_id'], body=update_body)

# 刪除操作
for _hit in response["hits"]["hits"]:
    res = es.delete(index=index_name, id=_hit['_id'])

到此這篇關(guān)于python調(diào)用Elasticsearch執(zhí)行增刪改查操作的文章就介紹到這了,更多相關(guān)python Elasticsearch增刪改查操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在django中form的label和verbose name的區(qū)別說明

    在django中form的label和verbose name的區(qū)別說明

    這篇文章主要介紹了在django中form的label和verbose name的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python中urllib.unquote亂碼的原因與解決方法

    python中urllib.unquote亂碼的原因與解決方法

    這篇文章主要給大家介紹了python中urllib.unquote亂碼的原因與解決方法,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友可以參考學(xué)習(xí),下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • Python實(shí)現(xiàn)銀行賬戶資金交易管理系統(tǒng)

    Python實(shí)現(xiàn)銀行賬戶資金交易管理系統(tǒng)

    這篇文章主要介紹了Python銀行賬戶資金交易管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 使用Python加密和解密PDF文件

    使用Python加密和解密PDF文件

    在日常工作和生活中,保護(hù)PDF文件的隱私和安全至關(guān)重要,Python提供了一些強(qiáng)大的庫,使得加密和解密PDF文件變得相對簡單,本文將詳細(xì)介紹如何使用PyPDF2庫來加密和解密PDF文件,需要的朋友可以參考下
    2025-03-03
  • Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊

    Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊

    這篇文章主要給大家介紹了關(guān)于Python小游戲之300行代碼實(shí)現(xiàn)俄羅斯方塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2019-01-01
  • Python+Selenium實(shí)現(xiàn)自動化的環(huán)境搭建的步驟(圖文)

    Python+Selenium實(shí)現(xiàn)自動化的環(huán)境搭建的步驟(圖文)

    這篇文章主要介紹了Python+Selenium實(shí)現(xiàn)自動化的環(huán)境搭建的步驟(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python3.5模塊的定義、導(dǎo)入、優(yōu)化操作圖文詳解

    Python3.5模塊的定義、導(dǎo)入、優(yōu)化操作圖文詳解

    這篇文章主要介紹了Python3.5模塊的定義、導(dǎo)入、優(yōu)化操作,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Python3.5模塊的定義、導(dǎo)入及優(yōu)化等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-04-04
  • 基于Python制作一副撲克牌過程詳解

    基于Python制作一副撲克牌過程詳解

    這篇文章主要介紹了基于Python制作一副撲克牌過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Python有序容器的 sort 方法詳解

    Python有序容器的 sort 方法詳解

    這篇文章主要介紹了Python有序容器的 sort 方法,容器.sort(key=選擇排序依據(jù)的函數(shù), reverse=True|False) 可以將有序容器進(jìn)行排序,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • 簡單介紹Python中用于求最小值的min()方法

    簡單介紹Python中用于求最小值的min()方法

    這篇文章主要介紹了簡單介紹Python中用于求最小值的min()方法,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05

最新評論

灵台县| 太谷县| 宁武县| 瑞丽市| 阿巴嘎旗| 福建省| 马鞍山市| 泰顺县| 丰镇市| 通州区| 山阳县| 广平县| 策勒县| 彝良县| 南华县| 开平市| 泗阳县| 温宿县| 靖安县| 崇礼县| 灵台县| 宁远县| 平原县| 娱乐| 东兰县| 新源县| 尼玛县| 浦北县| 南溪县| 芮城县| 喜德县| 沭阳县| 遂宁市| 紫云| 罗平县| 滦平县| 周宁县| 常宁市| 鹤岗市| 长葛市| 德庆县|