Skip to content

Instantly share code, notes, and snippets.

@polyfractal
Last active May 26, 2021 17:38
Show Gist options
  • Save polyfractal/4704772 to your computer and use it in GitHub Desktop.
Save polyfractal/4704772 to your computer and use it in GitHub Desktop.
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
Copy link

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