Elasticsearch簡單使用系列--入門

1 es啟動時,命令行設(shè)置名字

./elasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_name

2 REST API可以做什么

1.檢查集群,節(jié)點,索引的健康,狀態(tài),統(tǒng)計
2.管理集群,節(jié)點,索引的數(shù)據(jù)和元數(shù)據(jù)
3.執(zhí)行CRUD和搜索操作
4.執(zhí)行高級搜索操作,比如分頁,排序,過濾,腳本,聚合等
2.1 集群健康檢測api
http://192.168.0.128:9200/_cat/health?v

epoch      timestamp cluster    status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1498119164 16:12:44  es-cluster yellow          1         1     20  20    0    0       20             0                  -                 50.0%

說明:集群的狀態(tài)描述:
    green:一切都準(zhǔn)備好了,集群功能全部可用;
    yellow:數(shù)據(jù)準(zhǔn)備好了,但是副本還沒有分配好,集群功能全部可用;
    red:有些數(shù)據(jù)不可用,但是集群部分功能可用;
2.2 集群節(jié)點列表api
http://192.168.0.128:9200/_cat/nodes?v

ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.128           19          72  58                          mdi       *      master
2.3 列出集群中所有的索引
http://192.168.0.128:9200/_cat/indices?v

health status index uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   ttl   mPSsvTX3TbSsSQKUcJqtbA   5   1          0            0       795b           795b
yellow open   java  3IfBdV_-T8SuvNSK72jBqQ   5   1          0            0       650b           650b
yellow open   book  -rZ8v4AfTDyPPTm3oZ_qLQ   5   1         16            0     64.7kb         64.7kb
yellow open   index 4BAj2ycsSGyosLYPmTQEZw   5   1          0            0       795b           795b

上面health都為yellow是因為只有一個node,es默認(rèn)創(chuàng)建一個副本,等待其他的節(jié)點加入
2.4 創(chuàng)建一個customer的index
http://192.168.0.128:9200/customer?pretty
method: PUT

response:
{
  "acknowledged": true,
  "shards_acknowledged": true
}
2.5 創(chuàng)建文檔索引和查詢文檔
http://192.168.0.128:9200/customer/person/1?pretty
method: PUT
params: {"name":"張三","age":34,"sex":"男"}

response:
{
  "_index": "customer",
  "_type": "person",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

----------------------------------------------------------
 http://192.168.0.128:9200/customer/person?pretty
 method: POST
 params: {"name":"張三","age":34,"sex":"男"}
 
 response:
 {
   "_index": "customer",
   "_type": "person",
   "_id": "AVzTAOlNiSjTxTQlMxWw",
   "_version": 1,
   "result": "created",
   "_shards": {
     "total": 2,
     "successful": 1,
     "failed": 0
   },
   "created": true
 }

-----------------------------------------------------------

http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw/_update?pretty
method: POST
params:
{
    "doc":{"name":"李四","age":44,"sex":"男"}
}

response:
{
  "_index": "customer",
  "_type": "person",
  "_id": "AVzTAOlNiSjTxTQlMxWw",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

ps:AVzTAOlNiSjTxTQlMxWw是文檔id

---------------------------------------------------------------

使用腳本更新,ctx._source指向當(dāng)前source文檔
http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw/_update?pretty
method: POST
params:
{
    "script" : "ctx._source.age += 5"
}

response:
{
  "_index": "customer",
  "_type": "person",
  "_id": "AVzTAOlNiSjTxTQlMxWw",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}


-------------------------------------------------------

http://192.168.0.128:9200/customer/person/1?pretty
method: GET

response:
{
  "_index": "customer",
  "_type": "person",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "張三",
    "age": 34,
    "sex": "男"
  }
}
2.6 刪除索引
http://192.168.0.128:9200/customer
method: DELETE

response:
{
  "acknowledged": true
}
2.7 根據(jù)文檔id,刪除文檔
http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw?pretty
method: DELETE

response:
{
  "found": true,
  "_index": "customer",
  "_type": "person",
  "_id": "AVzTAOlNiSjTxTQlMxWw",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}
2.8 elasticsearch提供了批處理_bulk API,可以同時處理index,update,delete操作.批處理減少網(wǎng)絡(luò)請求.批處理時,如果某個動作失敗了,不會影響其他的動作;批處理返回結(jié)果按執(zhí)行的順序返回動作執(zhí)行狀態(tài),可以檢測是否失敗.
批量添加兩個index
http://192.168.0.128:9200/customer/person/_bulk?pretty
method: POST
params:
{"index" : {"_id" : 2}}
{"name" : "趙六","age" : 23}
{"index" : {"_id" : 3}}
{"name" : "王五","age" : 53}

response:
{
  "took" : 1541,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "customer",
        "_type" : "person",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "customer",
        "_type" : "person",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "created" : true,
        "status" : 201
      }
    }
  ]
}

------------------------------------------------------


更新文檔2,刪除文檔3
http://192.168.0.128:9200/customer/person/_bulk?pretty
method: POST
params:
{"update" : {"_id" : 2}}
{"doc" : {"age" : 33}}
{"delete" : {"_id" : 3}}

response:
{
  "took": 941,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "customer",
        "_type": "person",
        "_id": "2",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "status": 200
      }
    },
    {
      "delete": {
        "found": true,
        "_index": "customer",
        "_type": "person",
        "_id": "3",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "status": 200
      }
    }
  ]
}
2.9 使用REST API搜索文檔
搜索所有文檔,結(jié)果按account_number升序排序
http://192.168.0.128:9200/bank/_search?q=*&sort=account_number:asc&pretty
等價的寫法:
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from" : 5, //從第5條開始,默認(rèn)是0
  "size" : 1 //返回1條,默認(rèn)是10條
}

response:
{
  "took": 32, //搜索時間,單位:毫秒
  "timed_out": false, //搜索是否超時
  "_shards": { //搜索分片數(shù)量,以及成功和失敗的數(shù)量
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": { //搜索結(jié)果
    "total": 1000, //滿足搜索條件的文檔數(shù)量
    "max_score": null,
    "hits": [ //真實搜索結(jié)果數(shù)組,默認(rèn)顯示10條
      {
        "_index": "bank",
        "_type": "account",
        "_id": "0",
        "_score": null,
        "_source": {
          "account_number": 0,
          "balance": 16623,
          "firstname": "Bradshaw",
          "lastname": "Mckenzie",
          "age": 29,
          "gender": "F",
          "address": "244 Columbus Place",
          "employer": "Euron",
          "email": "bradshawmckenzie@euron.com",
          "city": "Hobucken",
          "state": "CO"
        },
        "sort": [ //排序的結(jié)果
          0
        ]
      }
    ]
  }
}

------------------------------------------
搜索所有的文檔,返回前2條,并顯示指定的fields
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": { "match_all": {} },
  "_source" : ["account_number","balance","email"], //返回指定的字段
  "size" : 2
}

response:
{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "account",
        "_id": "25",
        "_score": 1,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "email": "virginiaayala@filodyne.com"
        }
      },
      {
        "_index": "bank",
        "_type": "account",
        "_id": "44",
        "_score": 1,
        "_source": {
          "account_number": 44,
          "balance": 34487,
          "email": "aureliaharding@orbalix.com"
        }
      }
    ]
  }
}

------------------------------------
搜索account_number為20的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": { "match": {"account_number" : 20} }
}

------------------------------
搜索address中含有mill的所有文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
 "query": { "match": {"address" : "mill"} }
}

---------------------------------
使用match_phrase匹配address中含有"mill lane"短語的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": { "match_phrase": {"address" : "mill lane"} }
}

---------------------------------

使用bool query匹配address中同時含有"mill "和"lane"短語的文檔,must:and
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}

與之類似的:
should:or關(guān)系
must_not:即不含"mill",也不含"lane"的文檔
bool query可以同時包含must,should,must_not組成復(fù)雜的查詢
2.10 文檔score:根據(jù)搜索條件估算一個文檔匹配程度的相對的數(shù)值.得分越高,文檔越有價值;反之,價值越低.有些情況不需要score(比如"filter""),es會檢測自動優(yōu)化查詢,不計算得分.
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "query": {
    "bool": {
      "must": { "match_all": {} }, //查詢所有的文檔
      "filter": { //過濾,不計算得分,從結(jié)果可以查出所有的score都為1,是個常量
        "range": { //范圍查詢,適用于numeric或deta 類型
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}
2.11 執(zhí)行聚合:es提供了分組和統(tǒng)計的能力,這就是聚合.可以認(rèn)為就是sql中的group by和aggregate 功能.es在聚合時同時返回搜索的文檔和聚合兩部分.
按state分組聚合,不返回搜索的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "size": 0,//不返回搜索的文檔
  "aggs": {//聚合
    "group_by_state": {
      "terms": {
        "field": "state.keyword" //按state分組,降序排序
      }
    }
  }
}

response:
{
  "took": 58,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_state": {
      "doc_count_error_upper_bound": 20,
      "sum_other_doc_count": 770,
      "buckets": [
        {
          "key": "ID",
          "doc_count": 27
        },
        ...,
        {
          "key": "MO",
          "doc_count": 20
        }
      ]
    }
  }
}

--------------------------------------------------

按state分組,統(tǒng)計每個state的平均工資,并降序排序
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}   
 
 -------------------------------------------------
按年齡段分組,然后按性別分組,統(tǒng)計每個年齡段中不同性別的平均工資
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
 "size": 0,
 "aggs": {
   "group_by_age": {
     "range": {
       "field": "age",
       "ranges": [
         {
           "from": 20,
           "to": 30
         },
         {
           "from": 30,
           "to": 40
         },
         {
           "from": 40,
           "to": 50
         }
       ]
     },
     "aggs": {
       "group_by_gender": {
         "terms": {
           "field": "gender.keyword"
         },
         "aggs": {
           "average_balance": {
             "avg": {
               "field": "balance"
             }
           }
         }
       }
     }
   }
 }
}

3 案例使用的數(shù)據(jù)地址數(shù)據(jù)

4 以上所有的內(nèi)容都在es 5.4 文檔部分

5 打算后面一塊寫一篇文章(類似與精華翻譯了,哈哈哈),大家需要不?歡迎評論

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末豪诲,一起剝皮案震驚了整個濱河市罪帖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌法焰,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坛悉,死亡現(xiàn)場離奇詭異隔缀,居然都是意外死亡,警方通過查閱死者的電腦和手機斥滤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勉盅,“玉大人佑颇,你說我怎么就攤上這事〔菽龋” “怎么了挑胸?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長宰闰。 經(jīng)常有香客問我茬贵,道長,這世上最難降的妖魔是什么移袍? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任解藻,我火速辦了婚禮,結(jié)果婚禮上葡盗,老公的妹妹穿的比我還像新娘螟左。我一直安慰自己,他們只是感情好觅够,可當(dāng)我...
    茶點故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布胶背。 她就那樣靜靜地躺著,像睡著了一般喘先。 火紅的嫁衣襯著肌膚如雪钳吟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天窘拯,我揣著相機與錄音红且,去河邊找鬼坝茎。 笑死,一個胖子當(dāng)著我的面吹牛直焙,可吹牛的內(nèi)容都是我干的景东。 我是一名探鬼主播,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼奔誓,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搔涝?” 一聲冷哼從身側(cè)響起厨喂,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎庄呈,沒想到半個月后蜕煌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡诬留,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年斜纪,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片文兑。...
    茶點故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡盒刚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绿贞,到底是詐尸還是另有隱情因块,我是刑警寧澤,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布籍铁,位于F島的核電站涡上,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拒名。R本人自食惡果不足惜吩愧,卻給世界環(huán)境...
    茶點故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望增显。 院中可真熱鬧雁佳,春花似錦、人聲如沸甸怕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梢杭。三九已至温兼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間武契,已是汗流浹背募判。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工荡含, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人届垫。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓释液,卻偏偏與公主長得像,于是被迫代替她去往敵國和親装处。 傳聞我的和親對象是個殘疾皇子误债,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,446評論 2 348

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

  • 博客原文一博客原文二 翻譯作品,水平有限妄迁,如有錯誤寝蹈,煩請留言指正。原文請見 官網(wǎng)英文文檔 起步 Elasticse...
    rabbitGYK閱讀 3,230評論 0 68
  • 基礎(chǔ)概念 Elasticsearch有幾個核心概念登淘,從一開始理解這些概念會對整個學(xué)習(xí)過程有莫大的幫助箫老。 接近實時(...
    山天大畜閱讀 2,105評論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)黔州,斷路器耍鬓,智...
    卡卡羅2017閱讀 134,628評論 18 139
  • 介紹 elasticsearch是一個高效的、可擴展的全文搜索引擎 基本概念 Near Realtime(NRT)...
    imsilence閱讀 774評論 0 0
  • 這世上總有一個出口流妻,不在這里就在那里牲蜀,不在那里就在你看不見的那里。心靈有時也想出走合冀,去別人看不見某些人又能看得見的...
    十七年蟬魚閱讀 548評論 0 0