Search code examples
javaspring-bootelasticsearchspring-data-elasticsearch

Remove hyphens while search time in ElasticSearch


I want to create a search for books with ElasticSearch and SpringData.

I index my books with ISBN/EAN without hyphens and save it in my database. This data I index with ElasticSearch.

Indexed data: 1113333444444 If I'm search for a ISBN/EAN with hyphen: 111-3333-444444

There is no result. If I'm searching without hyphen, my book will be found as expected.

My settings are like this:

{
  "analysis": {
    "filter": {
      "clean_special": {
        "type": "pattern_replace",
        "pattern": "[^a-zA-Z0-9]",
        "replacement": ""
      }
    },
    "analyzer": {
      "isbn_search_analyzer": {
        "type": "custom",
        "tokenizer": "keyword",
        "filter": [
          "clean_special"
        ]
      }
    }
  }
}

I index my fields like this:

   @Field(type = FieldType.Keyword, searchAnalyzer = "isbn_search_analyzer")
   private String isbn;
   @Field(type = FieldType.Keyword, searchAnalyzer = "isbn_search_analyzer")
   private String ean;

If I test my analyzer:

GET indexname/_analyze
{
  "analyzer" : "isbn_search_analyzer",
  "text" : "111-3333-444444"
}

I get following result:

{
  "tokens" : [
    {
      "token" : "1113333444444",
      "start_offset" : 0,
      "end_offset" : 15,
      "type" : "word",
      "position" : 0
    }
  ]
}

If I'm search like this:

GET indexname/_search
{
   "query": {
    "query_string": {
      "fields": [ "isbn", "ean" ],
      "query": "111-3333-444444"
    }
  }
}

I don't get any result. Have someone of you an idea?


Solution

  • Elasticsearch does not analyze fields of type keyword. You need to set the type to text.