-
-
Save missinglink/01c7db8bb09f2f935574 to your computer and use it in GitHub Desktop.
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 | |
ES='localhost:9200'; | |
create_index(){ | |
curl -XPUT "$ES/completionsuggester?pretty=true" -d' | |
{ | |
"settings": { | |
"index": { | |
"number_of_shards": 1, | |
"number_of_replicas": 1 | |
}, | |
"analysis": { | |
"tokenizer":{ | |
"ngram_tokenizer_search":{ | |
"type": "edgeNgram", | |
"min_gram": 3, | |
"max_gram": 7, | |
"token_chars": ["letter","digit"] | |
}, | |
"ngram_tokenizer":{ | |
"type": "nGram", | |
"min_gram": 1, | |
"max_gram": 7, | |
"token_chars": ["letter","digit"] | |
} | |
}, | |
"analyzer": { | |
"word_analyzer_search": { | |
"type": "custom", | |
"tokenizer": "ngram_tokenizer_search", | |
"filter": ["lowercase"] | |
}, | |
"word_analyzer": { | |
"type": "custom", | |
"tokenizer": "ngram_tokenizer", | |
"filter": ["lowercase"] | |
}, | |
"autocomplete_analyzer": { | |
"type": "custom", | |
"tokenizer": "whitespace", | |
"filter": ["lowercase","word_delimiter"] | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"complete": { | |
"properties": { | |
"project_name": { | |
"type": "string", | |
"index_analyzer": "word_analyzer", | |
"search_analyzer": "word_analyzer_search" | |
}, | |
"project_autocomplete_list": { | |
"type": "completion", | |
"payloads": true, | |
"preserve_position_increments": false | |
} | |
} | |
} | |
} | |
}'; | |
} | |
# Index some docs | |
index_doc1() { | |
curl -XPOST "$ES/completionsuggester/complete/1?pretty=true" -d' | |
{ | |
"project_name":"Bluebird of Your Mind", | |
"project_autocomplete_list" : [{ | |
"input": [ | |
"Bangalore_Bluebird", | |
"Bangalore_Bluebird of", | |
"Bangalore_of", | |
"Bangalore_Bluebird of Your", | |
"Bangalore_of Your", | |
"Bangalore_Your", | |
"Bangalore_Bluebird of Your Mind", | |
"Bangalore_of Your Mind", | |
"Bangalore_Your Mind", | |
"Bangalore_Mind", | |
"Bangalore_Whitefield", | |
"Bangalore_Some" | |
], | |
"weight":55, | |
"payload": { | |
"id":1, | |
"name":"Bluebird of Your Mind", | |
"project_class":"B", | |
"locality_id":66, | |
"locality_name":"Whitefield", | |
"builder_id":9, | |
"builder_name":"Some", | |
"builder_logo":"www.ankitkumar.in" | |
}, | |
"output":"Bluebird of Your Mind"}] | |
}'; | |
} | |
index_doc2() { | |
for i in {101..7000}; do | |
curl -s -XPOST "$ES/completionsuggester/complete/$i?pretty=true" -d' | |
{ | |
"project_name":"Nahar Amrit Shakti", | |
"project_autocomplete_list" : [{ | |
"input": [ | |
"Bangalore_Nshar", | |
"Bangalore_Nahar Amrit", | |
"Bangalore_Amrit", | |
"Bangalore_Nahar Amrit Shakti", | |
"Bangalore_Amrit Shakti", | |
"Bangalore_Shakti", | |
"Bangalore_Electronic", | |
"Bangalore_Any" | |
], | |
"weight":55, | |
"payload": { | |
"id":2, | |
"name":"Nahar Amrit Shakti", | |
"project_class":"C", | |
"locality_id":61, | |
"locality_name":"Electronic", | |
"builder_id":1, | |
"builder_name":"Any", | |
"builder_logo":"www.ankit.in" | |
}, | |
"output":"Nahar Amrit Shakti"}] | |
}' > /dev/null; | |
if [[ $(( $i % 100 )) == 0 ]]; then | |
echo "indexing... $i"; | |
fi | |
done | |
} | |
use_suggester() { | |
curl -XPOST "$ES/completionsuggester/_suggest?pretty=true" -d ' | |
{ | |
"project_suggest": { | |
"text": "Bangalore_Bluebird", | |
"completion": { | |
"field": "project_autocomplete_list" | |
} | |
} | |
}'; | |
} | |
delete_doc() { | |
curl -XDELETE "$ES/completionsuggester/complete/1?pretty=true" | |
} | |
drop_index(){ | |
curl -XDELETE "$ES/completionsuggester?pretty=true"; | |
} | |
# Start from scratch | |
drop_index; | |
create_index; | |
index_doc1; | |
index_doc2; | |
echo "Delete Bangalore_Bluebird"; | |
delete_doc; | |
curl -XGET "$ES/completionsuggester/complete/1?pretty=true" | |
curl -XGET "$ES/completionsuggester/_optimize?only_expunge_deletes=true" ; | |
echo "Should not return Bangalore_Bluebird (or anything)"; | |
curl -XPOST "$ES/completionsuggester/_suggest?pretty=true" -d ' | |
{ | |
"project_suggest": { | |
"text": "Bangalore_Bluebird", | |
"completion": { | |
"field": "project_autocomplete_list" | |
} | |
} | |
}'; | |
# Refresh index | |
# curl -XPOST "$ES/completionsuggester/_refresh"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment