Page MenuHomeSoftware Heritage
Paste P913

random es_search with word typing simulation
ActivePublic

Authored by vsellier on Jan 4 2021, 9:40 AM.
#!/bin/bash -eu
searched=1
total=5
RANDOM_WORD="$(sort -R /usr/share/dict/words | head -${total})"
WORD="${1:-$RANDOM_WORD}"
export ES_SERVER=192.168.100.81:9200
for w in $WORD; do
echo
echo "Searching for $searched/$total $w"
letters="$(grep -o '.' <<<$w)"
search=""
search_file=$(mktemp)
result_file=$(mktemp)
for l in $letters; do
search="$search$l"
cat <<EOF >"${search_file}"
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "${search}",
"type": "bool_prefix",
"operator": "and",
"fields": [
"url.as_you_type",
"url.as_you_type._2gram",
"url.as_you_type._3gram"
]
}
}
]
}
},
"sort": [
{
"_score": "desc"
},
{
"sha1": "asc"
}
]
}
EOF
curl -s -XGET -H "Content-Type: application/json" http://${ES_SERVER}/origin/_search\?pretty -d @"$search_file" > "${result_file}"
took="$(jq '.took' $result_file)"
results="$(jq '.hits.total.value' $result_file)"
echo "Searching for $search : took=${took}ms nb_results=${results}"
done
searched=$(( searched + 1))
done
# echo
# echo "Search took $()ms"
# echo "Number of results: $()"
# echo
#jq '.hits.hits[]._source.url' $result_file
rm $search_file $result_file