2

we have some fields with an articlenumbers. This articlenumbers looks like AB 987 g567 323. When i search for "AB 987 g" then i find the right product, but when i search without withespaces i dont find anything. I tried pattern_replace, but it doesnt work.

"whitespace_filter": {
      "alphabets_char_filter": {
        "type": "pattern_replace",
        "pattern": " ",
        "replacement": ""
    }

How can i search for Articlenumbers with and without whitespaces?

1 Answer 1

2

You need to use edge_ngram along with char_filter, to achieve your use case

Adding a working example

Index Mapping:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer",
          "char_filter": [
            "replace_whitespace"
          ]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      },
      "char_filter": {
        "replace_whitespace": {
          "type": "mapping",
          "mappings": [
            "\\u0020=>"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "articlenumbers": {
        "type": "text",
        "fields": {
          "analyzed": {
            "type": "text",
            "analyzer": "my_analyzer"
          }
        }
      }
    }
  }
}

Index Data:

{
  "articlenumbers": "AB 987 g567 323"
}

Search Query:

{
  "query": {
    "multi_match": {
      "query": "AB987g",
      "fields": [
        "articlenumbers",
        "articlenumbers.analyzed"
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65936531",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4384104,
        "_source": {
          "articlenumbers": "AB 987 g567 323"
        }
      }
    ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.