老师,您好!
我运行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.
是哪里错了呢?这段代码我基本是参照您视频教程里打出来的。
请老师提示,谢谢~
登录后可查看更多问答,登录/注册
带你彻底掌握Scrapy,用Django+Elasticsearch搭建搜索引擎
了解课程