术语过滤器的弹性搜索连字符问题 [英] Elastic Search Hyphen issue with term filter

查看:38
本文介绍了术语过滤器的弹性搜索连字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下弹性搜索查询,只有一个术语过滤器.我的查询要复杂得多,但我只是想在这里展示这个问题.

I have the following Elastic Search query with only a term filter. My query is much more complex but I am just trying to show the issue here.

{
    "filter": {
            "term": {
                    "field": "update-time"
                }
        }
}

当我将一个带连字符的值传递给过滤器时,我得到的结果为零.但是,如果我在没有不带连字符的值的情况下尝试,我会得到结果.我不确定连字符在这里是否有问题,但我的情况让我相信.

When I pass in a hyphenated value to the filter, I get zero results back. But if I try without an unhyphenated value I get results back. I am not sure if the hyphen is an issue here but my scenario makes me believe so.

有没有办法转义连字符,以便过滤器返回结果?我试过用反斜杠转义连字符,我从 Lucene 论坛上读到了它,但这没有帮助.

Is there a way to escape the hyphen so the filter would return results? I have tried escaping the hyphen with a back slash which I read from the Lucene forums but that didn't help.

此外,如果我将 GUID 值传递到这个字段中,该字段用连字符连接并用花括号括起来,例如 - {ASD23-34SD-DFE1-42FWW},我是否需要小写字母字符,我是否需要也要避开花括号?

Also, if I pass in a GUID value into this field which is hyphenated and surrounded by curly braces, something like - {ASD23-34SD-DFE1-42FWW}, would I need to lower case the alphabet characters and would I need to escape the curly braces too?

谢谢

推荐答案

我猜你的字段被分析了,这是elasticsearch中字符串字段的默认设置.结果,当它被索引时,它没有被索引为一个术语更新时间",而是作为两个术语:更新"和时间".这就是为什么您的术语搜索找不到该术语的原因.如果您的字段始终包含必须按原样完全匹配的值,则最好在映射中将此类字段定义为未分析.您可以通过使用新映射重新创建索引来实现:

I would guess that your field is analyzed, which is default setting for string fields in elasticsearch. As a result, when it indexed it's not indexed as one term "update-time" but instead as 2 terms: "update" and "time". That's why your term search cannot find this term. If your field will always contain values that will have to be matched completely as is, it would be the best to define such field in mapping as not analyzed. You can do it by recreating the index with new mapping:

curl -XPUT http://localhost:9200/your-index -d '{
    "mappings" : {
        "your-type" : {
            "properties" : {
                "field" : { "type": "string", "index" : "not_analyzed" }
            }
        }
    }
}'

curl -XPUT  http://localhost:9200/your-index/your-type/1 -d '{
    "field" : "update-time"
}'

curl -XPOST http://localhost:9200/your-index/your-type/_search -d'{
    "filter": {
        "term": {
                "field": "update-time"
        }
    }
}'

或者,如果您希望根据此字段灵活查找记录,您可以继续分析此字段并改用文本查询:

Alternatively, if you want some flexibility in finding records based on this field, you can keep this field analyzed and use text queries instead:

curl -XPOST http://localhost:9200/your-index/your-type/_search -d'{
    "query": {
        "text": {
                "field": "update-time"
        }
    }
}'

请记住,如果对您的字段进行分析,则只需搜索单词update"或单词time"即可找到此记录.

Please, keep in mind that if your field is analyzed then this record will be found by searching for just word "update" or word "time" as well.

这篇关于术语过滤器的弹性搜索连字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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