url='-I -u username:password http://localhost:19200/t_device_logs_2022-09-28?pretty'
response=$(curl --write-out '%{http_code}' --silent --output /dev/null $url)
if [ $reponse -eq 200 ]
then
	echo "operate index"
	curl -X DELETE -u username:password "http://localhost:port/$indexName" # 使用delete举例
fi

第一句主要是根据elasticsearch官网的Exists API | Elasticsearch Guide [8.4] | Elastic​​​​​​这个里面的curl的方式,/t_device_logs_2022-09-28替换为你自己的index的名称,-u是如果有用户名和密码的时候设置。

第二句是裁剪curl返回的内容,并从内容中提取状态码,是404还是200,主要根据How to evaluate http response codes from bash/shell script? - Stack Overflow这个网站中描述的方法。

最终判断状态码如果是200,就存在这个index,如果404或者其他的,就不存在这个index.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐