ElasticSearch Client使用常见问题总结

406报错

elasticsearch.ApiError: ApiError(406, 'Content-Type header application/vnd.elasticsearch+x-ndjson; compatible-with=8 is not supported', 'Content-Type header application/vnd.elasticsearch+x-ndjson; compatible-with=8 is not supported')

原因

Elasticsearch client(python Elasticsearch)版本号与server版本号不一致。

解决方案

查看server端Elasticsearch版本,可以使用以下几种方法之一:

  1. 通过API请求:使用curl或类似工具向Elasticsearch的根端点发送GET请求。例如:

    sh 复制代码
    curl -XGET http://your-elasticsearch-host:9200/

    在返回的JSON响应中,您将看到版本号信息。

  2. 通过Kibana Dev Tools:如果您使用Kibana,可以在Kibana的Dev Tools中执行相同的API请求,以查看版本号。

  3. 通过Elasticsearch的安装目录 :在Elasticsearch的安装目录中,可以找到版本信息的文件。例如,在Linux系统上,版本信息通常位于/usr/share/elasticsearch/目录下的README.txt文件中。

  4. 通过日志文件:启动Elasticsearch时,日志文件通常会记录版本信息。您可以查看Elasticsearch的日志文件以获取版本信息。

本地代码中安装与server端版本相同的client 例如: 假设Elasticsearch server版本为 7.10.0 , 则本地安装方法如下:

bash 复制代码
pip install elasticsearch==7.10.0

401报错

AuthenticationException(401, 'security_exception', 'missing authentication credentials for REST request /_bulk')

原因

es client不同版本认证使用的参数名是不一样的,针对不同的版本需进行不同的设置

解决方案

  1. 对于elasticsearch 7.x及更高版本:

    • 使用http_auth参数设置HTTP身份验证。您可以将用户名和密码作为元组传递给http_auth参数。

    • 示例:

      python 复制代码
      from elasticsearch import Elasticsearch
      
      es = Elasticsearch(['localhost'], port=9200, http_auth=('username', 'password'))
  2. 对于elasticsearch 6.x版本:

    • 使用basic_auth参数设置基本身份验证。您可以将用户名和密码作为元组传递给basic_auth参数。

    • 示例:

      python 复制代码
      from elasticsearch import Elasticsearch
      
      es = Elasticsearch(['localhost'], port=9200, basic_auth=('username', 'password'))

404报错

'status': 404, 'error': {'type': 'index_not_found_exception', 'reason': 'no such index index_sample and action.auto_create_index contains -\* which forbids automatic creation of the index',

原因

对应的index不存在,且server端禁用了auto create index。

解决方案

  1. 手动创建索引:在执行批量导入之前,手动创建index_sample索引。您可以使用以下代码创建索引:

    python 复制代码
    from elasticsearch import Elasticsearch
    
    # 连接到本地Elasticsearch实例
    es = Elasticsearch(['localhost'], port=9200)
    
    # 创建索引
    index_name = "index_sample"
    body = {
        "mappings": {
            "properties": {
                "name": {"type": "text"},
                "age": {"type": "integer"},
                "city": {"type": "text"}
            }
        }
    }
    es.indices.create(index=index_name, body=body)
  2. 启用自动创建索引功能:在Elasticsearch配置中将action.auto_create_index设置为*,以允许自动创建索引。请注意,这可能会带来安全风险,因为任何尝试写入不存在的索引的操作都会自动创建索引。要启用自动创建索引功能,您可以在elasticsearch.yml配置文件中添加以下配置:

    arduino 复制代码
    action.auto_create_index: "*"
相关推荐
代码山河1 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
学长毕设1 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
微订校园外卖平台2 小时前
校园外卖平台选模板还是定制:架构、数据流与验收清单
大数据·架构·外卖系统·本地生活·微订
努力的小雨3 小时前
给 AI 眼镜开发应用有多难?我从零做了一个陪练,还上架了
后端
ZhengEnCi4 小时前
用 AI 写代码为什么拼到最后还是拼人:算力瓶颈与领域专家提示词的本质差异
算法·架构
Eloudy4 小时前
Qwen2.5-14B-Instruct-AWQ 模型文件结构分析
大数据·elasticsearch·搜索引擎
Decay__5 小时前
04_MySQL DBA 日常运维手册 —— 会话管理、锁与阻塞、事务监控、慢查询与日志巡检
运维·数据库·sql·mysql·架构
excel5 小时前
双 Token 与前端使用 Crypto 公私钥鉴权换 Token,哪种方案更好?
前端·后端
AI入职记5 小时前
AI 代码审查召回率实测:20 个骗过单元测试的变异体,抓到 13 个
后端
浮链序5 小时前
别让 AI 客服把用户当猴耍:百来行 Python 把「转人工」做成一条高可用链路
后端