個人Elasticsearch系列文章,逐漸更新中,歡迎關注
0A.關於Elasticsearch及實例應用
00.Solr與ElasticSearch對比
01.ElasticSearch能作什麼?
02.Elastic Stack功能介紹
03.如何安裝與設置Elasticsearch API
04.若是經過elasticsearch的head插件創建索引_CRUD操做
05.Elasticsearch多個實例和head plugin使用介紹
06.當Elasticsearch進行文檔索引時,它是如何工做的?
07.Elasticsearch中的映射方式—簡潔版教程
08.Elasticsearch中的分析和分析器應用方式
09.Elasticsearch中構建自定義分析器
10.Kibana科普-做爲Elasticsearhc開發工具
11.Elasticsearch查詢方法
12.Elasticsearch全文查詢php
另外Elasticsearch入門,我強烈推薦ElasticSearch搭建小白指南給你,很是想盡的入門指南手冊。html
咱們已經學習了Elasticsearch查詢的基本分類,這兩個類別的基本知識以及查詢/過濾器上下文。在此博客中,其目的是向您介紹Elasticsearch世界中常見的全文查詢。
讓咱們索引一些主要由一些文本組成的數據。爲簡單起見,我採用了Facebook帖子的修剪版本及其說明和詳細信息的CSV,這些內容能夠在公共網站上得到。您能夠將這些tweet索引到Elasticsearch
我已將上述推文索引到名爲fb-post的索引。索引後的樣本數據文檔以下所示:segmentfault
{ "_index" : "fb-post", "_type" : "_doc", "_id" : "TszxwG0Bm6hFGbtHjVCC", "_score" : 1.0, "_source" : { "status_type" : "shared_story", "link" : "http://abcnews.go.com/blogs/headlines/2011/12/chief-justice-roberts-responds-to-judicial-ethics-critics/", "description" : "PAUL J. RICHARDS/AFP/Getty Images Chief Justice John Roberts issued a ringing endorsement Saturday night of his colleagues’ ability to determine when they should step down from a case because of a conflict of interest. 「I have complete confidence in the capability of my colleagues to determine when ...", "caption" : "abcnews.go.com", "love_count" : 0, "shares_count" : 12, "page_id" : 86680728811, "wow_count" : 0, "post_type" : "link", "id" : "86680728811_272953252761568", "posted_at" : "2012-01-01 00:30:26", "sad_count" : 0, "angry_count" : 0, "message" : "Roberts took the unusual step of devoting the majority of his annual report to the issue of judicial ethics.", "picture" : "https://external.xx.fbcdn.net/safe_image.php?d=AQAPXteeHLT2K7Rb&w=130&h=130&url=http%3A%2F%2Fabcnews.go.com%2Fimages%2FPolitics%2Fgty_chief_justice_john_roberts_jt_111231_wblog.jpg&cfs=1&sx=108&sy=0&sw=269&sh=269", "likes_count" : 61, "thankful_count" : 0, "@timestamp" : "2012-01-01T00:30:26.000+05:30", "comments_count" : 27, "name" : "Chief Justice Roberts Responds to Judicial Ethics Critics", "haha_count" : 0 } }
在上面的文檔中,咱們感興趣的字段是諸如「名稱」,「消息」和「描述」之類的文本字段。
如今讓咱們一個接一個地轉到每一個全文查詢。
1.匹配查詢
咱們在以前的博客中討論了匹配查詢,可是沒有提到匹配查詢的正經常使用例。匹配查詢最多見的用例是當咱們擁有大量數據集時,咱們須要快速找到一些近似精確的匹配項。
例如,在咱們的Twitter數據集中,咱們須要肯定整個推文集中是否存在「信心」一詞。可使用針對如下「文本」字段的簡單匹配查詢來完成此操做:elasticsearch
POST fb-post/_search { "query": { "match": { "description": { "query":"confidence" } } } }
結果將顯示帶有「 confidence」文本的推文。
如今在上面的示例中,咱們只看到了一個單詞。當咱們輸入多個單詞時會發生什麼?讓咱們嘗試下面的查詢,這裏咱們要給出的查詢是「 信心大廈 」ide
POST fb-post/_search { "query": { "match": { "description": { "query":"confidence buildings" } } } }
如今,這將返回匹配「信心」 或 「建築物」的文檔。匹配查詢的默認行爲爲OR。這能夠更改。若是咱們要同時匹配「信心」 和「建築物」,則能夠在查詢中指定「 operator」參數,以下所示:
POST fb-post/_search工具
{ "query": { "match": { "description": { "query":"confidence buildings", "operator":"AND" } } } }
上面的查詢將返回包含「信心」和「建築物」(在咱們的數據集中爲零)的文檔
2.多重比對查詢
顧名思義,多匹配查詢將在多個字段中搜索搜索關鍵字。假設咱們有一個搜索關鍵字「 Giffords family」,能夠在「名稱」和「描述」字段中進行搜索,則可使用多重匹配查詢。post
POST fb-post/_search { "query": { "multi_match" : { "query": "Giffords family", "fields": [ "name", "description" ] } } }
在此處,針對「名稱」和「描述」字段搜索「 Giffords」或「 family」一詞,並返回匹配的文檔。
咱們還能夠針對特定字段進行自定義評分。在下面的查詢中,對全部與「名稱」字段中的關鍵字匹配的文檔給予5的提高學習
POST fb-post/_search { "query": { "multi_match" : { "query": "Giffords family", "fields": [ "name^5", "description" ] } } }
另外一個有用的查詢是query_string查詢。它與匹配查詢相似,但此處搜索關鍵字的格式很重要。它須要特定的格式,而且若是搜索關鍵字的格式不一樣,則會返回錯誤。
考慮如下查詢:開發工具
POST fb-post/_search { "query": { "query_string" : { "query" : "(step down) OR (official act)" } } }
在此,搜索關鍵字首先分爲兩部分,即「或」條件的左側和「或」條件的右側。也就是說,搜索查詢中的運算符用做定界符。而後將對每一個部分進行分析(根據要查詢的字段,在上面的示例中查詢全部字段,它將進行標準分析),而後進行查詢。
也能夠對特定的一個或多個字段進行查詢,以下所示:網站
POST fb-post/_search { "query": { "query_string" : { "query" : "(step down) OR (official act)", "fields" : ["description","name"] } } }
Match_phrase查詢是一個特別有用的查詢,它尋找匹配短語而不是單個單詞。在下面給出的示例中,match_phrase查詢以相同順序獲取與單詞「 deeply關心」匹配的文檔。
POST fb-post / _search { 「 query」:{ 「 match_phrase」:{ 「 description」:「 密切關注 」 } } }
即便更改了單詞順序,match_phrase查詢的一個很是有用的自定義設置也會匹配。例如,若是咱們但願「深切關注」和「深切關注」相匹配,則能夠將slop參數與match_phrase查詢一塊兒使用,以下所示:
POST fb-post/_search { "query": { "match_phrase" : { "description" : "deeply concerned" } } }
slope值默認爲0,最大範圍爲50。在上面的示例中,slope值2表示能夠將這些詞視爲匹配項的範圍。
如今考慮如下查詢,在該查詢的末尾加上不完整的關鍵字「 ab」。該match_phrase查詢沒有提供火柴,即便存在具備「深切關注文檔此查詢有關 」 短語中的「描述」字段
POST fb-post/_search { "query": { "match_phrase": { "description" : { "query" : "deeply concerned", "slop": 2 } } } }
在上面的示例中,咱們看到match_phrase查詢須要精確的短語來進行匹配。可是有時候,若是咱們也可使用match_phrase_prefix查詢來匹配部分匹配項,那將很方便。「 match_phrase_prefix」查詢可幫助咱們實現此類匹配。
POST fb-post/_search { "query": { "match_phrase" : { "description" : "deeply concerned ab" } } }
上面的查詢能夠像下面搭配詞組:
「deeply concerned about」
「deeply concerned above」
一個實際的用例是郵政編碼的自動完成實現,其中用戶鍵入部分短語。
結論在此博客中,咱們看到了Elasticsearch查詢世界中的一些重要的全文查詢。我將在下一個博客中介紹術語級別查詢,而後再返回一些特殊的全文查詢,這將有助於更好地理解。