Fonty
Fonty

Reputation: 159

Only getting results when elasticsearch is case sensitive

I currently have a problem with my multi match search in ES. Its simple like that: If I'm searching for the City "Sachsen", I'm getting results. If I'm searching for "sachsen" (lowercase), I'm getting no results.

how to avoid this?

QUERY with no results

{
"match" : {
    "City" : {
        "query" : "sachsen"
    }
}

My analyzer is analyzer_keyword. Should I have anything add ?

MAPPING

City: {
type: "string",
analyzer: "analyzer_keyword"
}

Upvotes: 1

Views: 589

Answers (1)

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

Your analyzer_keyword analyzer is most probably of type keyword which means you can only perform exact matches on it.


It's standard practice to apply multiple "variants" of a field, one of which is going to match lowercase, possibly ascii-tokenized characters (think München -> munchen) and one which will not be tokenized in any way (this is what you have in your analyzer_keyword).

Since you intend to search the lowercase version of Sachsen, your mapping could look something like

PUT sachsen
{
  "mappings": {
    "properties": {
      "City": {
        "type": "keyword",             <----

        "fields": {
          "standard": {
            "type": "text",           
            "analyzer": "standard"     <----
          }
        }
      }
    }
  }
}

After indexing a doc

POST sachsen/_doc
{
  "City": "Sachsen"
}

The following will work for exact matches:

GET sachsen/_search
{
  "query": {
    "match": {
      "City": "Sachsen"
    }
  }
}

and this for lowercase

GET sachsen/_search
{
  "query": {
    "match": {
      "City.standard": "sachsen"
    }
  }
}

Note that I'm using the default, standard analyzer here but you can choose any one you deem appropriate.

Upvotes: 1

Related Questions