es更新说明(dsl)

摘要:
I在视频的第10章中,许多学习弹性搜索dsl旧版本的学生使用python创建映射,该视频介绍了在python搜索引擎中使用弹性搜索。教师使用的以下代码未能引用elasticsearch dsl,并将报告datetimeimportdatetimefromselasticsearch_dslimportDocument,Date,Nested中的异常错误

一.旧版elasticsearch-dsl
很多同学在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()

免责声明:文章转载自《es更新说明(dsl)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Effective C++学习笔记(一)oracle sql日期比较:下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ELK之从6.3.1升级至6.6.2

  需要把原6.3.1版本升级为6.6.2版本   1,官网下载rpm包   2,升级elasticsearch和kibana rpm -U elasticsearch-6.6.2.rpm rpm -U kibana-6.6.2-x86_64.rpm rpm -U logstash-6.6.2.rpm      配置还是使用原配置,新版本配置会在配...

Elasticsearch之深入理解

@ 目录 ES应用场景 ES应用场景举例 应用场景思考 ES和其他数据库对比 ES架构 Gateway Lucene 数据处理 发现机制与脚本 Transport REST full API ES中集群、节点、索引、分片、段等概念 集群 节点 索引 分片 段(segment) 文档(document) 字段(field) 词项(term)...

Elasticsearch _reindex Alias使用

应用背景:1、当你的数据量过大,而你的索引最初创建的分片数量不足,导致数据入库较慢的情况,此时需要扩大分片的数量,此时可以尝试使用Reindex。 2、当数据的mapping需要修改,但是大量的数据已经导入到索引中了,重新导入数据到新的索引太耗时;但是在ES中,一个字段的mapping在定义并且导入数据之后是不能再修改的, 所以这种情况下也可以考虑尝试使用...

Elasticsearch增删改查 之 —— Update更新

更新操作,一般用这个的,应该不会很多吧!ES本身还是一个倾向于查询检索的框架,对于这种更新的操作,太过频繁总归是不好的。不过阅读本篇后,你可以使用Script对所有的文档执行更新操作,也可以使用doc对部分文档执行更新,也可以使用upsert对不存在的文档执行添加操作。 更新 Update更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更...

ES笔记一:周边工具

ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎。它用Java 编写的,它的内部使用 Lucene做索引与搜索,目的是使全文检索变得简单,设计用于云计算中,能够达到实时搜索,稳定,可靠,使用简单方便(使用JSON进行数据索引,通过隐藏 Lucene 的复杂性,取而代之的提供一套简单一致的 RESTful API)。...

Java API操作ES

Java API操作ES Elastic Search软件是由Java语言开发的,所以也可以通过Java API的方法对Elastic Search服务进行访问。 1. 引入POM文件 <properties> <elasticsearch.version>7.6.2</elasticsearch.ve...