弹性搜索带空格的通配符搜索 [英] Elastic Search wildcard search with spaces

查看:215
本文介绍了弹性搜索带空格的通配符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询。我试图找到你好世界的价值,但它返回零结果。但是,当 value ='hello *'时,它会给我预期的结果。任何想法,我如何可以改变我的查询给我这个你好世界的结果?我试过 * hello world * ,但由于某些原因,它不会搜索任何空格。



我认为这与空格有关,当我尝试搜索* *时,它给我没有结果。但我知道我有很多价值在那里与空格。任何想法都有帮助!

  {
查询:{
filtered:{
filter:{
and:[
{
terms:{
variant:[
collection
]
}
}
]
},
查询:{
通配符:{
name:{
value:你好世界
}
}
}
}
}
}


解决方案

您的字段使用的映射是什么? name ?如果您尚未定义任何映射,或者您刚刚将该类型定义为字符串(没有任何分析器),则使用标准分析器对该字段进行分析。这将分别创建令牌为你好和世界。这意味着通配符查询将适用于 * ell * * wor * 而不是空格。



您必须更改映射,将字段name设置为not_analyzed,然后使用带有空格的通配符搜索将起作用。



一个谨慎的词:
通配符搜索很重。如果要进行部分匹配搜索(相当于%like%)您可以在分析器中使用ngram令牌过滤器,并进行术语搜索。它将照顾匹配的部分字符串,并具有更好的性能。


I have the following query. I'm trying to find values of 'hello world', but it returns zero results. However, when value = 'hello*', it does give me that expected result. Any idea how I can change my query to give me that hello world result? I've tried *hello world*, but for some reason it just won't search anything with spaces.

I think it has something to do with the spaces as when I try to search "* *", it gives me no results. But I know I have many values in there with spaces. Any ideas would help!

 {
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "variant": [
                "collection"
              ]
            }
          }
        ]
      },
      "query": {
        "wildcard": {
          "name": {
            "value": "hello world"
          }
        }
      }
    }
  }
}

解决方案

What is the mapping you have used for your field name? If you have not defined any mapping or you have just defined the type as string (without any analyzer) then the field will be analyzed using the standard analyzer. This will create tokens as "hello" and "world" separately. This means wildcard query will work for something like *ell* or *wor* but not with spaces.

You have to change your mapping to have the field "name" as not_analyzed then wildcard searches with space will work.

A word of caution: Wildcard searches are heavy. If you want to do partial matching search (equivalent of %like%) You can use ngram token filter in your analyzer and do term search. It will take care of matching partial string and have better performance too.

这篇关于弹性搜索带空格的通配符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆