Python查询ES错误ApiError(406, ‘Content-Type ...is not supported

现象

使用python查询es数据时出现下面错误

bash 复制代码
Traceback (most recent call last):
  File "getUsers.py", line 26, in <module>
    response = es.search(index="lizz_users", body=query)
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/_sync/client/utils.py", line 402, in wrapped
    return api(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/_sync/client/__init__.py", line 3734, in search
    "POST", __path, params=__query, headers=__headers, body=__body
  File "/usr/local/lib/python3.6/site-packages/elasticsearch/_sync/client/_base.py", line 321, in perform_request
    message=message, meta=meta, body=resp_body
elasticsearch.ApiError: ApiError(406, 'Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported', 'Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported')

原因

python安装的Elasticsearch版本与es服务的版本不一致

解决办法

查看es版本

bash 复制代码
curl  http://172.11.111.11:9200/

{
  "name" : "bike-es-ssd-1",
  "cluster_name" : "bike-user",
  "cluster_uuid" : "9QixMq6dTpeDO-WEfnq_PA",
  "version" : {
    "number" : "7.6.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",
    "build_date" : "2020-02-06T00:09:00.449973Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
  • 可以看到es版本为7.6.0

重新安装python插件

bash 复制代码
pip3 install elasticsearch==7.6.0

搞定,执行,成功

相关推荐
码云骑士2 分钟前
94-实战Prompt优化师Agent-迭代优化-效果评分-自动生成Few-Shot
python·prompt
从零开始学习人工智能15 分钟前
PyTorch 踩坑:Input type \(Half\) and bias type \(float\) 类型不匹配终极解决方案
人工智能·pytorch·python
海盗123418 分钟前
微软技术日报——2026-08-07
python·microsoft·flask
Elasticsearch20 分钟前
一个 ES|QL 查询替代两个:`WHERE IN` subquery 取代 Elasticsearch 中的复制粘贴循环
elasticsearch
醉城夜风~25 分钟前
querySelector()
人工智能·python·tensorflow
看浪的路人1 小时前
第5讲:代码审查与 Bug 检测
开发语言·windows·python
码云骑士2 小时前
93-Prompt注入攻击与防御-直接注入-间接注入-越狱-输入净化
python·prompt
weixin_BYSJ19872 小时前
【java项目分享】springboot阅读推荐平台10600
java·javascript·spring boot·python·django·flask·php
阿kun要赚马内2 小时前
工具在langchain agent中的调用
人工智能·后端·python
GrowthDiary0072 小时前
Python 常用函数总结
开发语言·python