Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Created March 9, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evalphobia/6fc92588f3d75ef888fe5c607be04620 to your computer and use it in GitHub Desktop.
Save evalphobia/6fc92588f3d75ef888fe5c607be04620 to your computer and use it in GitHub Desktop.
Execute query from the list file to Elasticsearch
#!/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
100
101
102
900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment