3.ES----IndexTemplate 和 dynamicTemplate

1.索引模板 IndexTemplate

 

可以看到setting是用來設定集羣存儲的,mapping是用來定義具體的索引設置.

這裏需要注意一下order 的值

 

這個規定,使我們可以找到到底使用的是哪個索引模板中的設置.  上面的就是順序.

也就是說先去配置默認的設定,如果有模板則去尋找較低的order,用較低的order的模板覆蓋默認模板,

再去找order較高的模板,然後用它將order較低的模板覆蓋掉,如果創建索引時,顯示的指定了mappings,則用其覆蓋掉之前的設定,

總之.優先級就是顯示指定的mappings > order較高的mappings >order較低的mappings >默認的mappings

看下面的demo:

 

 

 

查看mapping:

 

可以看到someDate字段沒有被探測爲日期類型,而是設置成了text,而someNumber則被numeric_detection 探測出是數字類型,被設置成了long.

查看settings

  

 

這時可以看到 分片數與備份數都是2

 

下面是手動指定settings

 

可以看到,5已經設置成功

 

2. dynamicTemplate

 

DynamicTemplate 是更加聚焦於文本的,可以爲每一個字段,或者說是每一種字段(通過匹配規則數組),設置具體的mapping,類似於關係型數據庫的mapping了,而且可以更加符合我們的習慣

 

eg2:

 

有點像觸發器,或者是存儲過程

 

這裏有個問題:在執行上面步驟後,通過

 GET my_index/_doc/1  查詢得到的結果:

 

沒有full_name字段,但是通過

GET my_index/_search?q=full_name:JOhN

GET my_index/_search?q=full_name:lufei

都可以查到該文檔,

帶着這個問題我們查下文檔:

主要顯示 3點:

.相當於複製到一個數組類型的字段中,而不是覆蓋掉

The copy_to parameter allows you to copy the values of multiple fields into a group field, which can then be queried as a single field. For instance, the first_name and last_name fields can be copied to the full_name field,

copy_to參數允許您將多個字段的值複製到一個組字段中,然後可以作爲單個字段進行查詢。例如,可以將first_name和last_name字段複製到full_name字段.

二. 這個是對第一點的解釋,其實就是相當於存儲了一個數組

The first_name and last_name fields can still be queried for the first name and last name respectively, but the full_name field can be queried for both first and last names.

“名”和“姓”字段仍然可以分別查詢“名”和“姓”,但“全名”字段可以同時查詢“名”和“姓”。

 

三.總結

It is the field value which is copied, not the terms (which result from the analysis process).

它只是複製了值,但不是字段(這是分詞過程的結果)

 

The original _source field will not be modified to show the copied values.

不會修改原始源字段以顯示覆制的值。(這就是問題的答案了)

那麼這又會引發一個問題,如果文檔中存在了這個字段,值還是原來的值,mapping中是怎樣設定的呢?

(先留下懸念)

 

The same value can be copied to multiple fields, with "copy_to": [ "field_1", "field_2" ]

同一個值可以複製到多個字段,其中“copy_to”:[“field_1”,“field_2”]

 

You cannot copy recursively via intermediary fields such as a copy_to on field_1 to field_2 and copy_to on field_2 to field_3 expecting indexing into field_1 will eventuate in field_3, instead use copy_to directly to multiple fields from the originating field.

不能通過中介字段遞歸複製,如FieldIto字段Field1到Field2,Copyto to Field2到Field3,以期盼field_1 的值能存在field_3中,來代替直接複製到多個字段的用法,   也就是說只能存在兩級,

 

四,我們回過頭來,來測試一下之前留下的懸念:

如果文檔中存在了這個字段,值還是原來的值,mapping中是怎樣設定的呢?

首先,我們在原來的索引中添加一個帶full_name字段(數字型)的文檔

 

 

 

查看mapping

 

沒變化,只有一個full_name字段的mapping設定且與原來的一樣

 那麼我們再測試先讓顯示的full_name字段出現,(之前是通過copy_to隱式的創建了full_name的mapping)

刪除原來的索引,再創建,起初的mapping顯示:

 

這時還沒有 full_name字段的mapping,然後創建一個文檔只有full_name字段,還是數值類型的;

 

mapping 顯示的是long型

 

再創建一個 文檔讓其通過copy_to 過程

 

報錯了,也就是說其實通過copy_to建的字段mapping與通過文檔默認生成的是一個,至於爲什麼現在報錯了而之前沒報錯,是因爲 long類型可以轉爲text類型,但text類型不能自動轉爲long類型.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章