Elasticsearches之python使用及Django與Flask集成示例
更新時間:2022年04月19日 14:33:06 作者:Jeff的技術(shù)棧
這篇文章主要為大家介紹了Elasticsearches之python使用及Django與Flask集成示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
Elasticsearch之Python使用
from elasticsearch import Elasticsearch
obj = Elasticsearch()
# 創(chuàng)建索引(Index)
result = obj.indices.create(index='user', body={"userid":'1','username':'lqz'},ignore=400)
# print(result)
# 刪除索引
# result = obj.indices.delete(index='user', ignore=[400, 404])
# 插入數(shù)據(jù)
# data = {'userid': '1', 'username': 'lqz','password':'123'}
# result = obj.create(index='news', doc_type='politics', id=1, body=data)
# print(result)
# 更新數(shù)據(jù)
'''
不用doc包裹會報(bào)錯
ActionRequestValidationException[Validation Failed: 1: script or doc is missing
'''
# data ={'doc':{'userid': '1', 'username': 'lqz','password':'123ee','test':'test'}}
# result = obj.update(index='news', doc_type='politics', body=data, id=1)
# print(result)
# 刪除數(shù)據(jù)
# result = obj.delete(index='news', doc_type='politics', id=1)
# 查詢
# 查找所有文檔
query = {'query': {'match_all': {}}}
# 查找名字叫做jack的所有文檔
# query = {'query': {'term': {'username': 'lqz'}}}
# 查找年齡大于11的所有文檔
# query = {'query': {'range': {'age': {'gt': 11}}}}
allDoc = obj.search(index='news', doc_type='politics', body=query)
print(allDoc['hits']['hits'][0]['_source'])Elasticsearch之Django/Flask集成
elasticsearch-dsl
#安裝: pip3 install elasticsearch-dsl
#示例
from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean, \
analyzer, InnerDoc, Completion, Keyword, Text
html_strip = analyzer('html_strip',
tokenizer="standard",
filter=["standard", "lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
class Comment(InnerDoc):
author = Text(fields={'raw': Keyword()})
content = Text(analyzer='snowball')
created_at = Date()
def age(self):
return datetime.now() - self.created_at
class Post(Document):
title = Text()
title_suggest = Completion()
created_at = Date()
published = Boolean()
category = Text(
analyzer=html_strip,
fields={'raw': Keyword()}
)
comments = Nested(Comment)
class Index:
name = 'blog'
def add_comment(self, author, content):
self.comments.append(
Comment(author=author, content=content, created_at=datetime.now()))
def save(self, ** kwargs):
self.created_at = datetime.now()
return super().save(** kwargs)
django集成
from datetime import datetime
from elasticsearch_dsl import Document, Date, Nested, Boolean,analyzer, InnerDoc, Completion, Keyword, Text,Integer
from elasticsearch_dsl.connections import connections
connections.create_connection(hosts=["localhost"])
class Article(Document):
title = Text(analyzer='ik_max_word', search_analyzer="ik_max_word", fields={'title': Keyword()})
author = Text()
class Index:
name = 'myindex'
def save(self, ** kwargs):
return super(Article, self).save(** kwargs)
if __name__ == '__main__':
# Article.init() # 創(chuàng)建映射
# 保存數(shù)據(jù)
# article = Article()
# article.title = "測試測試"
# article.save() # 數(shù)據(jù)就保存了
#查詢數(shù)據(jù)
# s=Article.search()
# s = s.filter('match', title="測試")
# results = s.execute()
# print(results)
#刪除數(shù)據(jù)
# s = Article.search()
# s = s.filter('match', title="測試").delete()
#修改數(shù)據(jù)
# s = Article().search()
# s = s.filter('match', title="測試")
# results = s.execute()
# print(results[0])
# results[0].title="xxx"
# results[0].save()以上就是Elasticsearches之python使用及Django與Flask集成示例的詳細(xì)內(nèi)容,更多關(guān)于python Elasticsearches之Django與Flask集成的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺析Python的web.py框架中url的設(shè)定方法
web.py是Python的一個輕量級Web開發(fā)框架,這里我們來淺析Python的web.py框架中url的設(shè)定方法,需要的朋友可以參考下2016-07-07
Python3中在Anaconda環(huán)境下安裝basemap包
今天小編就為大家分享一篇關(guān)于Python3中在Anaconda環(huán)境下安裝basemap包的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Python將DataFrame的某一列作為index的方法
下面小編就為大家分享一篇Python將DataFrame的某一列作為index的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
解決Python中導(dǎo)入自己寫的類,被劃紅線,但不影響執(zhí)行的問題
這篇文章主要介紹了解決Python中導(dǎo)入自己寫的類,被劃紅線,但不影響執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例
這篇文章主要介紹了tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

