Elasticsearch 搜索

搜索數(shù)據(jù)建立

ElasticSearch最誘人的地方即是為我們提供了方便快捷的搜索功能延曙,我們首先嘗試使用如下的命令創(chuàng)建測試文檔:

curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}'

curl -XPUT "http://localhost:9200/movies/movie/2" -d'
{
    "title": "Lawrence of Arabia",
    "director": "David Lean",
    "year": 1962,
    "genres": ["Adventure", "Biography", "Drama"]
}'

curl -XPUT "http://localhost:9200/movies/movie/3" -d'
{
    "title": "To Kill a Mockingbird",
    "director": "Robert Mulligan",
    "year": 1962,
    "genres": ["Crime", "Drama", "Mystery"]
}'

curl -XPUT "http://localhost:9200/movies/movie/4" -d'
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War"]
}'

curl -XPUT "http://localhost:9200/movies/movie/5" -d'
{
    "title": "Kill Bill: Vol. 1",
    "director": "Quentin Tarantino",
    "year": 2003,
    "genres": ["Action", "Crime", "Thriller"]
}'

curl -XPUT "http://localhost:9200/movies/movie/6" -d'
{
    "title": "The Assassination of Jesse James by the Coward Robert Ford",
    "director": "Andrew Dominik",
    "year": 2007,
    "genres": ["Biography", "Crime", "Drama"]
}'

這里需要了解的是寂殉,ElasticSearch為我們提供了通用的_bulk端點來在單請求中完成多文檔創(chuàng)建操作刽锤,不過這里為了簡單起見還是分為了多個請求進行執(zhí)行。

ElasticSearch中搜索主要是基于_search這個端點進行的幻枉,其標準請求格式為:<index>/<type>/_search</type></index>询兴,其中index與type都是可選的。
換言之诽表,我們可以以如下幾種方式發(fā)起請求:

響應(yīng)內(nèi)容會包含文檔的元信息唉锌,文檔的原始數(shù)據(jù)存在 _source 字段中。

檢索某個文檔
我們也可以直接檢索出文檔的 _source 字段竿奏,如下:

curl -XGET 'http://localhost:9200/movies/movie/1/_source'

返回的結(jié)果:

{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}

檢索所有文檔
我們可以使用 _search 這個 API 檢索出所有的文檔袄简,命令如下:

curl -XGET 'http://localhost:9200/movies/movie/_search'

返回的結(jié)果:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 1,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "title": "Kill Bill: Vol. 1",
                    "director": "Quentin Tarantino",
                    "year": 2003,
                    "genres": [
                        "Action",
                        "Crime",
                        "Thriller"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "Lawrence of Arabia",
                    "director": "David Lean",
                    "year": 1962,
                    "genres": [
                        "Adventure",
                        "Biography",
                        "Drama"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "Apocalypse Now",
                    "director": "Francis Ford Coppola",
                    "year": 1979,
                    "genres": [
                        "Drama",
                        "War"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "title": "The Assassination of Jesse James by the Coward Robert Ford",
                    "director": "Andrew Dominik",
                    "year": 2007,
                    "genres": [
                        "Biography",
                        "Crime",
                        "Drama"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "The Godfather",
                    "director": "Francis Ford Coppola",
                    "year": 1972,
                    "genres": [
                        "Crime",
                        "Drama"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "title": "To Kill a Mockingbird",
                    "director": "Robert Mulligan",
                    "year": 1962,
                    "genres": [
                        "Crime",
                        "Drama",
                        "Mystery"
                    ]
                }
            }
        ]
    }
}

可以看到,hits 這個 object 包含了 total泛啸,hits 數(shù)組等字段绿语,其中,hits 數(shù)組包含了所有的文檔,這里只有兩個文檔吕粹,total 表明了文檔的數(shù)量种柑,默認情況下會返回前 10 個結(jié)果。我們也可以設(shè)定 From/Size 參數(shù)來獲取某一范圍的文檔匹耕,可參考這里聚请,比如:

curl -XGET 'http://localhost:9200/movies/movie/_search?from=1&size=2'

返回的結(jié)果如下:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 1,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "title": "Lawrence of Arabia",
                    "director": "David Lean",
                    "year": 1962,
                    "genres": [
                        "Adventure",
                        "Biography",
                        "Drama"
                    ]
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "title": "Apocalypse Now",
                    "director": "Francis Ford Coppola",
                    "year": 1979,
                    "genres": [
                        "Drama",
                        "War"
                    ]
                }
            }
        ]
    }
}

檢索某些字段

有時候,我們只需檢索文檔的個別字段稳其,這時可以使用 _source 參數(shù)驶赏,多個字段可以使用逗號分隔,如下所示:

curl -XGET 'http://localhost:9200/movies/movie/1?_source=title,director'

返回的結(jié)果:

{
    "_index": "movies",
    "_type": "movie",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "director": "Francis Ford Coppola",
        "title": "The Godfather"
    }
}

query string 搜索
query string 搜索以 q=field:value 的形式進行查詢既鞠,比如查詢 title 字段含有 godfather 的電影:

curl -XGET 'http://localhost:9200/movies/movie/_search?q=title:godfather'

返回的結(jié)果:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.25811607,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "1",
                "_score": 0.25811607,
                "_source": {
                    "title": "The Godfather",
                    "director": "Francis Ford Coppola",
                    "year": 1972,
                    "genres": [
                        "Crime",
                        "Drama"
                    ]
                }
            }
        ]
    }
}

DSL 搜索
上面的 query string 搜索比較輕量級煤傍,只適用于簡單的場合。Elasticsearch 提供了更為強大的 DSL(Domain Specific Language)查詢語言嘱蛋,適用于復(fù)雜的搜索場景蚯姆,比如全文搜索。我們可以將上面的 query string 搜索轉(zhuǎn)換為 DSL 搜索浑槽,如下:

GET /movies/movie/_search
{
    "query" : {
        "match" : {
            "title" : "godfather"
        }
    }
}

使用 curl請求:

curl -X GET "127.0.0.1:9200/movies/movie/_search" -d '{"query": {"match": {"title": "godfather"}}}'

最簡單的查詢請求即是全文檢索蒋失,譬如我們這里需要搜索關(guān)鍵字:godfather:

搜索包含“godfather”的關(guān)鍵字:

curl -XPOST "http://localhost:9200/_search" -d'
{
    "query": {
        "query_string": {
            "query": "godfather",
        }
    }
}'

在title中搜索包含“godfather”的關(guān)鍵字

curl -XPOST "http://localhost:9200/_search" -d'
{
    "query": {
        "query_string": {
            "query": "godfather",
            "fields": ["title"]
        }
    }
}'

返回的結(jié)果:

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 25,
        "successful": 25,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.25811607,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "1",
                "_score": 0.25811607,
                "_source": {
                    "title": "The Godfather",
                    "director": "Francis Ford Coppola",
                    "year": 1972,
                    "genres": [
                        "Crime",
                        "Drama"
                    ]
                }
            }
        ]
    }
}

檢查文檔是否存在
如果你想做的只是檢查文檔是否存在——你對內(nèi)容完全不感興趣——使用HEAD方法來代替GET。HEAD請求不會返回響應(yīng)體桐玻,只有HTTP頭:

curl -i -XHEAD "http://localhost:9200/movies/movie/3"

Elasticsearch將會返回200 OK狀態(tài)如果你的文檔存在:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 255

如果不存在返回404 Not Found:

curl -i -XHEAD "http://localhost:9200/movies/movie/36"
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 60

當然,這只表示你在查詢的那一刻文檔不存在荆萤,但并不表示幾毫秒后依舊不存在镊靴。另一個進程在這期間可能創(chuàng)建新文檔。

參考:
ElasticSearch 2.x 入門與快速實踐
Elasticsearch 入門使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末链韭,一起剝皮案震驚了整個濱河市偏竟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌敞峭,老刑警劉巖踊谋,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異旋讹,居然都是意外死亡殖蚕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門沉迹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來睦疫,“玉大人,你說我怎么就攤上這事鞭呕「蛴” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長瓦糕。 經(jīng)常有香客問我底洗,道長,這世上最難降的妖魔是什么咕娄? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任枷恕,我火速辦了婚禮,結(jié)果婚禮上谭胚,老公的妹妹穿的比我還像新娘徐块。我一直安慰自己,他們只是感情好灾而,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布胡控。 她就那樣靜靜地躺著,像睡著了一般旁趟。 火紅的嫁衣襯著肌膚如雪昼激。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天锡搜,我揣著相機與錄音橙困,去河邊找鬼。 笑死耕餐,一個胖子當著我的面吹牛凡傅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肠缔,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼夏跷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了明未?” 一聲冷哼從身側(cè)響起槽华,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎趟妥,沒想到半個月后猫态,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡披摄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年亲雪,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片行疏。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡匆光,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出酿联,到底是詐尸還是另有隱情终息,我是刑警寧澤夺巩,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站周崭,受9級特大地震影響柳譬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜续镇,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一美澳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摸航,春花似錦制跟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至读串,卻和暖如春聊记,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背恢暖。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工排监, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人杰捂。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓舆床,卻偏偏與公主長得像,于是被迫代替她去往敵國和親琼娘。 傳聞我的和親對象是個殘疾皇子峭弟,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容