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: "*"
相关推荐
程序员大金5 分钟前
基于SpringBoot+Vue+MySQL的在线学习交流平台
java·vue.js·spring boot·后端·学习·mysql·intellij-idea
qq_25183645712 分钟前
基于SpringBoot vue 医院病房信息管理系统设计与实现
vue.js·spring boot·后端
qq_2518364571 小时前
基于springboot vue3 在线考试系统设计与实现 源码数据库 文档
数据库·spring boot·后端
2401_858120531 小时前
古典舞在线交流平台:SpringBoot设计与实现详解
java·spring boot·后端
赐你岁月如歌1 小时前
如何使用ssm实现基于web的网站的设计与实现+vue
java·后端·ssm
闲宇非鱼1 小时前
微服务到底是技术问题还是管理问题?
java·后端·微服务
大G哥2 小时前
ELK日志收集之ES的DSL查询语句
大数据·elk·elasticsearch·搜索引擎·jenkins
不能放弃治疗3 小时前
重生之我们在ES顶端相遇第 20 章 - Mapping 参数设置大全(进阶)
elasticsearch
潘多编程3 小时前
Spring Boot微服务架构设计与实战
spring boot·后端·微服务
2402_857589363 小时前
新闻推荐系统:Spring Boot框架详解
java·spring boot·后端