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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python elasticsearch 入门教程(一)

發布時間:2025/4/5 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python elasticsearch 入门教程(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫入數據

from elasticsearch import Elasticsearch es = Elasticsearch() body1={"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests": [ "sports", "music" ] }#余下代碼為寫入三段數據 body2={"first_name" : "Jane","last_name" : "Smith","age" : 32,"about" : "I like to collect rock albums","interests": [ "music" ] }body3={"first_name" : "Douglas","last_name" : "Fir","age" : 35,"about": "I like to build cabinets","interests": [ "forestry" ] }res1 = es.index(index="megacorp", doc_type='employee', id=1,body=body1) res2 = es.index(index="megacorp", doc_type='employee', id=2,body=body2) res3 = es.index(index="megacorp", doc_type='employee', id=3,body=body3)

這是一個elasticsearch的簡單查詢,要求匹配 名字為”Smith”的文檔,有沒有和mongodb有點像.

bb1={"query" : {"match" : {"last_name" : "Smith" }}}rt1= es.search(index="megacorp", body=bb1) print(rt1) Out[11]: {'_shards': {'failed': 0, 'skipped': 0, 'successful': 5, 'total': 5},'hits': {'hits': [{'_id': '2','_index': 'megacorp','_score': 0.2876821,'_source': {'about': 'I like to collect rock albums','age': 32,'first_name': 'Jane','interests': ['music'],'last_name': 'Smith'},'_type': 'employee'},{'_id': '1','_index': 'megacorp','_score': 0.2876821,'_source': {'about': 'I love to go rock climbing','age': 25,'first_name': 'John','interests': ['sports', 'music'],'last_name': 'Smith'},'_type': 'employee'}],'max_score': 0.2876821,'total': 2},'timed_out': False,'took': 309}

現在嘗試下更復雜的搜索。 同樣搜索姓氏為 Smith 的雇員,但這次我們只需要年齡大于 30 的。查詢需要稍作調整,使用過濾器 filter ,它支持高效地執行一個結構化查詢。

bb2={"query" : {"bool": {"must": { "match" : { "last_name" : "smith" }},"filter": {"range" : {"age" : { "gt" : 30 } }}}} } rt2= es.search(index="megacorp", body=bb2) print(rt2) {'took': 23, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 0.2876821, 'hits': [{'_index': 'megacorp', '_type': 'employee', '_id': '2', '_score': 0.2876821, '_source': {'first_name': 'Jane', 'last_name': 'Smith', 'age': 32, 'about': 'I like to collect rock albums', 'interests': ['music']}}]}}

總結

以上是生活随笔為你收集整理的python elasticsearch 入门教程(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。