简体   繁体   English

带有术语过滤器的弹性搜索连字符问题

[英]Elastic Search Hyphen issue with term filter

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. 我不确定连字符是否是一个问题,但我的情况让我相信。

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. 我试图用Lushne论坛中读到的反斜杠来逃避连字符,但这并没有帮助。

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? 另外,如果我将一个GUID值传入此字段,该字段是连字符并用花括号括起来,比如 - {ASD23-34SD-DFE1-42FWW},我需要小写字母字符,我是否需要将其删除花括号呢?

Thanks 谢谢

I would guess that your field is analyzed, which is default setting for string fields in elasticsearch. 我猜你的字段是分析的,这是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"
        }
    }
}'

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. 请记住,如果您的字段被分析,那么通过搜索单词“update”或单词“time”来找到该记录。

The accepted answer didn't work for me with elastic 6.1. 接受的答案对弹性6.1不起作用。 I solved it using the "keyword" field that elastic provides by default on string fields. 我使用弹性在字符串字段上提供的“关键字”字段解决了它。

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

Based on the answer by @imotov If you're using spring-data-elasticsearch then all you need to do is mark your field as: 基于@imotov的答案如果您使用的是spring-data- elasticsearch,那么您需要做的就是将您的字段标记为:

@Field(type = FieldType.String, index = FieldIndex.not_analyzed)

instead of 代替

@Field(type = FieldType.String)

The problem is you need to drop the index though and re-instantiate it with new mappings. 问题是您需要删除索引并使用新映射重新实例化它。

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

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