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: "*"
相关推荐
Java技术小馆25 分钟前
GitDiagram如何让你的GitHub项目可视化
java·后端·面试
Codebee42 分钟前
“自举开发“范式:OneCode如何用低代码重构自身工具链
java·人工智能·架构
星星电灯猴1 小时前
iOS 性能调试全流程:从 Demo 到产品化的小团队实战经验
后端
掘金-我是哪吒1 小时前
分布式微服务系统架构第158集:JavaPlus技术文档平台日更-JVM基础知识
jvm·分布式·微服务·架构·系统架构
程序无bug1 小时前
手写Spring框架
java·后端
JohnYan1 小时前
模板+数据的文档生成技术方案设计和实现
javascript·后端·架构
全干engineer1 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Da_秀1 小时前
软件工程中耦合度
开发语言·后端·架构·软件工程
蓝易云2 小时前
Qt框架中connect()方法的ConnectionType参数使用说明 点击改变文章字体大小
linux·前端·后端
a_Dragon12 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea