文檔通過 _index、_type、_id 元數據(metadata),確定 URL 唯一
GET /<_index>/<_type>/<_id>
# curl -XPUT 'http://localhost:9200/website/profile/1' -d '{ "name" : "neo", "nickname" : "netkiller", "age" : "35", "message" : "Helloworld !!!" }' # curl -XGET 'http://localhost:9200/website/profile/1?pretty' { "_index" : "website", "_type" : "profile", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name" : "neo", "nickname" : "netkiller", "age" : "35", "message" : "Helloworld !!!" } } # curl -XPUT 'http://localhost:9200/website/blog/1?pretty' -d '{ > "title": "My first blog entry", > "text": "Just trying this out...", > "date": "2014/01/01" > }' { "_index" : "website", "_type" : "blog", "_id" : "1", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true }
後面會詳細講解 PUT與GET的使用方法以及相關參數
通過 PUT 寫入數據
[root@localhost ~]# curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{ > "user" : "kimchy", > "post_date" : "2009-11-15T14:12:12", > "message" : "trying out Elasticsearch" > }' {"_index":"twitter","_type":"tweet","_id":"1","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
使用 UUID 替代 _id, 注意使用UUID 必須使用 POST方式提交,不能使用 PUT。
curl -XPOST 'http://localhost:9200/website/news/?pretty' -d '{ "title": "My first news entry", "text": "Just trying this out..." }' { "_index" : "website", "_type" : "news1", "_id" : "AVY0RJrvJRTrBLpmYzBH", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : true } curl -XGET 'http://localhost:9200/website/news/AVY0RJrvJRTrBLpmYzBH?pretty'
提交後會輸出 "_id" : "AVY0RJrvJRTrBLpmYzBH",查詢時將此放到放到URL中即可。
通過 GET 讀取數據
[root@localhost ~]# curl -XGET 'http://localhost:9200/twitter/tweet/1' {"_index":"twitter","_type":"tweet","_id":"1","_version":1,"found":true,"_source":{ "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }}
只返回 _source 數據,去掉元數據
# curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2/_source?pretty' { "title" : "My first news entry", "text" : "Just trying this out..." }
選擇欄位 _source=title,超過一個欄位使用逗號分隔_source=title,text。
# curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2?_source=title&pretty' { "_index" : "website", "_type" : "news1", "_id" : "AVY0Q4SqdtH0Up0t-WB2", "_version" : 1, "found" : true, "_source" : { "title" : "My first news entry" } } # curl -XGET 'http://localhost:9200/website/news1/AVY0Q4SqdtH0Up0t-WB2?_source=title,text&pretty' { "_index" : "website", "_type" : "news1", "_id" : "AVY0Q4SqdtH0Up0t-WB2", "_version" : 1, "found" : true, "_source" : { "text" : "Just trying this out...", "title" : "My first news entry" } }
[root@localhost elasticsearch]# curl -i -XHEAD http://localhost:9200/website/blog/1 HTTP/1.1 200 OK Content-Type: text/plain; charset=UTF-8 Content-Length: 0 [root@localhost elasticsearch]# curl -i -XHEAD http://localhost:9200/website/blog/100 HTTP/1.1 404 Not Found Content-Type: text/plain; charset=UTF-8 Content-Length: 0
HTTP/1.1 200 OK 表示已經找到你要的數據
HTTP/1.1 404 Not Found 表示數據不存在
刪除 _index
curl -XDELETE http://localhost:9200/information/?pretty
刪除 _mapping
curl -XDELETE http://localhost:9200/information/news/_mapping?pretty
刪除對象
curl -XDELETE http://localhost:9200/information/news/1?pretty