Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Case Insensitive ElasticSearch Regular expressions
# Delete any old test index
DELETE example_copy
# Create index with appropriate mappings/settings
# Note that the pattern is meant to not match anything so that the entire field is taken as the "token"
PUT example_copy
{
"settings": {
"analysis": {
"analyzer": {
"caseInsensitive": {
"type" : "pattern",
"pattern" : "abcdefghijklmnopqrstuvwxyzywx",
"filter" : ["lowercase"]
}
}
}
},
"mappings": {
"example" : {
"properties": {
"original" : {
"type": "string",
"copy_to": ["copied", "unindex"]
},
"copied" : {
"type": "string",
"index": "analyzed",
"analyzer": "caseInsensitive"
},
"unindex" : {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
# Create document
POST /example_copy/example
{
"original" : "Hello There FRiend"
}
# Will return document
POST /example_copy/_search
{
"query": {
"regexp" : {
"original" : {
"value" : "t.*"
}
}
}
}
# Will return document
POST /example_copy/_search
{
"query": {
"regexp" : {
"copied" : {
"value" : "h.*"
}
}
}
}
# Will return document because case sensitive unindex field.
POST /example_copy/_search
{
"query": {
"regexp" : {
"unindex" : {
"value" : "H.*"
}
}
}
}
# Will NOT return document because the value in the field is 'h' not 'H' due to the lowercase filter
POST /example_copy/_search
{
"query": {
"regexp" : {
"copied" : {
"value" : "H.*"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment