Created
March 9, 2017 08:09
-
-
Save evalphobia/6fc92588f3d75ef888fe5c607be04620 to your computer and use it in GitHub Desktop.
Execute query from the list file to Elasticsearch
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 | |
# Read user_id from list.txt and executes query to ES. | |
# Usage: | |
# $ bash exec.sh list.txt | |
HOST='http://localhost:9200' | |
INDEX='index_name_1,index_name_2,my_awesome_index' | |
LIMIT=1000 | |
OUTPUT='[.user_id, .status]' # jq expression | |
TARGET=$1 | |
LINES=`wc -l $1 | awk '{print $1}'` | |
LOC=0 | |
URL=$HOST/$INDEX/_search | |
function exec_elasticsearch() { | |
while [ $LOC -lt $LINES ]; do | |
limit=$LIMIT | |
LOC=$(( LOC + LIMIT )) | |
SUB=$(( LINES - LOC )) | |
# last iteration | |
if [ $SUB -lt 0 ]; then | |
limit=$(( LIMIT + SUB )) | |
fi | |
# output example: 100,101,102,900 | |
IDS=`head -n $LOC $TARGET | tail -n $limit | perl -pe 's/\n/,/g' | perl -pe s/,$// ` | |
query=$TEMPLATE_BEFORE$IDS$TEMPLATE_AFTER | |
curl -s $URL -H 'Content-Type: application/json' -d "$query" | jq -c ".hits.hits[]._source | $OUTPUT" | |
done | |
} | |
# e.g. | |
# SELECT user_id, status | |
# FROM index_name_1, index_name_2, my_awesome_index | |
# WHERE user_id IN (...) | |
# AND status <> 2 | |
TEMPLATE_BEFORE='{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"terms": { | |
"id": [' | |
TEMPLATE_AFTER='] | |
} | |
} | |
], | |
"must_not": [ | |
{ | |
"terms": { | |
"status": [2] | |
} | |
} | |
] | |
} | |
}, | |
"_source": [ | |
"id", | |
"status" | |
] | |
}' | |
exec_elasticsearch |
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
100 | |
101 | |
102 | |
900 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment