简体   繁体   English

Elasticsearch字词过滤器,但带前缀

[英]Elasticsearch terms filter, but for a prefix

I have a field that I want to filter for multiple values. 我有一个要过滤多个值的字段。 I'm currently doing this with a terms filter: 我目前正在使用terms过滤器执行此操作:

"filter" : {
            "terms" : { "user" : ["kimchy", "elasticsearch"]}
        }

But I want the filter to match on prefixes as well as whole terms, something like this ( prefixes is just me pseudocoding what I want): 但是我希望过滤器匹配前缀以及整个术语,像这样( prefixes只是我对所需的内容进行伪编码):

 "filter" : {
            "prefixes" : { "user" : ["kim", "elast"]}
        }

Or do I have to resort to multiple or filters? 还是我不得不求助于多个or过滤器?

"filter" : {
            "or" : {
                "filters" : [
                    {
                        "prefix" : { "user" : "kim" }
                    },
                    {
                        "prefix" : { "user" : "elast" }
                    }
                ],

ES does not have anything like prefixes but ES没有像prefixes这样的东西,但是

1) you could use query string query rather than writing multiple or filters 1)您可以使用查询字符串查询而不是编写多个or filters

You could write your query as 您可以将查询写为

{
  "query": {
    "query_string": {
      "default_field": "users",
      "query": "kim* OR elas*"
    }
  }
}

There is one problem though, if your user field is mapped as not_analyzed then it wont find Kimchy , Elasticsearch as by default lowercase_expanded_terms is true and it will lowercase all your prefixes. 还有一个问题,但如果你的user的字段映射not_analyzed那么它不会找到Kimchy,Elasticsearch因为默认情况下lowercase_expanded_terms是真实的,它会小写所有的前缀。

For this to work 100%, I would recommend using keyword tokenizer with lowercase token filter so that we can make case insensitive search, after that above query will give you desired result. 为此,建议100%使用关键字标记器小写标记过滤器,以便我们可以进行不区分大小写的搜索,上述查询将为您提供所需的结果。 You could add more prefix with OR 您可以使用OR添加更多前缀

2) Another approach could be with keyword tokenizer mixed with lowercase and edgengram token filter . 2)另一种方法是将keyword tokenizerlowercaseedgengram token filter混合使用。 Lets say you use min_gram : 2 and max_gram : 7 then you could use terms query itself act like multiple prefix query like this 假设您使用min_gram : 2max_gram : 7那么您可以使用terms查询本身像这样的多前缀查询

"filter": {
    "terms": {
        "user": ["kim", "elast"]
    }
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM