• 问题

但查询http://localhost:9100/index1,index2,index3/_search某个索引不存在时,就会出错

1
elasticsearch.exceptions.NotFoundError: TransportError(404, 'index_not_found_exception', 'no such index')
  • 解决办法

    项目使用的是elasticsearch库,在使用search函数时,添加一个param参数,代码如下

1
2
3
4
5
6
7
from elasticsearch import Elasticsearch
es = Elasticsearch(api, http_auth=http_auth)
# body,index分别查询体,索引
es_index='index1,index2,index3'
body={}
params = {"ignore_unavailable": "true"}
result = es.search(index=es_index, body=body, params=params)

这样就会忽略索引不存在的问题,不会报404了

如果使用requests库或者类似postman接口请求工具请求es数据时,可以在url后加?ignore_unavailable=true参数即可忽略索引不存在错误

1
http://localhost:9100/index1,index2,index3/_search?ignore_unavailable=true