一.旧版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)的更多相关文章

  1. [Elasticsearch] ES更新问题踩坑记录

    问题描述 我们有个系统设计的时候针对Hive创建表.删除表, 需要更新ES中的一个状态,标记是否删除,在几乎同时执行两条下面的语句的时候,发现在ES 中出现表即使被创建了还是无法被查询到的情况,针对该 ...

  2. ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解

    前言 在上一篇中介绍了ElasticSearch集群和kinaba的安装教程,本篇文章就来讲解下 ElasticSearch的DSL语句使用. ElasticSearch DSL 介绍 Elastic ...

  3. es操作手册

    0 _search查询数据时可以指定多个index和type GET /index1,index2/type1,type2/_search GET /_all/type1/_search 相当于查询全 ...

  4. solr和ElasticSearch(ES)的区别?

    Solr2004年诞生 ElasticSearch 2010年诞生 ES更新 ElasticSearch简介: ElasticSearch是一个实时的分布式的搜索引擎和分析引擎.它可以帮助你用前所未有 ...

  5. ES与CQRS之旅

    引言 领域驱动设计(Domain Driven Design),使用统一的建模语言.专注业务领域分析.采取化整为零并反复迭代的方式,以业务领域模型为圆心,向外辐射到系统轮廓的勾勒.具体模块的实现,为我 ...

  6. Es 学习笔记 (1)

    目录 前言 什么是es? es数据组织类比 应用场景 核心概念 集群 节点(node) 索引(Index) 文档类型(Type) 文档(Document) Mapping 核心简单域类型 分片(sha ...

  7. Elasticsearch的CRUD:REST与Java API

    CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...

  8. Elasticsearch 检索

    说到查询,那么索引也是一个绕不开的话题,可以说,没有索引就没有检索,先来看一个示意图 左边是索引过程,右边是检索过程.关键的步骤是分词过程,我用等号表示这两个过程一样,而且,必须一样,这个等号并不是模 ...

  9. python | Elasticsearch-dsl常用方法总结(join为案例)

    Elasticsearch DSL是一个高级库,其目的是帮助编写和运行针对Elasticsearch的查询.它建立在官方低级客户端(elasticsearch-py)之上. 它提供了一种更方便和习惯的 ...

随机推荐

  1. 每天一道Rust-LeetCode(2019-06-14)

    每天一道Rust-LeetCode(2019-06-14) 常数时间插入.删除和获取随机元素 坚持每天一道题,刷题学习Rust. 题目描述 https://leetcode-cn.com/proble ...

  2. bdd框架之lettuce

    安装 执行 :lettuce  (需要在特定的文件夹下) 结果指定到文件中

  3. Mysql对表中 数据 查询的操作 DQL

    准备数据,倒入sql文件 运行sql文件 得到四张表 select * from 表名  * 代表全部 1.AS子句作为别名 select studentname as "姓名" ...

  4. appium--元素等待和屏幕截图

    元素等待 设置元素等待可以更加灵活的指定等待元素的时间,从而增强脚本的健壮性,提高执行效率 强制等待 from time import sleep sleep(5) 隐式等待 隐式等待是针对全部元素设 ...

  5. Educational Codeforces Round 61 (Rated for Div. 2) E 多重背包优化

    https://codeforces.com/contest/1132/problem/E 题意 有8种物品,重量是1~8,每种数量是\(cnt[i]\)(1e16),问容量为W(1e18)的背包最多 ...

  6. Ubuntu16LTS使用笔记(Server版)

    环境测试:Ubuntu Server 16.04.06 x64 虚拟机 以下均需要root权限,请sudo 或 su root 解决只有先启动Windows再重启到Linux才能使用usb网卡的问题: ...

  7. Windows10 WSL下 龙芯3A 交叉编译环境搭建

    记个流水账避免事后忘记怎么搭建的 - - 环境 Key Value 系统 Windows10 WSL系统 Ubuntu 下载工具链 http://www.loongnix.org/index.php/ ...

  8. BAT公司职级体系及薪水解密

    BAT公司职级体系及薪水解密 互联网圈有这么一句话:百度的技术,阿里的运营,腾讯的产品.那么代表互联网三座大山的BAT,内部人才体系有什么区别呢? 先谈谈腾讯的体系. 首先是腾讯. 1.职级: 腾讯职 ...

  9. C# HTTP系列11 以普通文件流方式上传文件远程服务器

    系列目录     [已更新最新开发文章,点击查看详细] 应用程序中上传附件是最常使用的操作之一,ASP.NET客户端一般通过上传控件实现, <input type="file" ...

  10. 文件上传之靶场upload-labs (11-20)

    第十一关 strrpos() 函数查找字符串在另一字符串中最后一次出现的位置 substr() 函数返回字符串的一部分 文件保存的方式是上传路径+随机时间+截取的文件后缀 其中上传路径可控,可以利用这 ...