Search code examples
elasticsearchmapping

ElasticSearch - Searching with hyphens


I want to index text that contains hyphens, for example U-12, U-17, WU-12, t-shirt... and to be able to use a "Simple Query String" query to search on them.

Data sample (simplified):

{"title":"U-12 Soccer",
 "comment": "the t-shirts are dirty"}

As there are quite a lot of questions already about hyphens, I tried the following solution already:

Use a Char filter: ElasticSearch - Searching with hyphens in name.

So I went for this mapping:

{
  "settings":{
    "analysis":{
      "char_filter":{
        "myHyphenRemoval":{
          "type":"mapping",
          "mappings":[
            "-=>"
          ]
        }
      },
      "analyzer":{
        "default":{
          "type":"custom",
          "char_filter":  [ "myHyphenRemoval" ],
          "tokenizer":"standard",
          "filter":[
            "standard",
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings":{
    "test":{
      "properties":{
        "title":{
          "type":"string"
        },
        "comment":{
          "type":"string"
        }
      }
    }
  }
}

Searching is done with the following query:

{"_source":true,
  "query":{
    "simple_query_string":{
      "query":"<Text>",
      "default_operator":"AND"
    }
  }
}
  1. What works:
    • "U-12"
    • "U*"
    • "t*"
    • "ts*"
  2. What didn't work:
    • "U-*"
    • "u-1*"
    • "t-*"
    • "t-sh*"

So it seems the char filter is not executed on search strings?

What could I do to make this work?

I am using Elastic Search 1.6.


Solution

  • The answer is really simple:

    Quote from Igor Motov: Configuring the standard tokenizer

    By default the simple_query_string query doesn't analyze the words with wildcards. As a result it searches for all tokens that start with i-ma. The word i-mac doesn't match this request because during analysis it's split into two tokens i and mac and neither of these tokens starts with i-ma. In order to make this query find i-mac you need to make it analyze wildcards:

    {
      "_source":true,
      "query":{
        "simple_query_string":{
          "query":"u-1*",
          "analyze_wildcard":true,
          "default_operator":"AND"
        }
      }
    }