Elasticsearch 急速入門·文件與搜尋
阿新 • • 發佈:2022-05-02
節選自《Netkiller Database 手札》
60.2. 文件API
60.2.1. 快速上手
文件通過 _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的使用方法以及相關引數
60.2.2. 寫入 PUT/POST
通過 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中即可。
60.2.3. 獲取 GET
通過 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"
}}
60.2.3.1. _source
只返回 _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"
}
}
60.2.4. 檢查記錄是否存在
[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 表示資料不存在
60.2.5. 引數
60.2.5.1. pretty 格式化 json
# curl -XGET 'http://localhost:9200/twitter/tweet/1?pretty'
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elasticsearch"
}
}
60.3. 搜尋
搜尋所有內容
# curl -XGET 'http://localhost:9200/_search?pretty'
# curl -XGET 'http://localhost:9200/_all/_search?pretty'
指定 _index 搜尋
# curl -XGET 'http://localhost:9200/website/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news/_search?pretty'
指定 _type 搜尋
# curl -XGET 'http://localhost:9200/website,twitter/_search?pretty'
# curl -XGET 'http://localhost:9200/website/news,blog/_search?pretty'
# curl -XGET 'http://localhost:9200/website,twitter/news,blog/_search?pretty'
所有 _index 包含指定 _type 搜尋
# curl -XGET 'http://localhost:9200/_all/news,blog/_search?pretty'
60.3.1. 分頁
該功能與SQL的LIMIT關鍵字結果一樣,Elasticsearch接受size和from兩個引數引數:
size: 返回結果集數量,預設10,用法與SQL中的 Limit相同
from: 偏移量,預設0,用法與 SQL中的 Offset相同
如果你想每頁顯示10個結果,那麼請求如下:
第一頁 GET /_search?size=10
第二頁 GET /_search?size=10&from=10
第三頁 GET /_search?size=10&from=20
60.3.2. 字串搜尋
# curl -XGET 'http://localhost:9200/_all/_search?q=neo&pretty'
同時滿足兩個條件
+name:neo +age:30
查詢name為mary 或者 john的資料
+name:(mary john)
查詢姓名是neo或者jam並且年齡小於30歲同時1980-09-10之後出生的
+name:(neo jam) +age:<30 +date:>1980-09-10