Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/65936531/elasticsearch-find-values-with-and-without-whitespaces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
946 views
Welcome To Ask or Share your Answers For Others

1 Answer

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"
        }
      }
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...