Last active
May 26, 2021 17:38
-
-
Save polyfractal/4704772 to your computer and use it in GitHub Desktop.
Commands associated with the "Starts-with" ElasticSearch tutorial
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
#Create the index with no mapping | |
curl -XPUT localhost:9200/startswith/ | |
#add some data | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"river dog"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"data"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"drive"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"drunk"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"dzone"}' | |
#try to perform a "starts-with" style query... | |
curl -XGET localhost:9200/startswith/test/_search?pretty -d '{ | |
"query": { | |
"match_phrase_prefix": { | |
"title": { | |
"query": "d", | |
"max_expansions": 5 | |
} | |
} | |
} | |
}' | grep title | |
#delete our old index, because it doesn't work | |
curl -XDELETE localhost:9200/startswith/ | |
#Recreate the index, this time with a keyword + lowercase mapping | |
curl -XPUT localhost:9200/startswith/ -d '{ | |
"settings":{ | |
"index":{ | |
"analysis":{ | |
"analyzer":{ | |
"analyzer_startswith":{ | |
"tokenizer":"keyword", | |
"filter":"lowercase" | |
} | |
} | |
} | |
} | |
}, | |
"mappings":{ | |
"test":{ | |
"properties":{ | |
"title":{ | |
"search_analyzer":"analyzer_startswith", | |
"index_analyzer":"analyzer_startswith", | |
"type":"string" | |
} | |
} | |
} | |
} | |
}' | |
#add the same data back | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"river dog"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"data"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"drive"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"drunk"}' | |
curl -XPOST localhost:9200/startswith/test/ -d '{"title":"dzone"}' | |
#Same query, different results | |
curl -XGET localhost:9200/startswith/test/_search?pretty -d '{ | |
"query": { | |
"match_phrase_prefix": { | |
"title": { | |
"query": "d", | |
"max_expansions": 5 | |
} | |
} | |
} | |
}' | grep title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's not compatible with ES 2. There is no index_analyzer in ES anymore.
You should use just analyzer