ES 增刪改查 入門

請求方式

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:


    通過posman獲取集群文檔數(shù)量

實(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

簡單表達(dá)式查詢

復(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

復(fù)雜查詢體查詢

全文檢索

查找所有喜歡攀巖的雇員(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

match_phrase

只會返回明確匹配的

高亮搜索

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

  • 請求


    請求
  • 返回


    image.png

高亮搜索

簡單聚合

獲取雇員最受歡迎的興趣愛好

  • 請求
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

  • 請求


    image.png
  • 返回


    image.png

注意: 如果聚合操作報(bào)錯的話,執(zhí)行:
fielddata

curl -XPUT 'localhost:9200/megacorp/_mapping/employee/' -H 'Content-Type: application/json' -d'
{
  "properties": {
    "interests": {
      "type":     "text",
      "fielddata": true
    }
  }
}'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末中捆,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子坊饶,更是在濱河造成了極大的恐慌泄伪,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匿级,死亡現(xiàn)場離奇詭異蟋滴,居然都是意外死亡染厅,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門津函,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肖粮,“玉大人,你說我怎么就攤上這事尔苦∩荩” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵允坚,是天一觀的道長魂那。 經(jīng)常有香客問我,道長稠项,這世上最難降的妖魔是什么涯雅? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮展运,結(jié)果婚禮上活逆,老公的妹妹穿的比我還像新娘。我一直安慰自己乐疆,他們只是感情好划乖,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著挤土,像睡著了一般。 火紅的嫁衣襯著肌膚如雪误算。 梳的紋絲不亂的頭發(fā)上仰美,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機(jī)與錄音儿礼,去河邊找鬼咖杂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蚊夫,可吹牛的內(nèi)容都是我干的诉字。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼知纷,長吁一口氣:“原來是場噩夢啊……” “哼壤圃!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起琅轧,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤伍绳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后乍桂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冲杀,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡效床,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了权谁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片剩檀。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖旺芽,靈堂內(nèi)的尸體忽然破棺而出沪猴,到底是詐尸還是另有隱情,我是刑警寧澤甥绿,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布字币,位于F島的核電站,受9級特大地震影響共缕,放射性物質(zhì)發(fā)生泄漏洗出。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一图谷、第九天 我趴在偏房一處隱蔽的房頂上張望翩活。 院中可真熱鬧,春花似錦便贵、人聲如沸菠镇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽利耍。三九已至,卻和暖如春盔粹,著一層夾襖步出監(jiān)牢的瞬間隘梨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工舷嗡, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留轴猎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓进萄,卻偏偏與公主長得像捻脖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子中鼠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容