forked from bsander/elastictools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsesynonyms
executable file
·49 lines (41 loc) · 1.08 KB
/
parsesynonyms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env sh
# Parameters:
# $1 = input filename
# $2 = server, defaults to localhost:9200
# $3 = output filename, defaults to [input filename]_generated.txt
if [ -z "$1" ]
then
echo Please apply an input file
exit 1
fi
if [ -z "$2" ]
then
server="localhost:9200"
else
server="$2"
fi
if [ -z "$3" ]
then
filename=$(basename "$1")
outputFile="${filename%.*}_generated.txt"
else
outputFile="$3"
fi
while read line
do
#expected response example: {"tokens":[{"token":"wlan kennwort,wlan schlussel","start_offset":0,"end_offset":28,"type":"word","position":1}]}
response=$(curl -s -XGET "$server/_analyze?tokenizer=keyword&filters=lowercase,asciifolding" -d "$line" | jq -r '.tokens[0].token | tostring')
if [ -z "$response" ] || [ "$response" = "null" ]
then
echo Elasticsearch returned a malformed response
exit 2
fi
if [ -n "$outputResponse" ]
then
#only add newline if outputresponse already exists
outputResponse=$outputResponse$"\n"
fi
#concatenate string
outputResponse=$outputResponse$response
done < $1
echo $outputResponse > $outputFile