ES6-映射(mapping)

1.mapping映射概述

咱們習慣上對ElasticSearch和數據庫作了一些對比,索引(index)至關於數據庫,類型(type)至關於數據表,映射(Mapping)至關於數據表的表結構。數據庫

ElasticSearch中的映射(Mapping)用來定義一個文檔,能夠定義所包含的字段以及字段的類型、分詞器及屬性等等。數組

映射能夠分爲動態映射和靜態映射:app

動態映射:咱們知道,在關係數據庫中,須要事先建立數據庫,而後在該數據庫實例下建立數據表,而後才能在該數據表中插入數據。而ElasticSearch中不須要事先定義映射(Mapping),文檔寫入ElasticSearch時,會根據文檔字段自動識別類型,這種機制稱之爲動態映射。spa

靜態映射:在ElasticSearch中也能夠事先定義好映射,包含文檔的各個字段及其類型等,這種方式稱之爲靜態映射。設計

2.動態映射

咱們新建立一個索引informationcode

PUT information

建立成功響應:orm

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "information"
}

查看mapping結構:對象

GET information/_mapping

查詢到mapping結構爲空:索引

{
  "information": {
    "mappings": {}
  }
}

插入文檔信息:ip

PUT information/record/1
{
  "ip":"10.192.168.4",
  "attack_type":"scan_ip",
  "count": 400,
  "create_time":"2018-03-07 00:00:00"
}

建立成功:

{
  "_index": "information",
  "_type": "record",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

而後咱們再次查詢mapping結構:

{
  "information": {
    "mappings": {
      "record": {
        "properties": {
          "attack_type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "count": {
            "type": "long"
          },
          "create_time": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "ip": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

在添加文檔時ElasticSearch會推測添加文檔中每一個屬性字段是什麼類別:

record推測結果:

attack_type:text類型

count:long類型

create_time:text類型(實際應該爲date類型,推測並非很是準確)

ip:text類型

3.動態映射規則

動態映射能夠幫助咱們在建立索引後直接將文檔數據寫入ElasticSearch,讓咱們儘快享受到ElasticSearch檢索功能。在實際項目中,若是在導入數據前不能肯定包含哪些字段或者不方便肯定字段類型,可使用動態映射。當向ElasticSearch寫入一個新文檔時,須要一個以前沒有的字段,會經過動態映射來推斷該字段類型。

JSON數據 自動推測的類型
null 沒有字段被添加
true或false boolean型
小數 float型
數字 long型
日期 date或text
字符串 text
數組 由數組第一個非空值決定
JSON對象 object類型

4.靜態映射

動態映射的自動類型推測功能並非100%正確的,這就須要靜態映射機制。靜態映射與關係數據庫中建立表語句類型,須要事先指定字段類型。相對於動態映射,靜態映射能夠添加更加詳細字段類型、更精準的配置信息等。

新建靜態映射:

首先刪除已經建立的information索引,而後再建立索引是定義mapping結構

PUT information
{
  "mappings": {
    "record":{
      "properties": {
        "ip":{"type": "text"},
        "count":{"type": "long"},
        "create_type":{"type": "date"},
        "i_type":{"type": "text"}
      }
    }
  }
}

執行成功:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "information"
}

查看索引mapping結構:

GET information/_mapping

響應結果:

{
  "information": {
    "mappings": {
      "record": {
        "properties": {
          "count": {
            "type": "long"
          },
          "create_type": {
            "type": "date"
          },
          "i_type": {
            "type": "text"
          },
          "ip": {
            "type": "text"
          }
        }
      }
    }
  }
}

類型是Elasticsearch的一個設計失誤,6.0開始後面的版本將再也不支持,在6.x中建立的索引只容許每一個索引有單一類型。任何名字均可以用於這個類型,可是隻能有一個。 

相關文章
相關標籤/搜索