搜索數(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ā)起請求:
- http://localhost:9200/_search... - 搜索所有的Index與Type
- http://localhost:9200/movies/... - 搜索Movies索引下的所有類型
- http://localhost:9200/movies/movie... -僅搜索包含在Movies索引Movie類型下的文檔
響應(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)建新文檔。