Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Commands associated with the "Starts-with" ElasticSearch tutorial
#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
@MarkLavrynenko

This comment has been minimized.

Copy link

@MarkLavrynenko MarkLavrynenko commented Jul 20, 2016

It's not compatible with ES 2. There is no index_analyzer in ES anymore.
You should use just analyzer

curl -XPUT localhost:9200/startswith/ -d '{
   "settings":{
      "index":{
         "analysis":{
            "analyzer":{
               "analyzer_startswith":{
                  "tokenizer":"keyword",
                  "filter":"lowercase"
               }
            }
         }
      }
   },
   "mappings":{
      "test":{
         "properties":{
            "title":{
               "analyzer":"analyzer_startswith",
               "type":"string"
            }
         }
      }
   }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.