在学习python搜索引擎中,python对于elasticsearch-dsl的创建mapping报错:elasticsearch.exceptions.RequestError: RequestError(400, ‘mapper_parsing_exception’, 'Root mapping definition has unsupported parameters: [praise_nums : {type=integer}] …
原因是:安装的elasticsearch-rtf是5.x.x的版本,默认pip install elasticsearch-dsl安装的则是7.x.x的版本。
解决办法:统一版本就可。操作如下:
-
卸载现在安装的新版elasticsearch-dsl
-
安装5.x.x版本的elasticsearch-rtf
-
python文件的代码
# -*- coding: utf-8 -*-
'''
@FileName: es_type.py
@Software: PyCharm
'''
from datetime import datetime
from elasticsearch_dsl import DocType, Date, Integer, Keyword, Text, analyzer
from elasticsearch_dsl.connections import connections
connections.create_connection(hosts=["localhost"])
class ArticleType(DocType):
title = Text(analyzer="ik_max_word")
create_date = Date()
url = Keyword()
url_object_id = Keyword()
front_img_url = Keyword()
front_image_path = Keyword()
praise_nums = Integer()
fav_nums = Integer()
read_nums = Integer()
content = Text(analyzer="ik_max_word")
class Meta:
index = "jobbole"
doc_type = "article"
"""
# 这个写法是elasticsearch-dsl 7.1.0的写法
class Index:
name = 'jobbole'
doc_type = "article"
settings = {
"number_of_shards": 6
}
def save(self, **kwargs):
self.lines = len(self.body.split())
return super(ArticleType, self).save(**kwargs)
def is_published(self):
return datetime.now() > self.published_from
"""
if __name__ == "__main__":
ArticleType.init()
卡了一下午的错误,反复对照了官方文档链接: elasticsearch-dsl.readthedocs,没有问题,网上也没找到相关经验,最后才发现是版本问题。
望可以帮到像我一样的小白,免受折磨!