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: "*"
相关推荐
⑩-6 分钟前
SpringCloud-Sleuth链路追踪实战
后端·spring·spring cloud
冷崖10 分钟前
原子锁操作
c++·后端
moxiaoran575312 分钟前
Spring AOP开发的使用场景
java·后端·spring
一线大码4 小时前
Gradle 基础篇之基础知识的介绍和使用
后端·gradle
前端不太难4 小时前
从 Navigation State 反推架构腐化
前端·架构·react
Java猿_4 小时前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
小王师傅665 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
码农很忙5 小时前
HSAP一体化混合搜索与分析架构全解:重塑数据价值的新范式
架构
码事漫谈5 小时前
C++高并发编程核心技能解析
后端
码事漫谈5 小时前
C++与浏览器交织-从Chrome插件到WebAssembly,开启性能之门
后端