Created
May 24, 2011 15:32
-
-
Save karmi/988923 to your computer and use it in GitHub Desktop.
NGram Analyzer in ElasticSearch
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
# ======================================== | |
# Testing n-gram analysis in ElasticSearch | |
# ======================================== | |
curl -X DELETE localhost:9200/ngram_test | |
curl -X PUT localhost:9200/ngram_test -d ' | |
{ | |
"settings" : { | |
"index" : { | |
"analysis" : { | |
"analyzer" : { | |
"url_analyzer" : { | |
"type" : "custom", | |
"tokenizer" : "lowercase", | |
"filter" : ["stop", "url_stop", "url_ngram"] | |
} | |
}, | |
"filter" : { | |
"url_stop" : { | |
"type" : "stop", | |
"stopwords" : ["http", "https"] | |
}, | |
"url_ngram" : { | |
"type" : "nGram", | |
"min_gram" : 3, | |
"max_gram" : 5 | |
} | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"url": { | |
"properties": { | |
"url": { | |
"type": "string", | |
"analyzer": "url_analyzer", | |
"boost": 10 | |
} | |
} | |
} | |
} | |
} | |
' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://heise.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://heisewetter.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://eisenwerken.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://eisenwerkenberlin.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://urlaubinkroatien.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://besteurlaubinkroatien.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/url" -d '{ "url" : "http://kroatien.de" }' | |
curl -X POST "http://localhost:9200/ngram_test/_refresh" | |
# curl "http://localhost:9200/ngram_test/_analyze?text=http://heise.de&analyzer=url_analyzer" | |
URLS=' | |
http://localhost:9200/ngram_test/_search?q=url:heise | |
http://localhost:9200/ngram_test/_search?q=url:eis | |
http://localhost:9200/ngram_test/_search?q=url:berlin | |
http://localhost:9200/ngram_test/_search?q=url:wetter | |
http://localhost:9200/ngram_test/_search?q=url:kroatien | |
http://localhost:9200/ngram_test/_search?q=url:(urlaub%20kroatien) | |
' | |
for url in ${URLS} | |
do | |
echo; echo; echo ">>> ${url}" | |
if which open &> /dev/null; then | |
open "${url}&pretty=true" | |
fi | |
curl "${url}&pretty=true" | |
done |
I also would like to know the answer to the question @stevebissett asked.
Apparently the answer is in the documentation https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html.
We want to ensure that our inverted index contains edge n-grams of every word, but we want to match only the full words that the user has entered (brown and fo). We can do this by using the autocomplete analyzer at index time and the standard analyzer at search time.
Hope this helps someone
You could add a condition to make all the NGrams match,
{
"match": {
"url": {
"query": "eisen",
"analyzer": "trigrams", <-- run the same analyzer on the query
"minimum_should_match": "100%"
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@karmi, are you aware here that if I search for
eisen
, I get backhttp://heise.de
as a result, how do I stop this from happening.In other words, I want the results to contains all of the letters in the search term.
http://localhost:9200/ngram_test/_search?q=url:eisen&pretty=true