Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Why does a search for a misspelling ("optius" instead of "optimus") find no documents?
echo 'Delete the index.'
curl -X DELETE 'http://localhost:9200/test_products/?pretty=true'
echo; echo
echo 'Create the index. Copied directly from https://gist.github.com/961303 .'
curl -X PUT 'http://localhost:9200/test_products/?pretty=true' -d '
{
"settings" : {
"analysis" : {
"filter" : {
"3_8_ngram" : {
"type": "nGram",
"min_gram": 3,
"max_gram": 8
}
},
"analyzer" : {
"ascii_3_8_ngram" : {
"type": "custom",
"tokenizer": "standard",
"filter": [ "standard", "lowercase", "asciifolding", "3_8_ngram" ]
}
}
}
}
}
'
echo; echo
echo 'Create the mapping.'
curl -X PUT 'http://localhost:9200/test_products/product/_mapping?pretty=true' -d '
{
"product" : {
"properties" : {
"name" : {
"type": "string",
"index_analyzer": "ascii_3_8_ngram",
"search_analyzer": "ascii_3_8_ngram"
}
}
}
}
'
echo; echo
echo 'Index some documents.'
curl -X POST 'http://localhost:9200/test_products/product?pretty=true' -d '{ "name" : "Optimus Prime" }'
curl -X POST 'http://localhost:9200/test_products/product?pretty=true' -d '{ "name" : "Optimus Primal" }'
curl -X POST 'http://localhost:9200/test_products/product?pretty=true' -d '{ "name" : "Fortress Maximus" }'
curl -X POST 'http://localhost:9200/test_products/product?pretty=true' -d '{ "name" : "Megatron" }'
echo; echo
echo 'Refresh the index.'
curl -X POST 'http://localhost:9200/test_products/_refresh?pretty=true'
echo; echo
echo 'Search for "prim". Expect "Optimus Prime" and "Optimus Primal".'
curl -X GET 'http://localhost:9200/test_products/_search?pretty=true' -d '
{
"query" : {
"dis_max" : {
"queries" : [
{ "field" : { "name" : "prim" } }
]
}
}
}
'
echo; echo
echo 'Search for "optius" (misspelling of "optimus"). Expect "Optimus Prime" and "Optimus Primal".'
curl -X GET 'http://localhost:9200/test_products/_search?pretty=true' -d '
{
"query" : {
"dis_max" : {
"queries" : [
{ "field" : { "name" : "optius" } }
]
}
}
}
'
echo; echo
echo 'Search for "imus". Expect "Optimus Prime", "Optimus Primal", "Fortress Maximus".'
curl -X GET 'http://localhost:9200/test_products/_search?pretty=true' -d '
{
"query" : {
"dis_max" : {
"queries" : [
{ "field" : { "name" : "imus" } }
]
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment