我的Elasticsearch系列文章,逐漸更新中,歡迎關(guān)注
0A.關(guān)于Elasticsearch及實(shí)例應(yīng)用
03.如何安裝與設(shè)置Elasticsearch API
04.如果通過(guò)elasticsearch的head插件建立索引_CRUD操作
05.Elasticsearch多個(gè)實(shí)例和head plugin使用介紹
06.當(dāng)Elasticsearch進(jìn)行文檔索引時(shí),它是怎樣工作的?
07.Elasticsearch中的映射方式—簡(jiǎn)潔版教程
08.Elasticsearch中的分析和分析器應(yīng)用方式
09.Elasticsearch中構(gòu)建自定義分析器
10.Kibana科普-作為Elasticsearhc開(kāi)發(fā)工具
12.Elasticsearch全文查詢(xún)
另外Elasticsearch入門(mén),我強(qiáng)烈推薦ElasticSearch搭建小白指南給你,非常想盡的入門(mén)指南手冊(cè)。
我們已經(jīng)學(xué)習(xí)了Elasticsearch查詢(xún)的基本分類(lèi),這兩個(gè)類(lèi)別的基本知識(shí)以及查詢(xún)/過(guò)濾器上下文。在此博客中,其目的是向您介紹Elasticsearch世界中常見(jiàn)的全文查詢(xún)。
讓我們索引一些主要由一些文本組成的數(shù)據(jù)。為簡(jiǎn)單起見(jiàn),我采用了Facebook帖子的修剪版本及其說(shuō)明和詳細(xì)信息的CSV,這些內(nèi)容可以在公共網(wǎng)站上獲得。您可以將這些tweet索引到Elasticsearch
我已將上述推文索引到名為fb-post的索引。索引后的樣本數(shù)據(jù)文檔如下所示:
{
? ? ? ? "_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
? ? ? ? }
? ? ? }b
在上面的文檔中,我們感興趣的字段是諸如“名稱(chēng)”,“消息”和“描述”之類(lèi)的文本字段。
現(xiàn)在讓我們一個(gè)接一個(gè)地轉(zhuǎn)到每個(gè)全文查詢(xún)。
1.匹配查詢(xún)
我們?cè)谥暗牟┛椭杏懻摿似ヅ洳樵?xún),但是沒(méi)有提到匹配查詢(xún)的正常用例。匹配查詢(xún)最常見(jiàn)的用例是當(dāng)我們擁有大量數(shù)據(jù)集時(shí),我們需要快速找到一些近似精確的匹配項(xiàng)。
例如,在我們的Twitter數(shù)據(jù)集中,我們需要確定整個(gè)推文集中是否存在“信心”一詞??梢允褂冕槍?duì)以下“文本”字段的簡(jiǎn)單匹配查詢(xún)來(lái)完成此操作:
POST fb-post/_search
{
? "query": {
? ? "match": {
? ? ? "description": {
? ? ? ? "query":"confidence"
? ? ? }
? ? }
? }
}
結(jié)果將顯示帶有“ confidence”文本的推文。
現(xiàn)在在上面的示例中,我們只看到了一個(gè)單詞。當(dāng)我們輸入多個(gè)單詞時(shí)會(huì)發(fā)生什么?讓我們嘗試下面的查詢(xún),這里我們要給出的查詢(xún)是“?信心大廈?”
POST fb-post/_search
{
? "query": {
? ? "match": {
? ? ? "description": {
? ? ? ? "query":"confidence buildings"
? ? ? }
? ? }
? }
}
現(xiàn)在,這將返回匹配“信心”?或?“建筑物”的文檔。匹配查詢(xún)的默認(rèn)行為為OR。這可以更改。如果我們要同時(shí)匹配“信心”?和“建筑物”,則可以在查詢(xún)中指定“ operator”參數(shù),如下所示:
POST fb-post/_search
{
? "query": {
? ? "match": {
? ? ? "description": {
? ? ? ? "query":"confidence buildings",
? ? ? ? "operator":"AND"
? ? ? }
? ? }
? }
}上面的查詢(xún)將返回包含“信心”和“建筑物”(在我們的數(shù)據(jù)集中為零)的文檔
2.多重比對(duì)查詢(xún)
顧名思義,多匹配查詢(xún)將在多個(gè)字段中搜索搜索關(guān)鍵字。假設(shè)我們有一個(gè)搜索關(guān)鍵字“ Giffords family”,可以在“名稱(chēng)”和“描述”字段中進(jìn)行搜索,則可以使用多重匹配查詢(xún)。
POST fb-post/_search
{
? "query": {
? ? "multi_match" : {
? ? ? "query":? ? "Giffords family",
? ? ? "fields": [ "name", "description" ]
? ? }
? }
}
在此處,針對(duì)“名稱(chēng)”和“描述”字段搜索“ Giffords”或“ family”一詞,并返回匹配的文檔。
我們還可以針對(duì)特定字段進(jìn)行自定義評(píng)分。在下面的查詢(xún)中,對(duì)所有與“名稱(chēng)”字段中的關(guān)鍵字匹配的文檔給予5的提升
POST fb-post/_search
{
? "query": {
? ? "multi_match" : {
? ? ? "query":? ? "Giffords family",
? ? ? "fields": [ "name^5", "description" ]
? ? }
? }
}
3. query_string查詢(xún)
另一個(gè)有用的查詢(xún)是query_string查詢(xún)。它與匹配查詢(xún)類(lèi)似,但此處搜索關(guān)鍵字的格式很重要。它需要特定的格式,并且如果搜索關(guān)鍵字的格式不同,則會(huì)返回錯(cuò)誤。
考慮以下查詢(xún):
POST fb-post/_search
{
? ? "query": {
? ? ? ? "query_string" : {
? ? ? ? ? ? "query" : "(step down) OR (official act)"
? ? ? ? }
? ? }
}
在此,搜索關(guān)鍵字首先分為兩部分,即“或”條件的左側(cè)和“或”條件的右側(cè)。也就是說(shuō),搜索查詢(xún)中的運(yùn)算符用作定界符。然后將對(duì)每個(gè)部分進(jìn)行分析(根據(jù)要查詢(xún)的字段,在上面的示例中查詢(xún)所有字段,它將進(jìn)行標(biāo)準(zhǔn)分析),然后進(jìn)行查詢(xún)。
也可以對(duì)特定的一個(gè)或多個(gè)字段進(jìn)行查詢(xún),如下所示:
POST fb-post/_search
{
? ? "query": {
? ? ? ? "query_string" : {
? ? ? ? ? ? "query" : "(step down) OR (official act)",
? ? ? ? ? ? "fields" : ["description","name"]
? ? ? ? }
? ? }
}
4. match_phrase查詢(xún)
Match_phrase查詢(xún)是一個(gè)特別有用的查詢(xún),它尋找匹配短語(yǔ)而不是單個(gè)單詞。在下面給出的示例中,match_phrase查詢(xún)以相同順序獲取與單詞“ deeply關(guān)心”匹配的文檔。
POST fb-post / _search
{
? ? “ query”:{
? ? ? ? “ match_phrase”:{
? ? ? ? ? ? “ description”:“ 密切關(guān)注 ”
? ? ? ? }
? ? }
}即使更改了單詞順序,match_phrase查詢(xún)的一個(gè)非常有用的自定義設(shè)置也會(huì)匹配。例如,如果我們希望“深切關(guān)注”和“深切關(guān)注”相匹配,則可以將slop參數(shù)與match_phrase查詢(xún)一起使用,如下所示:
POST fb-post/_search
{
? ? "query": {
? ? ? ? "match_phrase" : {
? ? ? ? ? ? "description" : "deeply concerned"
? ? ? ? }
? ? }
}
slope值默認(rèn)為0,最大范圍為50。在上面的示例中,slope值2表示可以將這些詞視為匹配項(xiàng)的范圍。
現(xiàn)在考慮以下查詢(xún),在該查詢(xún)的末尾加上不完整的關(guān)鍵字“ ab”。該match_phrase查詢(xún)沒(méi)有提供火柴,即使存在具有“深切關(guān)注文檔此查詢(xún)有關(guān)?”?短語(yǔ)中的“描述”字段
POST fb-post/_search
{
? ? "query": {
? ? ? ? "match_phrase": {
? ? ? ? ? ? "description" : {
? ? ? ? ? ? ? ? "query" : "deeply concerned",
? ? ? ? ? ? ? ? "slop": 2
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
5. match_phrase_prefix查詢(xún)
在上面的示例中,我們看到match_phrase查詢(xún)需要精確的短語(yǔ)來(lái)進(jìn)行匹配。但是有時(shí)候,如果我們也可以使用match_phrase_prefix查詢(xún)來(lái)匹??配部分匹配項(xiàng),那將很方便。“ match_phrase_prefix”查詢(xún)可幫助我們實(shí)現(xiàn)此類(lèi)匹配。
POST fb-post/_search
{
? ? "query": {
? ? ? ? "match_phrase" : {
? ? ? ? ? ? "description" : "deeply concerned ab"
? ? ? ? }
? ? }
}
上面的查詢(xún)可以像下面搭配詞組:
“deeply concerned about”
“deeply concerned above”
一個(gè)實(shí)際的用例是郵政編碼的自動(dòng)完成實(shí)現(xiàn),其中用戶(hù)鍵入部分短語(yǔ)。
結(jié)論
在此博客中,我們看到了Elasticsearch查詢(xún)世界中的一些重要的全文查詢(xún)。我將在下一個(gè)博客中介紹術(shù)語(yǔ)級(jí)別查詢(xún),然后再返回一些特殊的全文查詢(xún),這將有助于更好地理解。