Elasticsearch【正則搜索】分析&實踐


在ES中有很多使用不是很頻繁的查詢,可以達到一些特殊的效果。比如基於行為路徑的漏斗模型。本篇就從使用上講述一下正則表達式查詢的用法。

Regexp Query

regexp允許使用正則表達式進行term查詢.注意regexp如果使用不正確,會給服務器帶來很嚴重的性能壓力。比如.*開頭的查詢,將會匹配所有的倒排索引中的關鍵字,這幾乎相當於全表掃描,會很慢。因此如果可以的話,最好在使用正則前,加上匹配的前綴。在正則中如果使用.*?或者+都會降低查詢的性能。

注意:是term查詢,也就是說這個查詢不能跨term。

舉個簡單的例子:

GET /_search
{
    "query": {
        "regexp":{
            "name.first": "s.*y"
        }
    }
}

正則支持的一些標准的用法:

搜索關鍵詞的一部分

如果給定的term是abcde

ab.* 可以匹配
abcd 不可以匹配

也支持使用^或者$來指定開頭或者結尾。

允許特殊字符

一些特殊字符是需要轉義的,比如:

. ? + * | { } [ ] ( ) " \ # @ & < >  ~

如果想要搜索某個固定的詞,也可以加上雙引號。

匹配任何字符

.可以匹配任意字符,比如

ab... 
a.c.e

這幾個都可以匹配abcde

匹配一個或者多個

使用+表示匹配一個或者多個字符

a+b+        # match
aa+bb+      # match
a+.+        # match
aa+bbb+     # match

上面這些都可以匹配aaabbb

匹配零個或者多個

a*b*        # match
a*b*c*      # match
.*bbb.*     # match
aaa*bbb*    # match

上面這些都可以匹配aaabbb

匹配另個或者一個

aaa?bbb?    # match
aaaa?bbbb?  # match
.....?.?    # match
aa?bb?      # no match

上面這些都可以匹配aaabbb

支持匹配次數

使用{}支持匹配指定的最小值和最大值區間

{5}     # repeat exactly 5 times
{2,5}   # repeat at least twice and at most 5 times
{2,}    # repeat at least twice

比如對於字符串:

a{3}b{3}        # match
a{2,4}b{2,4}    # match
a{2,}b{2,}      # match
.{3}.{3}        # match
a{4}b{4}        # no match
a{4,6}b{4,6}    # no match
a{4,}b{4,}      # no match

捕獲組

對於字符串ababab

(ab)+       # match
ab(ab)+     # match
(..)+       # match
(...)+      # no match
(ab)*       # match
abab(ab)?   # match
ab(ab)?     # no match
(ab){3}     # match
(ab){1,2}   # no match

選擇運算符

支持或操作的匹配,注意這里默認都是最長匹配的。

aabb|bbaa   # match
aacc|bb     # no match
aa(cc|bb)   # match
a+|b+       # no match
a+b+|b+a+   # match
a+(b|c)+    # match

字符匹配

支持在[]中進行字符匹配,^代表非的意思

[abc]   # 'a' or 'b' or 'c'
[a-c]   # 'a' or 'b' or 'c'
[-abc]  # '-' or 'a' or 'b' or 'c'
[abc\-] # '-' or 'a' or 'b' or 'c'
[^abc]  # any character except 'a' or 'b' or 'c'
[^a-c]  # any character except 'a' or 'b' or 'c'
[^-abc]  # any character except '-' or 'a' or 'b' or 'c'
[^abc\-] # any character except '-' or 'a' or 'b' or 'c'

其中-代表的范圍匹配。

可選的匹配符

在正則表達式中也支持一些特殊的操作符,可以使用flags字段控制是否開啟。

Complement

這個表示正則表示匹配一段字符串,比如ab~cd意思是:a開頭,后面是b,然后是一堆非c的字符串,最后以d結尾。比如字符串abcdef

ab~df     # match
ab~cf     # match
ab~cdef   # no match
a~(cb)def # match
a~(bc)def # no match

Interval

interval選項支持數值的范圍,比如字符串foo80:

foo<1-100>     # match
foo<01-100>    # match
foo<001-100>   # no match

Intersection

使用&可以實現多個匹配的連接,比如字符串aaabbb

aaa.+&.+bbb     # match
aaa&bbb         # no match

Any

使用@,可以匹配任意的字符串

實踐

首先創建索引:
PUT test

然后創建映射:
PUT test/_mapping/test
{
  "properties": {
    "a": {
      "type": "string",
      "index":"not_analyzed" 
    },
    "b":{
      "type":"string"
    }
  }
}

添加一條數據:
PUT test/test/1
{
  "a":"a,b,c","b":"a,b,c"
}

先來分析一下,a,b,c被默認分析成了什么?

POST test/_analyze
{
  "analyzer": "standard",
  "text": "a,b,c"
}

返回內容:

{
  "tokens": [
    {
      "token": "a",
      "start_offset": 0,
      "end_offset": 1,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "b",
      "start_offset": 2,
      "end_offset": 3,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "c",
      "start_offset": 4,
      "end_offset": 5,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

然后查詢一下:

POST /test/test/_search?pretty
{
  "query":{
    "regexp":{
        "a": "a.*b.*"
    }
  }
}

返回

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "a": "a,b,c",
          "b": "a,b,c"
        }
      }
    ]
  }
}

再換成b字段試試:

POST /test/test/_search?pretty
{
  "query":{
    "regexp":{
        "b": "a.*b.*"
    }
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

這是為什么呢?

因為整個regexp查詢是應用到一個詞上的,針對某個詞,搜索a.*b.*,a字段由於不分詞,它的詞是整個的a.b.c;b字段經過分詞,他的詞是abc三個獨立的詞,因此針對a字段的正則搜索可以查詢到結果;但是針對b字段卻搜索不到。

歸納起來,還是需要好好理解分詞在搜索引擎中的作用才行。

參考

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM