zoukankan      html  css  js  c++  java
  • python对于新版本elasticsearch-dsl(7.1)的使用说明

    一.旧版elasticsearch-dsl(5.1)对应elasticsearch5.1.1的版本

    很多同学在python搜索引擎视频中关于看到的第十章elasticsearch使用中使用python创建mapping老师使用的以下代码,这些代码对于最新版的elasticsearch-dsl的引用已经失效,会报异常错误

    from datetime import datetime
    from elasticsearch_dsl import Document, Date, Nested, Boolean, 
        analyzer, InnerDoc, Completion, Keyword, Text,Integer
    
    from elasticsearch_dsl.analysis import CustomAnalyzer as _CustomAnalyzer
    
    from elasticsearch_dsl.connections import connections
    connections.create_connection(hosts=["localhost"])
    
    # class CustomAnalyzer(_CustomAnalyzer):
    #     def get_analysis_definition(self):
    #         return {}
    
    
    # ik_analyzer = CustomAnalyzer("ik_max_word", filter=["lowercase"])
    class ArticleType(Document):
        #伯乐在线文章类型
        # suggest = Completion(analyzer=ik_analyzer)
        title = Text(analyzer="ik_max_word")
        create_date = Date()
        url = Keyword()
        url_object_id = Keyword()
        front_image_url = Keyword()
        front_image_path = Keyword()
        praise_nums = Integer()
        comment_nums = Integer()
        fav_nums = Integer()
        tags = Text(analyzer="ik_max_word")
        content = Text(analyzer="ik_max_word")
    
        class Meta:
            index = "jobbole"
            doc_type = "article"
    
    if __name__ == "__main__":
        ArticleType.init()

    二.新版的引用更正以及代码

    1.最新版elasticsearch-dsl下载地址:
    es-dsl对应的github地址
    2.最新版构建jobbole的mapping代码

    # -*- coding: utf-8 -*-
    __author__ = 'yh'
    from datetime import datetime
    from elasticsearch_dsl import Document, Date, Integer, Keyword, Text, connections
    
    # Define a default Elasticsearch client
    connections.create_connection(hosts=['localhost'])
    
    class ArticleType(Document):
        #伯乐在线文章类型
        # suggest = Completion(analyzer=ik_analyzer)
        title = Text(analyzer="ik_max_word")
        create_date = Date()
        url = Keyword()
        url_object_id = Keyword()
        front_image_url = Keyword()
        front_image_path = Keyword()
        praise_nums = Integer()
        comment_nums = Integer()
        fav_nums = Integer()
        tags = Text(analyzer="ik_max_word")
        content = Text(analyzer="ik_max_word")
    
        class Index:
            name = 'jobbole'
            settings = {
              "number_of_shards": 5,
            }
    # create the mappings in elasticsearch
    if __name__ == "__main__":
        ArticleType.init()

    前方高能

    关于接下来的elasticsearch-dsl使用说明

    新版elasticsearch-dsl上边是这样写

    from ArticleSpider.models.es_types import ArticleType
    from elasticsearch_dsl.connections import connections
    # 与ElasticSearch进行连接,生成搜索建议
    es = connections.create_connection(ArticleType)

    新版elasticsearch-dsl下边是这样写

    def gen_suggests(index,info_tuple):
        #根据字符串生成搜索建议数组
        used_words = set()
        suggests = []
        for text, weight in info_tuple:
            if text:
                #调用es的analyze接口分析字符串
                words = es.indices.analyze(index="jobbole",
                                           body={"analyzer": "ik_max_word", "text": "{0}".format(text)})
                anylyzed_words = set([r["token"] for r in words["tokens"] if len(r["token"])>1])
                new_words = anylyzed_words - used_words
            else:
                new_words = set()
    
            if new_words:
                suggests.append({"input":list(new_words), "weight":weight})
    
        return suggests

    然后调用这样写

     article.suggest = gen_suggests(ArticleType, ((article.title, 10), (article.tags, 7)))
    
                article.save()
  • 相关阅读:
    基于TMS320C6678、FPGA XC5VLX110T的6U CPCI 8路光纤信号处理卡
    KC705E 增强版 基于FMC接口的Xilinx Kintex7 FPGA K7 XC7K325T PCIeX8 接口卡
    基于Xilinx Kintex7 FPGA K7 XC7K325T PCIeX8 四路光纤卡
    IOS常用API
    数学和物理的公式
    [cocos2d]如何实现模态对话框
    使用加速器实现的定位算法
    cocos2d 粒子设计器
    陀螺仪操作摄相机,可以旋转,但角度(轴)还不对
    gluLookAt 的参数
  • 原文地址:https://www.cnblogs.com/yoyowin/p/12208365.html
Copyright © 2011-2022 走看看