請求方式
CURL
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
TAG | 描述 |
---|---|
VERB | 適當(dāng)?shù)?HTTP 方法 或 謂詞 : GET、 POST虚倒、 PUT弊琴、 HEAD 或者 DELETE。 |
PROTOCOL | http 或者 https(如果你在 Elasticsearch 前面有一個(gè) https 代理) |
HOST | Elasticsearch 集群中任意節(jié)點(diǎn)的主機(jī)名,或者用 localhost 代表本地機(jī)器上的節(jié)點(diǎn)来破。 |
PORT | 運(yùn)行 Elasticsearch HTTP 服務(wù)的端口號默辨,默認(rèn)是 9200 。 |
PATH | API 的終端路徑(例如 _count 將返回集群中文檔數(shù)量)鲜漩。Path 可能包含多個(gè)組件源譬,例如:_cluster/stats 和 _nodes/stats/jvm 。 |
QUERY_STRING | 任意可選的查詢字符串參數(shù) (例如 ?pretty 將格式化地輸出 JSON 返回值孕似,使其更容易閱讀) |
BODY | 一個(gè) JSON 格式的請求體 (如果請求需要的話) |
POSMAN
可以使用POSMAN進(jìn)行調(diào)試
例:
- curl:
請求:
獲取集群中文檔的數(shù)量
curl -XGET 'localhost:9200/_count?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
返回:
{
"count" : 1,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
}
}
-
POSMAN:
實(shí)戰(zhàn)
新增索引和數(shù)據(jù)
curl
- 請求:
curl -XPUT 'localhost:9200/megacorp/employee/1?pretty' -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
'
- 返回:
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
posman
詳解:
megacorp : 索引名稱
employee : 類型名稱
1 : 特定ID
獲取文檔
curl
- 請求:
[hadoop@centos-linux data]$ curl -XGET
- 返回:
'localhost:9200/megacorp/employee/1?pretty'
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
posman
簡單查詢
curl
- 請求
curl -GET 'localhost:9200/megacorp/employee/_search?pretty'
- 返回
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}
POSMAN
簡單條件查詢
curl
- 請求
curl -XGET 'localhost:9200/megacorp/employee/_search?q=first_name=Jane&pretty'
- 返回
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}
posman
簡單查詢表達(dá)式
curl
- 請求
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"first_name" : "John"
}
}
}
'
- 返回
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}
posman
復(fù)雜搜索
查詢 last_name 為: smith,并且 age > 30
curl
- 請求
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
'
- 返回
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
}
]
}
}
posman
全文檢索
查找所有喜歡攀巖的雇員(rock climbing)
curl
- 請求
curl -XGET 'localhost:9200/megacorp2/employee2/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
'
- 返回
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "megacorp2",
"_type": "employee2",
"_id": "1",
"_score": 0.5753642,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "megacorp2",
"_type": "employee2",
"_id": "2",
"_score": 0.2876821,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
}
]
}
}
posman
Elasticsearch 默認(rèn)按照相關(guān)性得分排序踩娘,即每個(gè)文檔跟查詢的匹配程度。第一個(gè)最高得分的結(jié)果很明顯:John Smith 的 about
屬性清楚地寫著 “rock climbing” 喉祭。
但為什么 Jane Smith 也作為結(jié)果返回了呢养渴?原因是她的 about
屬性里提到了 “rock” 。因?yàn)橹挥?“rock” 而沒有 “climbing” 泛烙,所以她的相關(guān)性得分低于 John 的理卑。
這是一個(gè)很好的案例,闡明了 Elasticsearch 如何 在 全文屬性上搜索并返回相關(guān)性最強(qiáng)的結(jié)果胶惰。Elasticsearch中的 相關(guān)性 概念非常重要傻工,也是完全區(qū)別于傳統(tǒng)關(guān)系型數(shù)據(jù)庫的一個(gè)概念,數(shù)據(jù)庫中的一條記錄要么匹配要么不匹配孵滞。
強(qiáng)制匹配搜索
curl
- 請求
curl -XGET 'localhost:9200/megacorp2/employee2/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
'
- 返回
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "megacorp2",
"_type": "employee2",
"_id": "1",
"_score": 0.5753642,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
posman
只會返回明確匹配的
高亮搜索
curl
- 請求
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
'
- 返回
{
"took" : 44,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.5753642,
"_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>"
]
}
}
]
}
}
posman
-
請求
-
返回
簡單聚合
獲取雇員最受歡迎的興趣愛好
- 請求
curl -XGET 'localhost:9200/megacorp/employee/_search?pretty' -H 'Content-Type: application/json' -d'
{
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
'
- 返回
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
},
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "music",
"doc_count" : 2
},
{
"key" : "sports",
"doc_count" : 1
}
]
}
}
}
posman
-
請求
-
返回
注意: 如果聚合操作報(bào)錯的話,執(zhí)行:
fielddata
curl -XPUT 'localhost:9200/megacorp/_mapping/employee/' -H 'Content-Type: application/json' -d'
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}'