elasticsearc之mapping的介紹

來源:互聯網
上載者:User

標籤:映射   es   mapping   分詞   elasticsearch   

    為了能夠把日期欄位處理成日期,把數字欄位處理成數字,把字串欄位處理成全文本(Full-text)或精確的字串值,Elasticsearch需要知道每個欄位裡面都包含了什麼類型。這些類型和欄位的資訊儲存(包含)在映射(mapping)中。

   

Elasticsearch支援以下簡單欄位類型:

類型 表示的資料類型
String string
Whole number byte, short, integer, long
Floating point float, double
Boolean boolean
Date date

 查看索引的mapping

[[email protected] es]# curl -GET "http://192.168.10.49:9200/site_test/test/_mapping"

{"site_test":     { "mappings":      {  "test":       {   "properties":        {        "@timestamp":           {              "type":"date",              "format":"strict_date_optional_time||epoch_millis"           },         "count":          {              "type":"long"           },          "brower":          {          "type":"string"          }        }      }    }  }}

原創地址:http://irow10.blog.51cto.com/2425361/1851001

上面的mapping中我們可以看到這個索引中有三個變數,分別是date類型,long類型和string類型

我們也可以用其他方式查看mapping。用head外掛程式查看索引的索引資訊

其中string類型的欄位,預設的,考慮到包含全文本,它們的值在索引前要經過分析器分析,並且在全文檢索搜尋此欄位前要把查詢語句做分析處理。也就是說只要定義為string的欄位預設會按照一個規則拆分欄位。如:hello100 會拆分成 hello  100兩個欄位。漢字會拆成一個個的。這樣在畫圖是就尷尬了!

是elasticsearch中head外掛程式查詢到的資料

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/87/09/wKioL1fSJSLBGdX6AAAnb89QWw0822.png" title="2.png" alt="wKioL1fSJSLBGdX6AAAnb89QWw0822.png" />

windows 8.1這個欄位就是上面mapping對應的brower。類型是string。

我們在kibana的setting查看下site_test各個欄位的資訊

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/87/0B/wKiom1fSJaqCXB2tAAApI6lelzM567.png" title="1.png" alt="wKiom1fSJaqCXB2tAAApI6lelzM567.png" />string類型的brower在analyzed(分析)這選項中打勾了!

現在我們用kibana畫圖試試看看會不會有什麼異常

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/87/0B/wKiom1fSJjzS1y7TAAAhbIwKbdc335.png" title="3.png" alt="wKiom1fSJjzS1y7TAAAhbIwKbdc335.png" />怎麼windows和8.1分成兩個欄位畫圖了,這不是我們想要的結果。那該如何解決呢?

自訂mapping

因為重新更改一個索引的mapping,必須建立索引。

[[email protected] es]# curl -XPOST http://192.168.10.49:9200/site_test{"acknowledged":true}

把建立的mapping放入一個檔案類:mapping

{  "test":       {   "properties":        {         "@timestamp":               {              "type":"date",              "format":"strict_date_optional_time||epoch_millis"                },          "count":                {               "type":"long"                },           "brower":                {               "type":"string",               "index": "not_analyzed"                }          }      }  }


對brower欄位額外加個屬性not_analyzed(不分詞)!

[[email protected] es]# curl -XPOST "http://192.168.10.49:9200/site_test/test/_mapping?pretty" -d ‘@mapping‘{  "acknowledged" : true}

這樣就把索引和mapping都定義好了 然後在匯入資料就不會出現分詞的情況!

備忘:如果有多個_type,定義一個就好,其他的會按照定義好的格式去自己擴建mapping。

當然我們也可以把上面兩步做到一步到位。如下:

curl -XPUT ‘http://192.168.10.49:9200/site_test‘ -d ‘@mapping‘

上面這條命令能完成建立索引和mapping。

本文出自 “寧靜致遠” 部落格,請務必保留此出處http://irow10.blog.51cto.com/2425361/1851001

elasticsearc之mapping的介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.