简体   繁体   中英

How to handle elastic search resource_already_exists_exception using bash script

I am quite new to Elastic and bash script. Could anyone please help me with the below - How can i skip creating the elastic index which already exists using bash

First I am creating a mapping in a bash script -

#!/bin/bash

curl --user uid:password -X PUT **"https:localhost/newindex"** -H 'Content-Type: application/json' -d'{  "mappings": { "properties": {.....}}}}'

#Then i am adding the data to it

curl --user uid:password -X POST **"https:localhost/newindex"** -H 'Content-Type: application/json' -d'{"date":"data..."...... }'

If i run it for the first time it works, but when i run for the second time onwards it gives me an error "resource_already_exists_exception" , how can i handle this using bash script

You can use --fail flag:

-f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will pre‐ vent curl from outputting that and return error 22.

The exit code will be zero if the command has succeeded, so you can check that:

curl --fail --user uid:password -X POST "https:localhost/newindex" -H 'Content- 
Type: application/json' -d'{"date":"data..."...... }'

if [ 0 -eq $? ]; then … fi;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM