Last active
May 14, 2020 16:24
-
-
Save seanstory/d02c362cfa428a3860e7a12e8104a351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "What is your Access Token? (Example: cfcbcbae003c1ff47cd338edb32da3749b6652f3739ed2c55384542a1dc9cccd)" | |
read ACCESS_TOKEN | |
echo "What is your Key? (Example: 5e7bce38f74c32c93d685d4f)" | |
read KEY | |
echo "What is your Host? (Example: workplace-search-1.ea-eden-3-staging.elastic.dev)" | |
read HOST | |
ACCESS_TOKEN=`echo $ACCESS_TOKEN | tr -d '[:blank:]'` | |
KEY=`echo $KEY | tr -d '[:blank:]'` | |
HOST=`echo $HOST | tr -d '[:blank:]'` | |
HOST=${HOST/https:\/\//} # remove https:// | |
HOST=${HOST//\//} #remove any lingering slashes | |
HOST=${HOST/workplace-search-/workplace-search-api-} #use API host | |
HOST=${HOST/workplace-search-api-api/workplace-search-api-} # just in case you passed the api host | |
JSON=`curl -s https://gist.githubusercontent.com/seanstory/fee66b1eaeb1afa6ca5eb29701f3c183/raw/3c7b3c2ceba66977c05ab72161711b635fdda56b/demo_business_records.json` | |
for i in {0..8}; do | |
printf "\n#############################\n### Uploading batch $((i+1)) of 9...\n" | |
batch=`echo "${JSON}" | jq -r ".[$((i*100)):$(((i+1)*100))]"` | |
status=$(echo $batch | curl -s -o response.json -w "%{http_code}\\n" -k -XPOST https://$HOST/api/ws/v1/sources/$KEY/documents/bulk_create -d @- -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json") | |
if [[ "$status" -ne 200 ]]; then | |
cat response.json | |
echo "failure! Got response code: $status" | |
if [[ "$status" -eq 401 ]]; then | |
echo "Make sure that you have the right Access Token, you entered: '$ACCESS_TOKEN'" | |
fi | |
if [[ "$status" -eq 404 ]]; then | |
echo "Make sure that you have the right Key, you entered: '$KEY'" | |
fi | |
if [[ "$status" -eq 000 ]]; then | |
echo "Make sure that you have the right host - something went unexpededly wrong when CUrling '$HOST'..." | |
fi | |
exit 1 | |
fi | |
response=`cat response.json` | |
rm response.json | |
error_count=`echo $response | jq -r ".results | .[] | .errors | length" | awk '{s+=$1} END {print s}'` | |
if [[ "$error_count" -ne 0 ]]; then | |
echo $response | |
echo "failure! The response had errors" | |
exit 1 | |
fi | |
count=`echo $response | jq -r ".results | length"` | |
echo "Indexed $count businesses:" | |
echo $batch | jq -r ".[] | .business_name" | |
done | |
printf "\n#####################################\n### Finished! Uploaded all businesses\n\n" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment