全文查詢將在執行以前分析查詢字符串,但詞項級別查詢將按照存儲在倒排索引中的詞項進行精確操做。這些查詢一般用於數字,日期和枚舉等結構化數據,而不是全文本字段。 或者,它們容許您製做低級查詢,並在分析過程以前進行。正則表達式
term查詢用於詞項搜索,前一章已經介紹過這裏再也不重複。數組
term查詢對於查找單個值很是有用,但一般咱們可能想搜索多個值。咱們只要用單個 terms
查詢(注意末尾的 s ), terms
查詢比如是 term
查詢的複數形式(以英語名詞的單複數作比)。性能
以下查詢」title「中包含」河北「,」長生「,」碧桂園「三個詞組。url
GET telegraph/_search { "query": { "terms": { "title": ["河北","長生","碧桂園"] } } }
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "telegraph", "_type": "msg", "_id": "A5etp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "碧桂園集團副主席楊惠妍", "content": "楊惠妍分別於7月10日、11日買入碧桂園1000萬股、1500萬股", "author": "小財注", "pubdate": "2018-07-17T16:12:55" } }, { "_index": "telegraph", "_type": "msg", "_id": "Apetp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "長生生物再次跌停 三機構拋售近1000萬元", "content": "長生生物再次一字跌停,報收19.89元,成交1432萬元", "author": "長生生物", "pubdate": "2018-07-17T10:03:11" } }, { "_index": "telegraph", "_type": "msg", "_id": "BJetp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "河北聚焦十大行業推動國際產能合做", "content": "河北省政府近日出臺積極參與「一帶一路」建設推動國際產能合做實施方案", "author": "財聯社", "pubdate": "2018-07-17T14:14:55" } } ] } }
查找與一個或多個指定詞項匹配的文檔,其中必須匹配的術語數量取決於指定的最小值,應匹配字段或腳本。spa
range查詢用於匹配數值型、日期型或字符串型字段在某一範圍內的文檔。code
上面例子查詢發佈時間「pubdate」在「2018-07-17T12:00:00」和「2018-07-17T16:30:00」之間的文檔數據。regexp
GET telegraph/_search { "query": { "range": { "pubdate": { "gte": "2018-07-17T12:00:00", "lte": "2018-07-17T16:30:00" } } } }
查詢結果orm
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "telegraph", "_type": "msg", "_id": "AZetp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "週五召開董事會會議 審議及批准更新後的一季報", "content": "以審議及批准更新後的2018年第一季度報告", "author": "中興通信", "pubdate": "2018-07-17T12:33:11" } }, { "_index": "telegraph", "_type": "msg", "_id": "A5etp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "碧桂園集團副主席楊惠妍", "content": "楊惠妍分別於7月10日、11日買入碧桂園1000萬股、1500萬股", "author": "小財注", "pubdate": "2018-07-17T16:12:55" } }, { "_index": "telegraph", "_type": "msg", "_id": "BJetp2QBW8hrYY3zGJk7", "_score": 1, "_source": { "title": "河北聚焦十大行業推動國際產能合做", "content": "河北省政府近日出臺積極參與「一帶一路」建設推動國際產能合做實施方案", "author": "財聯社", "pubdate": "2018-07-17T14:14:55" } } ] } }
新建索引添加數據索引
DELETE my_person PUT my_person PUT my_person/stu/1 { "name":"sean", "age":20 } PUT my_person/stu/2 { "name":"sum", "age":25 } PUT my_person/stu/3 { "name":"dean", "age":30 } PUT my_person/stu/4 { "name":"kastel", "age":35 }
查詢「age」範圍在20到30之間的人員ip
GET my_person/_search { "query": { "range": { "age": { "gte": 20, "lte": 30 } } } }
查詢結果
{ "took": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "2", "_score": 1, "_source": { "name": "sum", "age": 25 } }, { "_index": "my_person", "_type": "stu", "_id": "1", "_score": 1, "_source": { "name": "sean", "age": 20 } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "dean", "age": 30 } } ] } }
查詢文檔中的字段至少包含一個非空值。
建立索引添加數據
DELETE my_person PUT my_person PUT my_person/stu/1 { "name":"sean", "hobby":"running" } PUT my_person/stu/2 { "name":"Jhon", "hobby":"" } PUT my_person/stu/3 { "name":"sum", "hobby":["swimming",null] } PUT my_person/stu/4 { "name":"lily", "hobby":[null,null] } PUT my_person/stu/5 { "name":"lucy" }
查詢「hobby」不爲空的文檔
GET my_person/_search { "query": { "exists":{ "field":"hobby" } } }
查詢結果
{ "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "2", "_score": 1, "_source": { "name": "Jhon", "hobby": "" } }, { "_index": "my_person", "_type": "stu", "_id": "1", "_score": 1, "_source": { "name": "sean", "hobby": "running" } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "sum", "hobby": [ "swimming", null ] } } ] } }
匹配說明:
查詢以匹配字符串開頭的文檔,以下查詢」hobby「中以」sw「開頭的文檔
GET my_person/_search { "query": { "prefix": { "hobby": { "value": "sw" } } } }
查詢結果
{ "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "6", "_score": 1, "_source": { "name": "deak", "hobby": "swimming" } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "sum", "hobby": [ "swimming", null ] } } ] } }
通配符查詢,以下查詢hobby匹配」*ing「的文檔
GET my_person/_search { "query": { "wildcard": { "hobby": { "value": "*ing" } } } }
查詢結果
{ "took": 27, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "6", "_score": 1, "_source": { "name": "deak", "hobby": "swimming" } }, { "_index": "my_person", "_type": "stu", "_id": "1", "_score": 1, "_source": { "name": "sean", "hobby": "running" } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "sum", "hobby": [ "swimming", null ] } } ] } }
正則表達式查詢的性能很大程度上取決於所選的正則表達式。 相似.*
的匹配任何內容的正則表達式很是緩慢,而且使用了lookaround正則表達式。 若是能夠的話,請嘗試在正則表達式開始以前使用長前綴。 像.*?+
這樣的通配符匹配器大多會下降性能。大多數正則表達式引擎容許您匹配字符串的任何部分。 若是你想讓正則表達式模式從字符串的開頭開始,或者在字符串的末尾完成,那麼你必須明確地定位它,使用^
表示開始或$
表示結束。
元字符 | 語義 | 說明 | 例子 |
---|---|---|---|
. |
Match any character | The period 「.」 can be used to represent any character 匹配任何一個字符 |
ab. 匹配abc、ab1 |
+ |
One-or-more | The plus sign 「+」 can be used to repeat the preceding shortest pattern once or more times. 加號「+」能夠用來重複上一個最短的模式一次或屢次。 |
「aaabbb」匹配a+b+ |
* |
Zero-or-more | The asterisk 「*」 can be used to match the preceding shortest pattern zero-or-more times. | 「aaabbb」匹配a*b* |
? |
Zero-or-one | The question mark 「?」 makes the preceding shortest pattern optional. It matches zero or one times. | 「aaabbb」匹配aaa?bbbb? |
{m} ,{m,n} |
Min-to-max | Curly brackets 「{}」 can be used to specify a minimum and (optionally) a maximum number of times the preceding shortest pattern can repeat. | 「aaabbb」匹配a{3}b{3}和a{2,4}b{2,4} |
() |
Grouping | Parentheses 「()」 can be used to form sub-patterns. | 「ababab」匹配(ab)+ |
| |
Alternation | The pipe symbol 「|」 acts as an OR operator. | 「aabb」匹配aabb|bbaa |
[] |
Character classes | Ranges of potential characters may be represented as character classes by enclosing them in square brackets 「[]」. A leading ^ negates the character class. | [abc]匹配 ‘a’ or ‘b’ or ‘c’ |
~ |
Complement | The shortest pattern that follows a tilde 「~」 is negated(否認).「ab~cd」的意思是:以a開頭,後跟b,後面跟一個任意長度的字符串,但不是c,以d結尾 | 「abcdef」匹配ab~df或a~(cb)def,不匹配ab~cdef和a~(bc)def |
<> |
Interval間隔 | The interval option enables the use of numeric ranges, enclosed by angle brackets 「<>」. | 「foo80」匹配foo<1-100> |
& |
Intersection | The ampersand 「&」 joins two patterns in a way that both of them have to match. | 「aaabbb」匹配aaa.+&.+bbb |
@ |
Any string | The at sign 「@」 matches any string in its entirety. | @&~(foo.+) 匹配除了以「foo」開頭的字符串 「foo」 |
查詢」hobby「字段值與」sw.+「正則匹配的文檔
GET my_person/_search { "query": { "regexp":{ "hobby":"sw.+" } } }
查詢結果
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "6", "_score": 1, "_source": { "name": "deak", "hobby": "swimming" } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "sum", "hobby": [ "swimming", null ] } } ] } }
模糊查詢
GET telegraph/_search { "query": { "fuzzy": { "title": "十大" } } }
查詢結果
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.99277425, "hits": [ { "_index": "telegraph", "_type": "msg", "_id": "BJetp2QBW8hrYY3zGJk7", "_score": 0.99277425, "_source": { "title": "河北聚焦十大行業推動國際產能合做", "content": "河北省政府近日出臺積極參與「一帶一路」建設推動國際產能合做實施方案", "author": "財聯社", "pubdate": "2018-07-17T14:14:55" } } ] } }
根據跟定的文檔id列表查詢文檔。
GET my_person/_search { "query": { "ids": { "values": ["1","3","5"] } } }
查詢結果
{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "my_person", "_type": "stu", "_id": "5", "_score": 1, "_source": { "name": "lucy" } }, { "_index": "my_person", "_type": "stu", "_id": "1", "_score": 1, "_source": { "name": "sean", "hobby": "running" } }, { "_index": "my_person", "_type": "stu", "_id": "3", "_score": 1, "_source": { "name": "sum", "hobby": [ "swimming", null ] } } ] } }