索引文檔:
對於員工目錄,我們將做如下操作:
實踐中這非常簡單(盡管看起來有很多步驟),我們可以通過一條命令完成所有這些動作:
PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] }
注意,路徑 /megacorp/employee/1
包含了三部分的信息:
-
megacorp
索引名稱 -
employee
類型名稱 -
1
特定雇員的ID
檢索文檔:
這在 Elasticsearch 中很簡單。簡單地執行 一個 HTTP GET
請求並指定文檔的地址——索引庫、類型和ID。 使用這三個信息可以返回原始的 JSON 文檔:
GET /megacorp/employee/1
返回結果包含了文檔的一些元數據,以及 _source
屬性,內容是 John Smith 雇員的原始 JSON 文檔:
{ "_index" : "megacorp", "_type" : "employee", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } }
輕量搜索:
GET /megacorp/employee/_search
可以看到,我們仍然使用索引庫 megacorp
以及類型 employee`,但與指定一個文檔 ID 不同,這次使用 `_search
。返回結果包括了所有三個文檔,放在數組 hits
中。一個搜索默認返回十條結果。

{ "took": 6, "timed_out": false, "_shards": { ... }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "3", "_score": 1, "_source": { "first_name": "Douglas", "last_name": "Fir", "age": 35, "about": "I like to build cabinets", "interests": [ "forestry" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 1, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 1, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ] } }
接下來,嘗試下搜索姓氏為 ``Smith`` 的雇員。為此,我們將使用一個 高亮 搜索,很容易通過命令行完成。這個方法一般涉及到一個 查詢字符串 (_query-string_) 搜索,因為我們通過一個URL參數來傳遞查詢信息給搜索接口:
GET /megacorp/employee/_search?q=last_name:Smith
curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"

{ ... "hits": { "total": 2, "max_score": 0.30685282, "hits": [ { ... "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }, { ... "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ] } }
現在嘗試下更復雜的搜索。 同樣搜索姓氏為 Smith 的員工,但這次我們只需要年齡大於 30 的。查詢需要稍作調整,使用過濾器 filter ,它支持高效地執行一個結構化查詢。
GET /megacorp/employee/_search { "query" : { "bool": { "must": { "match" : { "last_name" : "smith" } }, "filter": { "range" : { "age" : { "gt" : 30 } } } } } }
全文搜索:
截止目前的搜索相對都很簡單:單個姓名,通過年齡過濾。現在嘗試下稍微高級點兒的全文搜索——一項 傳統數據庫確實很難搞定的任務。
搜索下所有喜歡攀岩(rock climbing)的員工:
GET /megacorp/employee/_search { "query" : { "match" : { "about" : "rock climbing" } } }
顯然我們依舊使用之前的 match
查詢在about
屬性上搜索 “rock climbing” 。得到兩個匹配的文檔:

{ ... "hits": { "total": 2, "max_score": 0.16273327, "hits": [ { ... "_score": 0.16273327, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }, { ... "_score": 0.016878016, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ] } }
Elasticsearch 默認按照相關性得分排序,即每個文檔跟查詢的匹配程度。第一個最高得分的結果很明顯:John Smith 的 about
屬性清楚地寫着 “rock climbing” 。
短語搜索:
找出一個屬性中的獨立單詞是沒有問題的,但有時候想要精確匹配一系列單詞或者短語 。 比如, 我們想執行這樣一個查詢,僅匹配同時包含 “rock” 和 “climbing” ,並且 二者以短語 “rock climbing” 的形式緊挨着的雇員記錄。
為此對 match
查詢稍作調整,使用一個叫做 match_phrase
的查詢:
GET /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } } }
毫無懸念,返回結果僅有 John Smith 的文檔。
高亮搜索:
許多應用都傾向於在每個搜索結果中 高亮 部分文本片段,以便讓用戶知道為何該文檔符合查詢條件。在 Elasticsearch 中檢索出高亮片段也很容易。
再次執行前面的查詢,並增加一個新的 highlight
參數:
GET /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } }

{ ... "hits": { "total": 1, "max_score": 0.23013961, "hits": [ { ... "_score": 0.23013961, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] }, "highlight": { "about": [ "I love to go <em>rock</em> <em>climbing</em>" ] } } ] } }
參考文檔:
Elasticsearch官方教程:https://www.elastic.co/guide/cn/elasticsearch/guide/cn/getting-started.html
SpringData與Elasticsearch結合教程:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.10.RELEASE/reference/html/#elasticsearch.repositories