Last active
August 29, 2015 14:03
-
-
Save ChristianRuetgers/165a707a1e628caac73d to your computer and use it in GitHub Desktop.
Elasticsearch autocomplete or autosuggest by token
This file contains hidden or 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
| # [unsolved question] How to get google like autosuggest term list by token by elasticsearch | |
| # mapping including a tokenizer and a filter | |
| curl -XPUT 'http://127.0.0.1:9200/files/?pretty=1' -d ' | |
| { | |
| "settings" : { | |
| "analysis" : { | |
| "analyzer" : { | |
| "filename_search" : { | |
| "tokenizer" : "filename", | |
| "filter" : ["lowercase"] | |
| }, | |
| "filename_index" : { | |
| "tokenizer" : "filename", | |
| "filter" : ["lowercase","edge_ngram"] | |
| } | |
| }, | |
| "tokenizer" : { | |
| "filename" : { | |
| "pattern" : "[^[;_\\.\\/]\\d]+", | |
| "type" : "pattern" | |
| } | |
| }, | |
| "filter" : { | |
| "edge_ngram" : { | |
| "side" : "front", | |
| "max_gram" : 20, | |
| "min_gram" : 2, | |
| "type" : "edgeNGram" | |
| } | |
| } | |
| } | |
| }, | |
| "mappings" : { | |
| "file" : { | |
| "properties" : { | |
| "filename" : { | |
| "type" : "string", | |
| "search_analyzer" : "filename_search", | |
| "index_analyzer" : "filename_index" | |
| } | |
| } | |
| } | |
| } | |
| }' | |
| # test analyzers | |
| curl -XGET 'localhost:9200/files/_analyze?pretty=1&text=BRAND_ConnectBlue_A1234.jpg&analyzer=filename_search' | |
| curl -XGET 'localhost:9200/files/_analyze?pretty=1&text=BRAND_ConnectBlue_A1234.jpg&analyzer=filename_index' | |
| # add some example data | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "BRAND_ConnectBlue_A1234.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "BRAND_Connect_A1233.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "BRAND_ConceptSpace_A1244.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "COMPANY_Connect_A1222.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "COMPANY_Concept_A1233.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "DEALER_Connect_B1234_.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "DEALER_Contour21_B1233.jpg"}' | |
| curl -X POST "localhost:9200/files/file" -d '{ "filename" : "DEALER_ConceptCube_B2233.jpg"}' | |
| curl -X POST "localhost:9200/files/_refresh" | |
| # Various approaches to get the desired suggestion does not deliver the expected results | |
| # Test1: | |
| curl -XGET 'localhost:9200/files/_suggest?pretty=true' -d '{ | |
| "text" : "con", | |
| "simple_phrase" : { | |
| "phrase" : { | |
| "field" : "filename", | |
| "size" : 15, | |
| "real_word_error_likelihood" : 0.75, | |
| "max_errors" : 0.1, | |
| "gram_size" : 3 | |
| } | |
| } | |
| }' | |
| # Test2: | |
| curl -XGET 'localhost:9200/files/_suggest?pretty=true' -d '{ | |
| "my-suggestion" : { | |
| "text" : "con", | |
| "term" : { | |
| "field" : "filename", | |
| "analyzer": "filename_index" | |
| } | |
| } | |
| }' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment