欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Elasticsearches之python使用及Django與Flask集成示例

 更新時(shí)間: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包裹會(huì)報(bào)錯(cuò)
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 = "測(cè)試測(cè)試"
    # article.save()  # 數(shù)據(jù)就保存了
    #查詢數(shù)據(jù)
    # s=Article.search()
    # s = s.filter('match', title="測(cè)試")
    # results = s.execute()
    # print(results)
    #刪除數(shù)據(jù)
    # s = Article.search()
    # s = s.filter('match', title="測(cè)試").delete()
    #修改數(shù)據(jù)
    # s = Article().search()
    # s = s.filter('match', title="測(cè)試")
    # 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集成的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • django中SMTP發(fā)送郵件配置詳解

    django中SMTP發(fā)送郵件配置詳解

    這篇文章主要介紹了django中SMTP發(fā)送郵件配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 淺析Python的web.py框架中url的設(shè)定方法

    淺析Python的web.py框架中url的設(shè)定方法

    web.py是Python的一個(gè)輕量級(jí)Web開發(fā)框架,這里我們來(lái)淺析Python的web.py框架中url的設(shè)定方法,需要的朋友可以參考下
    2016-07-07
  • Python3中在Anaconda環(huán)境下安裝basemap包

    Python3中在Anaconda環(huán)境下安裝basemap包

    今天小編就為大家分享一篇關(guān)于Python3中在Anaconda環(huán)境下安裝basemap包的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Python將DataFrame的某一列作為index的方法

    Python將DataFrame的某一列作為index的方法

    下面小編就為大家分享一篇Python將DataFrame的某一列作為index的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 解決Python中導(dǎo)入自己寫的類,被劃紅線,但不影響執(zhí)行的問題

    解決Python中導(dǎo)入自己寫的類,被劃紅線,但不影響執(zhí)行的問題

    這篇文章主要介紹了解決Python中導(dǎo)入自己寫的類,被劃紅線,但不影響執(zhí)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例

    tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例

    這篇文章主要介紹了tensorflow 20:搭網(wǎng)絡(luò),導(dǎo)出模型,運(yùn)行模型的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • 如何使用Python?VTK高亮顯示actor

    如何使用Python?VTK高亮顯示actor

    這篇文章主要介紹了如何使用Python?VTK高亮顯示actor,通過(guò)Python-VTK在同一個(gè)窗口中,高亮顯示選中的actor。本例子中的代碼,當(dāng)窗口中的圓球actor被選中時(shí),會(huì)變成紅色,并且會(huì)顯示actor三遍面片邊緣信息,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • python自動(dòng)重試第三方包retrying模塊的方法

    python自動(dòng)重試第三方包retrying模塊的方法

    retrying是一個(gè)python的重試包,可以用來(lái)自動(dòng)重試一些可能運(yùn)行失敗的程序段。這篇文章主要介紹了python自動(dòng)重試第三方包retrying的方法,需要的朋友參考下吧
    2018-04-04
  • Python處理鍵映射值操作詳解

    Python處理鍵映射值操作詳解

    這篇文章主要為大家詳細(xì)介紹了Python中的處理鍵映射值操作的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2022-11-11
  • matplotlib quiver箭圖繪制案例

    matplotlib quiver箭圖繪制案例

    這篇文章主要介紹了matplotlib quiver箭圖繪制案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04

最新評(píng)論