Sense
為了方便、直觀的使用es的REST Api,我們可以使用sense。Sense是Chrome瀏覽器的一個插件,使用簡單。
如圖:

Sense安裝:
https://chrome.google.com/webstore/detail/sense/doinijnbnggojdlcjifpdckfokbbfpbo
或者直接去chrome網上應用店搜索安裝。

CRUD
URL的格式:
http://localhost:9200/<index>/<type>/[<id>]
其中index、type是必須提供的。
id是可選的,不提供es會自動生成。
index、type將信息進行分層,利於管理。index可以理解為數據庫,type理解為數據表。
補一張圖(0211)

The type called another_type and the index called another is shown in order to emphasize that Elasticsearch is multi-tenant, by which we mean that a single server can store multiple indexes and multiple types.
上面解釋了“多租戶”!
索引文檔(Create、update)
首先我們塞入一條數據,命令如下:
curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}'
-d代表一個json格式的對象,這里是一部電影數據。
運行結果如下:

通過默認查詢我們可以查詢到剛才添加的數據,如下:

下面我們來修改這條數據,添加電影的類型,如下:
curl -XPUT "http://localhost:9200/movies/movie/1" -d'
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972,
"genres": ["Crime", "Drama"]
}'
再查詢可以發現該數據多了一個字段:genres。

通過id進行查詢(getting by id)
curl -XGET "http://localhost:9200/movies/movie/1" -d''

刪除文檔(deleting document)
curl -XDELETE "http://localhost:9200/movies/movie/1" -d''

再通過getting by id,返回:
檢索(search)
為了檢索我們先多添加幾篇文檔:
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's query DSL
{
"query": {
//Query DSL here
}
}
--基本的文本檢索
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "kill"
}
}
}'
--指定字段進行檢索
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}'
fields默認為"_all" 。
--過濾(filtering)
curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}'
詳細api,參考:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/index.html
部分內容摘抄自:
http://joelabrahamsson.com/elasticsearch-101/
query VS filter
摘錄一張有意思的ppt。

