es通过python生成索引出错:You cannot perform API calls on the default index.

来源:14-13 scrapy写入数据到elasticsearch中 - 2

Xiaoyu_Wu

2019-06-01

老师,您好!
我运行python的es_type.py,为了生成elasticsearch的索引,但报错了,代码如下:

# -*- coding: utf-8 -*-

from elasticsearch_dsl import DocType, Date, Nested, Boolean, \
    analyzer, 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(DocType):
    suggest = Completion(analyzer=ik_analyzer)
    title = Text(analyzer="ik_max_word")
    time = Date()
    domain = Text(analyzer="ik_max_word")
    article_url = Keyword()

    class Meta:
        index = "AJobbole"
        doc_type = "article"


if __name__ == "__main__":
    ArticleType.init()

出错信息如下:

Traceback (most recent call last):
  File "E:/homework/python/ScrapySpider/spider/spider/models/es_types.py", line 33, in <module>
    ArticleType.init()
  File "E:\Envs\py3\lib\site-packages\elasticsearch_dsl\document.py", line 136, in init
    i.save(using=using)
  File "E:\Envs\py3\lib\site-packages\elasticsearch_dsl\index.py", line 269, in save
    if not self.exists(using=using):
  File "E:\Envs\py3\lib\site-packages\elasticsearch_dsl\index.py", line 380, in exists
    return self._get_connection(using).indices.exists(index=self._name, **kwargs)
  File "E:\Envs\py3\lib\site-packages\elasticsearch_dsl\index.py", line 108, in _get_connection
    "You cannot perform API calls on the default index.")
ValueError: You cannot perform API calls on the default index.

Process finished with exit code 1

ValueError: You cannot perform API calls on the default index.

是哪里错了呢?这段代码我基本是参照您视频教程里打出来的。
请老师提示,谢谢~

写回答

3回答

归根结底不优秀

2019-06-03

这个问题是因为我们当初pip install elasticsearch-dsl没有指定版本,你可以pip list查看,会发现elasticsearch和elasticsearch-dsl是7.0+的版本,老师安装的是5.1的版本

解决办法:pip uninstall elasticsearch和elasticsearch-dsl

然后pip install elasticsearch-dsl=5.1就行了


0
4
归根结底不优秀
回复
Xiaoyu_Wu
这bug也是老师给解决的,换个问题我就原形毕露了
2019-06-05
共4条回复

归根结底不优秀

2019-06-03

兄弟,好像是es版本的问题,导致这种方法失效了

0
3
归根结底不优秀
回复
Xiaoyu_Wu
老师说的5.1.1版本
2019-06-03
共3条回复

归根结底不优秀

2019-06-02

同问+1

0
0

Scrapy打造搜索引擎 畅销4年的Python分布式爬虫课

带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎

5796 学习 · 6290 问题

查看课程