You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows虚拟环境中Haystack导入Elasticsearch失败求助

Haystack框架Elasticsearch导入失败求助

我正在学习Haystack框架,尝试开发用例,但在初始导入环节就遭遇错误。已尝试多种方法,甚至下载了elasticsearch.zip文件,仍无法解决。

运行的代码

第一单元格代码

import os
from haystack.document_stores import ElasticsearchDocumentStore
from haystack.utils import clean_wiki_text
from haystack.nodes import PreProcessor
import elasticsearch

from haystack.utils import launch_es
launch_es()

第二单元格代码

host=os.environ.get("ELASTICSEARCH_HOST","localhost")
document_store = ElasticsearchDocumentStore(host='localhost', username="", password="", index="document")

报错信息

运行第二单元格时出现如下错误:

ModuleNotFoundError                       Traceback (most recent call last)
File ~\Anaconda3\envs\learn_haystack\Lib\site-packages\haystack\document_stores\elasticsearch\__init__.py:3
      1 try:
      2     # Use appropriate ElasticsearchDocumentStore depending on ES client version
----> 3     from elasticsearch import VERSION
      5     if VERSION[0] == 8:

ModuleNotFoundError: No module named 'elasticsearch'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
File ~\Anaconda3\envs\learn_haystack\Lib\site-packages\haystack\document_stores\elasticsearch\es7.py:18
     17 with LazyImport(failed_import_message) as es_import:
---> 18     from elasticsearch import Connection, Elasticsearch, RequestsHttpConnection, Urllib3HttpConnection
     19     from elasticsearch.helpers import bulk, scan

ModuleNotFoundError: No module named 'elasticsearch'

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
Cell In[69], line 7
      2 print(host)
      4 # document_store = ElasticsearchDocumentStore(host=host, username="", password="", index="document")
----> 7 document_store = ElasticsearchDocumentStore(host="localhost",port=9200, username="",password="",index="document",create_index=True,similarity="dot_product")

File ~\Anaconda3\envs\learn_haystack\Lib\site-packages\haystack\nodes\base.py:46, in exportable_to_yaml.<locals>.wrapper_exportable_to_yaml(self, *args, **kwargs)
     43         self._component_config["params"][k] = v
     45 # Call the actuall __init__ function with all the arguments
---> 46 init_func(self, *args, **kwargs)

File ~\Anaconda3\envs\learn_haystack\Lib\site-packages\haystack\document_stores\elasticsearch\es7.py:137, in ElasticsearchDocumentStore.__init__(self, host, port, username, password, api_key_id, api_key, aws4auth, index, label_index, search_fields, content_field, name_field, embedding_field, embedding_dim, custom_mapping, excluded_meta_data, analyzer, scheme, ca_certs, verify_certs, recreate_index, create_index, refresh_type, similarity, timeout, return_embedding, duplicate_documents, scroll, skip_missing_embeddings, synonyms, synonym_type, use_system_proxy, batch_size)
     62 """
     63 A DocumentStore using Elasticsearch to store and query the documents for our search.
     64 
   (...)
    134 
    135 """
    136 # Ensure all the required inputs were successful
---> 137 es_import.check()
    138 # Let the base class trap the right exception from the specific client
    139 self._RequestError = RequestError

File ~\Anaconda3\envs\learn_haystack\Lib\site-packages\lazy_imports\try_import.py:107, in _DeferredImportExceptionContextManager.check(self)
    105 if self._deferred is not None:
    106     exc_value, message = self._deferred
---> 107     raise ImportError(message) from exc_value

ImportError: Failed to import 'elasticsearch'. Run 'pip install farm-haystack[elasticsearch]'.
Note that this installs the Elasticsearch 7 client and requires a running Elasticsearch 7 instance. Starting from Haystack version 1.21 , the 'elasticsearch' extra will install the Elasticsearch 8 client and will require a running Elasticsearch 8 instance. To continue using Elasticsearch 7 with Haystack 1.21 and beyond, install the 'elasticsearch7' extra.
If you want to already start using Elasticsearch 8, install the 'elasticsearch8' extra. Original error: No module named 'elasticsearch'

已尝试的解决方法

  • 运行elasticsearch.bat启动本地Elasticsearch服务(版本7.17.12)
  • 在Windows虚拟环境中安装了farm-haystack[all]和farm-haystack[elasticsearch]

内容的提问来源于stack exchange,提问作者Teja Sruthi Varanasi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 14:15:58