Skip to content

Instantly share code, notes, and snippets.

@evalphobia evalphobia/exec.sh
Created Mar 9, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.