3

I'm trying to make a field lowercase and not analyzed in Elasticsearch 5+ in order to search for strings with spaces in lowercase (them being indexed in mixed case)
Before Elasticsearch v5 we could use an analyzer like this one to accomplish it:

  "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "analyzer_keyword":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
     }
  }

This however doesn't work for me right now. And I believe the problem to be that "string" is deprecated and automatically converted to either keyword or text.
Anyone here know how to accomplish this? I thought about adding a "fields" tag to my mapping along the lines of:

  "fields": {
    "lowercase": {
      "type": "string"
       **somehow convert to lowercase**
    }
  }

This would make working with it slightly more challenging and I have no idea how to convert it to lowercase either.

Below you'll find a test setup which reproduces my exact problem.

create index:

{
  "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "analyzer_keyword":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
     }
  },
  "mappings":{
     "test":{
        "properties":{
           "name":{
              "analyzer":"analyzer_keyword",
              "type":"string"
           }
        }
     }
  }
}

Add a test record:

 {
    "name": "city test"
  }

Query that should match:

{
    "size": 20,
    "from": 0,
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "should": [{
                        "wildcard": {
                            "name": "*city t*"
                        }
                    }]
                }
            }]
        }
    }
}
Rick van Lieshout
  • 2,276
  • 2
  • 22
  • 39
  • Why are you searching on the `name` field while the field in your mapping is called `title`? Also why not simply declaring its type as `"type":"text"` instead? And finally, how `amsterdam t` is supposed to match `city test`? – Val Jan 12 '17 at 12:12
  • Let me update the examples, I have copied the wrong thingies :) My apologies. if I declare it as text it doesn't work with spaces. – Rick van Lieshout Jan 12 '17 at 13:02
  • What I basically want is to have a string like "City Test" and match that on "city test". that is all. – Rick van Lieshout Jan 12 '17 at 13:03
  • Yes, but what if you declare it as `text` with your keyword/lowercase analyzer? – Val Jan 12 '17 at 13:07
  • It ignores that completely. It will still not match spaces and behave as if I didn't place the analyzer. – Rick van Lieshout Jan 12 '17 at 13:26
  • Oh I know, it's because the `analysis` section needs to go directly under `settings` and not `settings > index`. Wipe your index and try again... – Val Jan 12 '17 at 13:30
  • That works flawlessly! Thanks mate! (if you want, post an answer I'll accept it!) – Rick van Lieshout Jan 12 '17 at 13:33

1 Answers1

4

When creating your index, you need to make sure that the analysis section is right under the settings section and not inside the settings > index section otherwise it won't work.

Then you also need to use the text data type for your field instead of the string one. Wipe your index, do that and it will work.

{
  "settings":{
        "analysis":{
           "analyzer":{
              "analyzer_keyword":{
                 "tokenizer":"keyword",
                 "filter":"lowercase"
              }
           }
        }
  },
  "mappings":{
     "test":{
        "properties":{
           "name":{
              "analyzer": "analyzer_keyword",
              "type": "text"
           }
        }
     }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360