ElasticSearch入門實(shí)戰(zhàn)1

  1. document數(shù)據(jù)格式
  2. 電商網(wǎng)站商品管理案例背景介紹
  3. 簡(jiǎn)單的集群管理
  4. 商品的CRUD操作(document curd)

1. Document數(shù)據(jù)格式

面向文檔的搜索分析引擎

  • 應(yīng)用系統(tǒng)的數(shù)據(jù)結(jié)構(gòu)都是面向?qū)ο蟮模瑥?fù)雜的
  • 對(duì)象數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中,只能拆解開(kāi)來(lái)先嬉,變成扁平的多張表,每次查詢的時(shí)候還有還原成對(duì)象格式顶猜,相當(dāng)麻煩
  • ES是面向文檔的,文檔中存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu),與面向?qū)ο蟮臄?shù)據(jù)結(jié)構(gòu)是一樣的,基于這種文檔數(shù)據(jù)結(jié)構(gòu)偷俭。ES可以提供復(fù)雜的索引缰盏,全文檢索涌萤,分析聚合等功能。
  • ES的document用json數(shù)據(jù)格式來(lái)表示的负溪。
public class Employee {
    private String email;
    private String firstName;
    private String lastName;
    private EmployeeInfo info;
    private Date joinDate;
}


private class EmployeeInfo {
    private String bio;
    private Integer age;
    private String[] interesets;
}

EmployeeInfo info = new EmployeeInfo();
info.SetBio("youdi");
info.setAge(24);
info.setInteresets(new String[]{"dance", "code"});

Employee employee = new Employee();
employee.setEmail("@");
employee.setInfo(info);

employee對(duì)象:里面包含了Employee類自己屬性。需要保存在兩張表辐真。
使用ORM.數(shù)據(jù)庫(kù)是扁平化的,不能體現(xiàn)出面向?qū)ο蟮慕Y(jié)構(gòu)侍咱。

2.電商網(wǎng)站商品管理案例背景介紹

提供功能如下:

  1. 對(duì)商品信息進(jìn)行CRUD
  2. 執(zhí)行簡(jiǎn)單的全文檢索密幔,以及復(fù)雜的phrase檢索
  3. 對(duì)于全文檢索結(jié)果,可以進(jìn)行高亮顯示
  4. 對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單的聚合分析

3. 簡(jiǎn)單的集群管理

  1. 快速檢查集群的健康狀況

    ES提供了一套cat api胯甩,可以查看es中各種各樣的數(shù)據(jù)

    GET /_cat/health?pretty
    epoch      timestamp cluster             status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1524842572 23:22:52  elasticsearch_youdi yellow          1         1     10  10    0    0       10             0                  -                 50.0%
    

    查看status

    Green: 每個(gè)索引的primary shard和replica shard 都是active狀態(tài)

    yellow: 每個(gè)索引的primary shard是active狀態(tài),但是部分replica shard不是active狀態(tài)偎箫,處于不可用的狀態(tài)

    red: 不是所有的索引的primary shard都是active狀態(tài),部分縮影有數(shù)據(jù)丟失

為什么現(xiàn)在會(huì)處于yellow狀態(tài)眉枕?

primary shard replica shard沒(méi)有第二個(gè)節(jié)點(diǎn)

  1. 快速查看集群中所有索引?
GET /_cat/indices?v
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   website  -NVtisruRUO1buiENwh7Vw   5   1          1            0      5.5kb          5.5kb
yellow open   megacorp 3YlG1FRPTDynSETmZBDIhg   5   1          3            0     17.5kb         17.5kb
  1. 簡(jiǎn)單的索引操作
PUT /test_index?pretty


DELETE /test_index?pretty

商品的CURD

  1. 新增商品
PUT /index/type/id

PUT /products/goods/1
{
  "name": "test",
  "desc": "hello world",
  "price": 11.0,
  "producer": "hello",
  "tags": ["youdi", "haha"]
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
  1. 查詢商品
GET /products/goods/1

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "test",
    "desc": "hello world",
    "price": 11,
    "producer": "hello",
    "tags": [
      "youdi",
      "haha"
    ]
  }
}
  1. 更新操作
替換方式: 必須提交所有的信息
PUT /products/goods/1
{
  "name": "test3",
  "desc": "hello world",
  "price": 11.5,
  "producer": "hello",
  "tags": ["youdi", "haha"]
}


{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}



PUT /products/goods/1
{
  "name": "test3"
}

GET /products/goods/1
{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 3,
  "found": true,
  "_source": {
    "name": "test3"
  }
}


POST products/goods/1/_update
{
  "doc": {
  "name": "test6"
  }
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 5,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}
  1. 刪除文檔
DELETE /products/goods/1
{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "_version": 6,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

{
  "_index": "products",
  "_type": "goods",
  "_id": "1",
  "found": false
}

商品的搜索方式

  1. query string search
  2. Query DSL
  3. query filter
  4. Full-text search

1. query string search

搜索全部商品

GET /products/goods/_search

{
  "took": 104, //耗時(shí)
  "timed_out": false, //是否超時(shí)
  "_shards": { //_shards 
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1, // 查詢結(jié)果的數(shù)量 1條數(shù)據(jù)
    "max_score": 1, //ES對(duì)相關(guān)度的匹配分?jǐn)?shù)
    "hits": [  //包含了搜索的詳細(xì)數(shù)據(jù)
      {
        "_index": "products",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "tes2",
          "desc": "hello world",
          "price": 14,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      }
    ]
  }
}



{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "products",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "tes2",
          "desc": "hello world",
          "price": 14,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      },
      {
        "_index": "products",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "test3",
          "desc": "hello world",
          "price": 11.5,
          "producer": "hello",
          "tags": [
            "youdi",
            "haha"
          ]
        }
      }
    ]
  }
}


      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      },
  1. 搜索賬號(hào)名字中有ber,而且按照年齡排序
GET /bank/_doc/_search?q=firstname:Virginia&sort=age:des

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 4.882802,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 4.882802,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      }
    ]
  }
}
  1. query DSL

DSL:Domain specified Language 特定領(lǐng)域的語(yǔ)言

查詢所有的account

http request body :請(qǐng)求體塔插,可以使用json的格式構(gòu)建查詢語(yǔ)法梗摇,比較方便,可以構(gòu)建各種復(fù)雜的語(yǔ)法想许。

GET /bank/_doc/_search
{
  "query": {
    "match_all": {}
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 1,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "25",
        "_score": 1,
        "_source": {
          "account_number": 25,
          "balance": 40540,
          "firstname": "Virginia",
          "lastname": "Ayala",
          "age": 39,
          "gender": "F",
          "address": "171 Putnam Avenue",
          "employer": "Filodyne",
          "email": "virginiaayala@filodyne.com",
          "city": "Nicholson",
          "state": "PA"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "44",
        "_score": 1,
        "_source": {
          "account_number": 44,
          "balance": 34487,
          "firstname": "Aurelia",
          "lastname": "Harding",
          "age": 37,
          "gender": "M",
          "address": "502 Baycliff Terrace",
          "employer": "Orbalix",
          "email": "aureliaharding@orbalix.com",
          "city": "Yardville",
          "state": "DE"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "99",
        "_score": 1,
        "_source": {
          "account_number": 99,
          "balance": 47159,
          "firstname": "Ratliff",
          "lastname": "Heath",
          "age": 39,
          "gender": "F",
          "address": "806 Rockwell Place",
          "employer": "Zappix",
          "email": "ratliffheath@zappix.com",
          "city": "Shaft",
          "state": "ND"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "119",
        "_score": 1,
        "_source": {
          "account_number": 119,
          "balance": 49222,
          "firstname": "Laverne",
          "lastname": "Johnson",
          "age": 28,
          "gender": "F",
          "address": "302 Howard Place",
          "employer": "Senmei",
          "email": "lavernejohnson@senmei.com",
          "city": "Herlong",
          "state": "DC"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "126",
        "_score": 1,
        "_source": {
          "account_number": 126,
          "balance": 3607,
          "firstname": "Effie",
          "lastname": "Gates",
          "age": 39,
          "gender": "F",
          "address": "620 National Drive",
          "employer": "Digitalus",
          "email": "effiegates@digitalus.com",
          "city": "Blodgett",
          "state": "MD"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "145",
        "_score": 1,
        "_source": {
          "account_number": 145,
          "balance": 47406,
          "firstname": "Rowena",
          "lastname": "Wilkinson",
          "age": 32,
          "gender": "M",
          "address": "891 Elton Street",
          "employer": "Asimiline",
          "email": "rowenawilkinson@asimiline.com",
          "city": "Ripley",
          "state": "NH"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "183",
        "_score": 1,
        "_source": {
          "account_number": 183,
          "balance": 14223,
          "firstname": "Hudson",
          "lastname": "English",
          "age": 26,
          "gender": "F",
          "address": "823 Herkimer Place",
          "employer": "Xinware",
          "email": "hudsonenglish@xinware.com",
          "city": "Robbins",
          "state": "ND"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "190",
        "_score": 1,
        "_source": {
          "account_number": 190,
          "balance": 3150,
          "firstname": "Blake",
          "lastname": "Davidson",
          "age": 30,
          "gender": "F",
          "address": "636 Diamond Street",
          "employer": "Quantasis",
          "email": "blakedavidson@quantasis.com",
          "city": "Crumpler",
          "state": "KY"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "208",
        "_score": 1,
        "_source": {
          "account_number": 208,
          "balance": 40760,
          "firstname": "Garcia",
          "lastname": "Hess",
          "age": 26,
          "gender": "F",
          "address": "810 Nostrand Avenue",
          "employer": "Quiltigen",
          "email": "garciahess@quiltigen.com",
          "city": "Brooktrails",
          "state": "GA"
        }
      },
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "222",
        "_score": 1,
        "_source": {
          "account_number": 222,
          "balance": 14764,
          "firstname": "Rachelle",
          "lastname": "Rice",
          "age": 36,
          "gender": "M",
          "address": "333 Narrows Avenue",
          "employer": "Enaut",
          "email": "rachellerice@enaut.com",
          "city": "Wright",
          "state": "AZ"
        }
      }
    ]
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末伶授,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子流纹,更是在濱河造成了極大的恐慌糜烹,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件漱凝,死亡現(xiàn)場(chǎng)離奇詭異疮蹦,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)茸炒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門愕乎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)阵苇,“玉大人,你說(shuō)我怎么就攤上這事感论∩鹣睿” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵比肄,是天一觀的道長(zhǎng)快耿。 經(jīng)常有香客問(wèn)我,道長(zhǎng)芳绩,這世上最難降的妖魔是什么掀亥? 我笑而不...
    開(kāi)封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮妥色,結(jié)果婚禮上铺浇,老公的妹妹穿的比我還像新娘。我一直安慰自己垛膝,他們只是感情好鳍侣,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著吼拥,像睡著了一般倚聚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凿可,一...
    開(kāi)封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天惑折,我揣著相機(jī)與錄音枯跑,去河邊找鬼。 笑死粗卜,一個(gè)胖子當(dāng)著我的面吹牛续扔,可吹牛的內(nèi)容都是我干的焕数。 我是一名探鬼主播堡赔,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼离例!你這毒婦竟也來(lái)了粘招?” 一聲冷哼從身側(cè)響起偎球,我...
    開(kāi)封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤辑甜,失蹤者是張志新(化名)和其女友劉穎磷醋,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體淌友,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡震庭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年器联,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了婿崭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡渣磷,死狀恐怖幸海,靈堂內(nèi)的尸體忽然破棺而出奥务,到底是詐尸還是另有隱情,我是刑警寧澤挡篓,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布秽澳,位于F島的核電站,受9級(jí)特大地震影響担神,放射性物質(zhì)發(fā)生泄漏妄讯。R本人自食惡果不足惜酷宵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一浇垦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧朴摊,春花似錦甚纲、人聲如沸寡壮。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)悲靴。三九已至莫其,卻和暖如春乱陡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背憨颠。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缚陷,地道東北人往核。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓聂儒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親翁都。 傳聞我的和親對(duì)象是個(gè)殘疾皇子碍论,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

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