【问题标题】:find substring with special chars in Elastic Search在 Elastic Search 中查找具有特殊字符的子字符串
【发布时间】:2012-09-28 09:37:27
【问题描述】:

我是弹性搜索的新手。我想按子字符串搜索,它由数字和符号(如“/”和“-”)组成。例如,我使用默认设置和一个索引字段创建一个索引:

curl -XPUT "http://localhost:9200/test/" -d ' {
    "mappings" : {
            "properties": {
                    "test_field": {
                            "type": "string"
                    }
            }
    }
} '

然后,我将一些数据添加到我的索引中:

curl -XPOST "http://localhost:9200/test/test_field" -d '{ "test_field" : "14/21-35" }'
curl -XPOST "http://localhost:9200/test/test_field" -d '{ "test_field" : "1/1-35" }'
curl -XPOST "http://localhost:9200/test/test_field" -d '{ "test_field" : "1/2-25" }'

刷新索引后,我执行搜索。所以,我想找到“test_field”以“1/1”开头的数据。我的要求:

curl -X GET "http://localhost:9200/test/_search?pretty=true" -d '{"query":{"query_string":{"query":"1/1*"}}}'

不返回任何命中。如果我删除 star 符号,那么作为回应,我会看到两个点击:“1/1-35”和“1/2-25”。如果我尝试通过 反斜杠 ("1\/1*") 转义 slash 符号,结果分别相同。

当我的查询中有“-”符号时,我必须转义这个 Lucene 特殊字符。所以我发送下一个搜索请求:

curl -X GET "http://localhost:9200/test/_search?pretty=true" -d '{"query":{"query_string":{"query":"*1\-3*"}}}'

并返回解析错误。如果我双转义 ("\\") 减号,那么我没有结果。

当查询包含这些字符时,我不知道搜索的执行情况。也许我做错了什么。

我尝试在我的自定义分析器中使用 nGram 过滤器,但它不符合搜索引擎的要求。

如果有人遇到这个问题,请回答。

【问题讨论】:

    标签: lucene elasticsearch


    【解决方案1】:

    默认analyzer 将在索引时从您的数据中删除所有特殊字符。您可以使用keyword analyzer 或者干脆不在索引时分析您的数据:

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

    【讨论】:

    • 我已经尝试过您的映射,几乎一切正常。但不是所有的。我必须在“test_field”中设置值:“16/31-216”和“15/31-2”。当我搜索“31”时,两者都返回。但是当我搜索“31-2”时,只有“15/31-2”返回。 =(
    • 试试Analyze API 看看你的数据是如何被标记化/过滤的。如果你想做部分匹配,this thread 很好地解释了可能性(包括 prefixQuery VS Edge nGrams)。
    • 在了解了 ES 的工作原理后,我找到了解决方案:我需要 keyword 标记器和 lowercaseNGram 过滤器用于 index_analyzerkeyword 标记器用于 @ 987654331@。感谢您的帮助!
    猜你喜欢
    • 2015-11-28
    • 2021-08-15
    • 2021-01-21
    • 2015-12-30
    • 2019-09-03
    • 2018-11-07
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多