es更新说明(dsl)
2024-08-26 11:21:02
一.旧版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()
最新文章
- 3.C#WinForm基础累加器
- 深入浅出node(1) Node简介
- C#Lite Unity热更新开源解决方案改名C#Light
- HDU 4768 Flyer(二分)
- 148. Sort List -- 时间复杂度O(n log n)
- MemoryMappingFile泄漏分析过程
- Codeforces Round #197 (Div. 2) : B
- linux获取CPU温度
- XtraReport改变纸张方向
- 「工具」Aquarelo - 来自意大利的色阶管理工具
- Charles手机抓包常见问题(各种常见坑)
- 完成了Coursera的一个机器学习课程
- mysql 查询优化 ~ 多表查询基础知识
- rabbitmq更换数据文件和日志文件的存放位置
- BBS(第一天)项目之 注册功能实现通过forms验证与 前端ajax请求触发查询数据库判断用户是否存在的功能实现
- Vue + Element UI 实现权限管理系统 前端篇(三):工具模块封装
- SQL Server Management Studio记住的密码丢失的问题
- MFC创建好的对话框如何移植到新程序中
- HIBERNATE知识复习记录3-关联关系
- 转:CMake安装和使用
热门文章
- 【Spring AOP】切入点表达式(四)
- Netty线程模型(五)
- [BZOJ1040][CODEVS1423][ZJOI2008]骑士
- Educational Codeforces Round 70 题解
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
- 代码移植的福音 namespace_alias
- PHP获取cookie、Token、模拟登录、抓取数据、解析生成json
- python运维开发常用模块(二)IPy
- SpringCloud Ribbon 负载均衡 通过服务器名无法连接的神坑一个
- 是时候解决 students's Test 假设检验(显著性检验)了