ElasticSearch - Searching with hyphens

前端 未结 3 1399
离开以前
离开以前 2020-12-16 02:17

Elastic Search 1.6

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 sea

3条回答
  •  有刺的猬
    2020-12-16 03:11

    the Quote from Igor Motov is true, you have to add "analyze_wildcard":true, in order to make it worked with regex. But it is important to notice that the hyphen actually tokenizes "u-12" in "u" "12", two separated words.

    if preserve the original is important do not use Mapping char filter. Otherwise is kind of useful.

    Imagine that you have "m0-77", "m1-77" and "m2-77", if you search m*-77 you are going to have zero hits. However you can remplace "-" (hyphen) with AND in order to connect the two separed words and then search m* AND 77 that is going to give you a correct hit.

    you can do it in the client front.

    In your problem u-*

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

    t-sh*

      {
          "query":{
            "simple_query_string":{
              "query":"t AND sh*",
              "analyze_wildcard":true
            }
          }
        }
    

提交回复
热议问题