Char_filter and lowercase not working for search_analyzer

I've been experimenting with many different iterations of the below but haven't been able to match all the cases I'd like to.

My objective is to match the following cases:

  • "7-eleven" or "7eleven" to the text "7-Eleven"
  • "At&t" or "ATT" to "AT&T"

Mapping:

{
  "settings": {
    "analysis": {
      "char_filter" : {
        "remove_special_chars" : {
          "type" : "mapping",
          "mappings" : [
            "-=>",
            "&=>"
          ]
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 15,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      },
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase"
          ],
          "char_filter" : ["remove_special_chars"]
        },
        "autocomplete_search": {
          "tokenizer": "standard",
          "filter": [
            "lowercase"
          ],
          "char_filter" : ["remove_special_chars"]
        }
      }
    }
  },
  "mappings": {
    "vendors": {
      "_all": {},
      "properties": {
        "searchable": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}

Add Document:

put my_test_index/vendors/1
{
    "searchable": "7-Eleven"
}

Querying the documents

{
  "sort" : [
    "_score"
  ],
  "query": {
    "bool": {
      "should": [
          { "match" :
            { "searchable" :
              {
                "query" : "7-eleven",
                "operator" : "and"
              }
            }
          }
        ]
    }
  }
}

The above works for a query of 7eleven but not 7-eleven. Is something wrong with my syntax?Why is the query not going through the search analyzer?

OMG, there was some code deep within our project which was escaping the hyphen character.
The code above works as expected.

glad you found it, for future reference you might want to take a look at the explain API as well as the explain parameter in the search bodry.

1 Like