天天看點

elk入門概念詳解

開始第一步

我們現在開始進行一個簡單教程,它涵蓋了一些基本的概念介紹,比如索引(indexing)、搜尋(search)以及聚合(aggregations)。通過這個教程,我們可以讓你對Elasticsearch能做的事以及其易用程度有一個大緻的感覺。

我們接下來将陸續介紹一些術語和基本的概念,但就算你沒有馬上完全了解也沒有關系。我們将在本書的各個章節中更加深入的探讨這些内容。

是以,坐下來,開始以旋風般的速度來感受Elasticsearch的能力吧!

讓我們建立一個員工目錄

假設我們剛好在Megacorp工作,這時人力資源部門出于某種目的需要讓我們建立一個員工目錄,這個目錄用于促進人文關懷和用于實時協同工作,是以它有以下不同的需求:

資料能夠包含多個值的标簽、數字和純文字。

檢索任何員工的所有資訊。

支援結構化搜尋,例如查找30歲以上的員工。

支援簡單的全文搜尋和更複雜的短語(phrase)搜尋

高亮搜尋結果中的關鍵字

能夠利用圖表管理分析這些資料

索引員工文檔

我們首先要做的是存儲員工資料,每個文檔代表一個員工。在Elasticsearch中存儲資料的行為就叫做索引(indexing),不過在索引之前,我們需要明确資料應該存儲在哪裡。

在Elasticsearch中,文檔歸屬于一種類型(type),而這些類型存在于索引(index)中,我們可以畫一些簡單的對比圖來類比傳統關系型資料庫:

Relational DB -> Databases -> Tables -> Rows -> Columns

Elasticsearch -> Indices   -> Types  -> Documents -> Fields

Elasticsearch叢集可以包含多個索引(indices)(資料庫),每一個索引可以包含多個類型(types)(表),每一個類型包含多個文檔(documents)(行),然後每個文檔包含多個字段(Fields)(列)。

「索引」含義的區分

你可能已經注意到索引(index)這個詞在Elasticsearch中有着不同的含義,是以有必要在此做一下區分:

索引(名詞) 如上文所述,一個索引(index)就像是傳統關系資料庫中的資料庫,它是相關文檔存儲的地方,index的複數是indices 或indexes。

索引(動詞) 「索引一個文檔」表示把一個文檔存儲到索引(名詞)裡,以便它可以被檢索或者查詢。這很像SQL中的INSERT關鍵字,差别是,如果文檔已經存在,新的文檔将覆寫舊的文檔。

反向索引 傳統資料庫為特定列增加一個索引,例如B-Tree索引來加速檢索。Elasticsearch和Lucene使用一種叫做反向索引(inverted index)的資料結構來達到相同目的。

預設情況下,文檔中的所有字段都會被索引(擁有一個反向索引),隻有這樣他們才是可被搜尋的。

我們将會在反向索引章節中更詳細的讨論。

是以為了建立員工目錄,我們将進行如下操作:

為每個員工的文檔(document)建立索引,每個文檔包含了相應員工的所有資訊。

每個文檔的類型為employee。

employee類型歸屬于索引megacorp。

megacorp索引存儲在Elasticsearch叢集中。

實際上這些都是很容易的(盡管看起來有許多步驟)。我們能通過一個指令執行完成的操作:

PUT /megacorp/employee/1

{

    "first_name" : "John",

    "last_name" :  "Smith",

    "age" :        25,

    "about" :      "I love to go rock climbing",

    "interests": [ "sports", "music" ]

}

我們看到path:/megacorp/employee/1包含三部分資訊:

名字 說明

megacorp 索引名

employee 類型名

1 這個員工的ID

請求實體(JSON文檔),包含了這個員工的所有資訊。他的名字叫“John Smith”,25歲,喜歡攀岩。

很簡單吧!它不需要你做額外的管理操作,比如建立索引或者定義每個字段的資料類型。我們能夠直接索引文檔,Elasticsearch已經内置所有的預設設定,所有管理操作都是透明的。

接下來,讓我們在目錄中加入更多員工資訊:

PUT /megacorp/employee/2

    "first_name" :  "Jane",

    "last_name" :   "Smith",

    "age" :         32,

    "about" :       "I like to collect rock albums",

    "interests":  [ "music" ]

PUT /megacorp/employee/3

    "first_name" :  "Douglas",

    "last_name" :   "Fir",

    "age" :         35,

    "about":        "I like to build cabinets",

    "interests":  [ "forestry" ]

結果:

[root@master elasticsearch]#

<code>curl -i -XPUT 127.0.0.1:9200</code><code>/megacorp/employee/1</code> <code>-d '                     </code>

<code>{</code>

<code>    </code><code>"first_name"</code> <code>: </code><code>"John"</code><code>,</code>

<code>    </code><code>"last_name"</code> <code>:  </code><code>"Smith"</code><code>,</code>

<code>    </code><code>"age"</code> <code>:        25,</code>

<code>    </code><code>"about"</code> <code>:      </code><code>"I love to go rock climbing"</code><code>,</code>

<code>    </code><code>"interests"</code><code>: [ </code><code>"sports"</code><code>, </code><code>"music"</code> <code>]</code>

<code>}</code>

<code>'</code>

結果: 叢集資料目錄多一個了index megacorp名名稱的(indices目錄是 index名稱的複數)

<code>ls</code> <code>-l </code><code>/tmp/elasticsearch/data/elasticsearch-cluster/nodes/0/indices/megacorp/</code>

<code>total 24</code>

<code>drwxr-xr-x 5 elasticsearch elasticsearch 4096 May 15 19:32 0</code>

<code>drwxr-xr-x 5 elasticsearch elasticsearch 4096 May 15 19:31 1</code>

<code>drwxr-xr-x 5 elasticsearch elasticsearch 4096 May 15 19:32 2</code>

<code>drwxr-xr-x 5 elasticsearch elasticsearch 4096 May 15 19:31 3</code>

<code>drwxr-xr-x 5 elasticsearch elasticsearch 4096 May 15 19:33 4</code>

<code>drwxr-xr-x 2 elasticsearch elasticsearch 4096 May 15 19:33 _state</code>

<code></code>

本文轉自殘劍部落格51CTO部落格,原文連結http://blog.51cto.com/cuidehua/1773685如需轉載請自行聯系原作者

cuizhiliang