Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

mapping - Elasticsearch: Find values with and without whitespaces

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...