欢迎投稿

今日深度:

elasticsearch-wildcard、regexp通配符与正则表达式查询,

elasticsearch-wildcard、regexp通配符与正则表达式查询,



通配符与正则表达式查询

假设将邮编作为 not_analyzed 的精确值字段索引,所以可以为其创建索引,如下:

PUT /my_index
{
    "mappings": {
        "address": {
            "properties": {
                "postcode": {
                    "type":  "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

与 prefix 前缀查询的特性类似, wildcard 通配符查询也是一种底层基于词的查询,与前缀查询不同的是它允许指定匹配的正则式。它使用标准的 shell 通配符查询: ? 匹配任意字符, * 匹配 0 或多个字符。

这个查询会匹配包含 W1F 7HW 和 W2F 8HW 的文档:

GET /my_index/address/_search
{
    "query": {
        "wildcard": {
            "postcode": "W?F*HW" (1)
        }
    }
}

设想如果现在只想匹配 W 区域的所有邮编,前缀匹配也会包括以 WC 开头的所有邮编,与通配符匹配碰到的问题类似,如果想匹配只以 W 开始并跟随一个数字的所有邮编, regexp 正则式查询允许写出这样更复杂的模式:

GET /my_index/address/_search
{
    "query": {
        "regexp": {
            "postcode": "W[0-9].+" (1)
        }
    }
}


这也意味着需要同样注意前缀查询存在性能问题,对有很多唯一词的字段执行这些查询可能会消耗非常多的资源,所以要避免使用左通配这样的模式匹配(如: *foo 或 .*foo 这样的正则式)。








www.htsjk.Com true http://www.htsjk.com/Elasticsearch/35878.html NewsArticle elasticsearch-wildcard、regexp通配符与正则表达式查询, 通配符与正则表达式查询 假设将邮编作为  not_analyzed  的精确值字段索引,所以可以为其创建索引,如下: PUT / my_index{ " mappings "...
相关文章
    暂无相关文章
评论暂时关闭