java學(xué)習(xí)

          java學(xué)習(xí)

           

          elasticsearch 商品搜索信息的crud簡單操作

          課程大綱
          1、document數(shù)據(jù)格式
          2、電商網(wǎng)站商品管理案例:背景介紹
          3、簡單的集群管理
          4、商品的CRUD操作(document CRUD操作)
          ----------------------------------------------------------------------------------------------------------------------------
          1、document數(shù)據(jù)格式
          面向文檔的搜索分析引擎
          (1)應(yīng)用系統(tǒng)的數(shù)據(jù)結(jié)構(gòu)都是面向?qū)ο蟮模瑥?fù)雜的
          (2)對象數(shù)據(jù)存儲到數(shù)據(jù)庫中,只能拆解開來,變?yōu)楸馄降亩鄰埍恚看尾樵兊臅r候還得還原回對象格式,相當(dāng)麻煩
          (3)ES是面向文檔的,文檔中存儲的數(shù)據(jù)結(jié)構(gòu),與面向?qū)ο蟮臄?shù)據(jù)結(jié)構(gòu)是一樣的,基于這種文檔數(shù)據(jù)結(jié)構(gòu),es可以提供復(fù)雜的索引,全文檢索,分析聚合等功能
          (4)es的document用json數(shù)據(jù)格式來表達
          2、電商網(wǎng)站商品管理案例背景介紹
          有一個電商網(wǎng)站,需要為其基于ES構(gòu)建一個后臺系統(tǒng),提供以下功能:
          (1)對商品信息進行CRUD(增刪改查)操作
          (2)執(zhí)行簡單的結(jié)構(gòu)化查詢
          (3)可以執(zhí)行簡單的全文檢索,以及復(fù)雜的phrase(短語)檢索
          (4)對于全文檢索的結(jié)果,可以進行高亮顯示
          (5)對數(shù)據(jù)進行簡單的聚合分析
          ----------------------------------------------------------------------------------------------------------------------------
          3、簡單的集群管理
          (1)快速檢查集群的健康狀況
          es提供了一套api,叫做cat api,可以查看es中各種各樣的數(shù)據(jù)
          GET /_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
          1488006741 15:12:21  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0%
          epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
          1488007113 15:18:33  elasticsearch green           2         2      2   1    0    0        0             0                  -                100.0%
          epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
          1488007216 15:20:16  elasticsearch yellow          1         1      1   1    0    0        1             0                  -                 50.0%
          如何快速了解集群的健康狀況?green、yellow、red?
          green:每個索引的primary shard和replica shard都是active狀態(tài)的
          yellow:每個索引的primary shard都是active狀態(tài)的,但是部分replica shard不是active狀態(tài),處于不可用的狀態(tài)
          red:不是所有索引的primary shard都是active狀態(tài)的,部分索引有數(shù)據(jù)丟失了
          為什么現(xiàn)在會處于一個yellow狀態(tài)?
          我們現(xiàn)在就一個筆記本電腦,就啟動了一個es進程,相當(dāng)于就只有一個node。現(xiàn)在es中有一個index,就是kibana自己內(nèi)置建立的index。由于默認(rèn)的配置是給每個index分配5個primary shard和5個replica shard,而且primary shard和replica shard不能在同一臺機器上(為了容錯)。現(xiàn)在kibana自己建立的index是1個primary shard和1個replica shard。當(dāng)前就一個node,所以只有1個primary shard被分配了和啟動了,但是一個replica shard沒有第二臺機器去啟動。
          做一個小實驗:此時只要啟動第二個es進程,就會在es集群中有2個node,然后那1個replica shard就會自動分配過去,然后cluster status就會變成green狀態(tài)。
          (2)快速查看集群中有哪些索引
          GET /_cat/indices?v
          health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
          yellow open   .kibana rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb
          (3)簡單的索引操作
          創(chuàng)建索引:PUT /test_index?pretty
          health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
          yellow open   test_index XmS9DTAtSkSZSwWhhGEKkQ   5   1          0            0       650b           650b
          yellow open   .kibana    rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb
          刪除索引:DELETE /test_index?pretty
          health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
          yellow open   .kibana rUm9n9wMRQCCrRDEhqneBg   1   1          1            0      3.1kb          3.1kb
          ----------------------------------------------------------------------------------------------------------------------------
          4、商品的CRUD操作
          (1)新增商品:新增文檔,建立索引
          PUT /index/type/id
          {
            "json數(shù)據(jù)"
          }
          PUT /ecommerce/product/1
          {
              "name" : "gaolujie yagao",
              "desc" :  "gaoxiao meibai",
              "price" :  30,
              "producer" :      "gaolujie producer",
              "tags": [ "meibai", "fangzhu" ]
          }
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 1,
            "result": "created",
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            },
            "created": true
          }
          PUT /ecommerce/product/2
          {
              "name" : "jiajieshi yagao",
              "desc" :  "youxiao fangzhu",
              "price" :  25,
              "producer" :      "jiajieshi producer",
              "tags": [ "fangzhu" ]
          }
          PUT /ecommerce/product/3
          {
              "name" : "zhonghua yagao",
              "desc" :  "caoben zhiwu",
              "price" :  40,
              "producer" :      "zhonghua producer",
              "tags": [ "qingxin" ]
          }
          es會自動建立index和type,不需要提前創(chuàng)建,而且es默認(rèn)會對document每個field都建立倒排索引,讓其可以被搜索
          (2)查詢商品:檢索文檔
          GET /index/type/id
          GET /ecommerce/product/1
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 1,
            "found": true,
            "_source": {
              "name": "gaolujie yagao",
              "desc": "gaoxiao meibai",
              "price": 30,
              "producer": "gaolujie producer",
              "tags": [
                "meibai",
                "fangzhu"
              ]
            }
          }
          (3)修改商品:替換文檔
          PUT /ecommerce/product/1
          {
              "name" : "jiaqiangban gaolujie yagao",
              "desc" :  "gaoxiao meibai",
              "price" :  30,
              "producer" :      "gaolujie producer",
              "tags": [ "meibai", "fangzhu" ]
          }
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 1,
            "result": "created",
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            },
            "created": true
          }
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 2,
            "result": "updated",
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            },
            "created": false
          }
          PUT /ecommerce/product/1
          {
              "name" : "jiaqiangban gaolujie yagao"
          }
          替換方式有一個不好,即使必須帶上所有的field,才能去進行信息的修改
          (4)修改商品:更新文檔
          POST /ecommerce/product/1/_update
          {
            "doc": {
              "name": "jiaqiangban gaolujie yagao"
            }
          }
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 8,
            "result": "updated",
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            }
          }
          我的風(fēng)格,其實有選擇的情況下,不太喜歡念ppt,或者照著文檔做,或者直接粘貼寫好的代碼,盡量是純手敲代碼
          (5)刪除商品:刪除文檔
          DELETE /ecommerce/product/1
          {
            "found": true,
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "_version": 9,
            "result": "deleted",
            "_shards": {
              "total": 2,
              "successful": 1,
              "failed": 0
            }
          }
          {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1",
            "found": false
          }

          posted on 2018-11-10 18:31 楊軍威 閱讀(144) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 商洛市| 建湖县| 阜平县| 息烽县| 阿拉善右旗| 象山县| 杂多县| 高州市| 黄龙县| 闻喜县| 铜山县| 吉安县| 万源市| 黔南| 盐城市| 海盐县| 桐乡市| 沧州市| 巍山| 剑川县| 永顺县| 简阳市| 光泽县| 友谊县| 漾濞| 阳东县| 遵化市| 施秉县| 潜山县| 江山市| 双流县| 卢湾区| 兴义市| 将乐县| 苍溪县| 故城县| 昆山市| 五指山市| 南康市| 衡阳市| 九龙坡区|