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

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)
# 查詢(xún)
# 查找所有文檔
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ù)就保存了
    #查詢(xún)數(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)文章

  • python關(guān)于圖片和base64互轉(zhuǎn)的三種方式

    python關(guān)于圖片和base64互轉(zhuǎn)的三種方式

    無(wú)論使用cv2、PIL還是直接讀取圖片的方法進(jìn)行圖片與Base64的轉(zhuǎn)換,核心步驟都涉及到二進(jìn)制格式的轉(zhuǎn)換,每種方法的基本過(guò)程都是:Base64轉(zhuǎn)二進(jìn)制,然后二進(jìn)制轉(zhuǎn)圖片,或反向操作,這些方法均基于二進(jìn)制與圖片轉(zhuǎn)換的基本原理
    2024-09-09
  • 重溫Python基礎(chǔ)之列表操作

    重溫Python基礎(chǔ)之列表操作

    這篇文章主要帶大家來(lái)復(fù)習(xí)一下Python基礎(chǔ)中的列表操作,不知道各位還記得多少呢?文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-11-11
  • OpenCV圖像卷積之cv.filter2D()函數(shù)詳解

    OpenCV圖像卷積之cv.filter2D()函數(shù)詳解

    在其官方文檔中,filter2D()函數(shù)在掩模板介紹中一筆帶過(guò),我認(rèn)為該函數(shù)應(yīng)該進(jìn)行詳細(xì)介紹,下面這篇文章主要給大家介紹了關(guān)于OpenCV圖像卷積之cv.filter2D()函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • python基礎(chǔ)之迭代器與生成器

    python基礎(chǔ)之迭代器與生成器

    這篇文章主要為大家介紹了python迭代器與生成器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • pyCharm 實(shí)現(xiàn)關(guān)閉代碼檢查

    pyCharm 實(shí)現(xiàn)關(guān)閉代碼檢查

    這篇文章主要介紹了pyCharm 實(shí)現(xiàn)關(guān)閉代碼檢查,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • MySQL適配器PyMySQL詳解

    MySQL適配器PyMySQL詳解

    這篇文章主要為大家詳細(xì)介紹了MySQL適配器PyMySQL的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Python使用MapReduce編程模型統(tǒng)計(jì)銷(xiāo)量

    Python使用MapReduce編程模型統(tǒng)計(jì)銷(xiāo)量

    MapReduce是面向大數(shù)據(jù)并行處理的計(jì)算模型、框架和平臺(tái),是一種計(jì)算引擎,可以把我們對(duì)大批量數(shù)據(jù)的計(jì)算通過(guò)抽象成map與reduce兩個(gè)子任務(wù)進(jìn)行計(jì)算從而更快的得到想要的結(jié)果
    2022-04-04
  • python 如何使用find和find_all爬蟲(chóng)、找文本的實(shí)現(xiàn)

    python 如何使用find和find_all爬蟲(chóng)、找文本的實(shí)現(xiàn)

    這篇文章主要介紹了python 如何使用find和find_all,爬蟲(chóng)、找文本,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • numpy.random.choice()函數(shù)詳解

    numpy.random.choice()函數(shù)詳解

    處理數(shù)據(jù)時(shí)我們經(jīng)常需要從數(shù)組中隨機(jī)抽取元素,這時(shí)候我們可以考慮使用np.random.choice()函數(shù),這篇文章主要介紹了numpy.random.choice()函數(shù),需要的朋友可以參考下
    2023-05-05
  • Python imread、newaxis用法詳解

    Python imread、newaxis用法詳解

    這篇文章主要介紹了python imread、newaxis用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評(píng)論

类乌齐县| 聊城市| 富裕县| 大安市| 太谷县| 黎城县| 定西市| 太原市| 吴忠市| 衡水市| 通海县| 苏尼特左旗| 洛宁县| 东乌珠穆沁旗| 游戏| 辉南县| 西青区| 富裕县| 沂源县| 朝阳区| 中宁县| 河北省| 合江县| 广丰县| 广河县| 社会| 张北县| 铅山县| 遂平县| 孟州市| 皮山县| 襄汾县| 灵宝市| 谷城县| 东乌| 如东县| 固安县| 油尖旺区| 礼泉县| 德清县| 邛崃市|