ElasticSearch數(shù)據(jù)類型

1. 字段類型概述

一級分類 二級分類 具體類型
核心類型 字符串類型 string,text,keyword
整數(shù)類型 integer,long,short,byte
浮點(diǎn)類型 double,float,half_float,scaled_float
邏輯類型 boolean
日期類型 date
范圍類型 range
二進(jìn)制類型 binary
復(fù)合類型 數(shù)組類型 array
對象類型 object
嵌套類型 nested
地理類型 地理坐標(biāo)類型 geo_point
地理地圖 geo_shape
特殊類型 IP類型 ip
范圍類型 completion
令牌計數(shù)類型 token_count
附件類型 attachment
抽取類型 percolator

2. 字符串類型

2.1 string

string類型在ElasticSearch 舊版本中使用較多,從ElasticSearch 5.x開始不再支持string,由text和keyword類型替代。

2.2 text

當(dāng)一個字段是要被全文搜索的,比如Email內(nèi)容、產(chǎn)品描述,應(yīng)該使用text類型。設(shè)置text類型以后,字段內(nèi)容會被分析,在生成倒排索引以前,字符串會被分析器分成一個一個詞項。text類型的字段不用于排序,很少用于聚合。

2.3 keyword

keyword類型適用于索引結(jié)構(gòu)化的字段,比如email地址、主機(jī)名、狀態(tài)碼和標(biāo)簽。如果字段需要進(jìn)行過濾(比如查找已發(fā)布博客中status屬性為published的文章)、排序、聚合。keyword類型的字段只能通過精確值搜索到。

3. 整數(shù)類型

類型 取值范圍
byte -128~127
short -32768~32767
integer -231~231-1
long -263~263-1

在滿足需求的情況下,盡可能選擇范圍小的數(shù)據(jù)類型。比如,某個字段的取值最大值不會超過100,那么選擇byte類型即可

4. 浮點(diǎn)類型

類型 取值范圍
doule 64位雙精度IEEE 754浮點(diǎn)類型
float 32位單精度IEEE 754浮點(diǎn)類型
half_float 16位半精度IEEE 754浮點(diǎn)類型
scaled_float 縮放類型的的浮點(diǎn)數(shù)

對于float、half_float和scaled_float,-0.0和+0.0是不同的值,使用term查詢查找-0.0不會匹配+0.0,同樣range查詢中上邊界是-0.0不會匹配+0.0,下邊界是+0.0不會匹配-0.0。

其中scaled_float,比如價格只需要精確到分,price為57.34的字段縮放因子為100,存起來就是5734
優(yōu)先考慮使用帶縮放因子的scaled_float浮點(diǎn)類型。

5. date類型

人類使用的計時系統(tǒng)是相當(dāng)復(fù)雜的:秒是基本單位, 60秒為1分鐘, 60分鐘為1小時, 24小時是一天……如果計算機(jī)也使用相同的方式來計時, 那顯然就要用多個變量來分別存放年月日時分秒, 不停的進(jìn)行進(jìn)位運(yùn)算, 而且還要處理偶爾的閏年和閏秒以及協(xié)調(diào)不同的時區(qū). 基于”追求簡單”的設(shè)計理念, UNIX在內(nèi)部采用了一種最簡單的計時方式:

  • 計算從UNIX誕生的UTC時間1970年1月1日0時0分0秒起, 流逝的秒數(shù).;
  • UTC時間1970年1月1日0時0分0秒就是UNIX時間0, UTC時間1970年1月2日0時0分0秒就是UNIX時間86400;
  • 這個計時系統(tǒng)被所有的UNIX和類UNIX系統(tǒng)繼承了下來, 而且影響了許多非UNIX系統(tǒng);

日期類型表示格式可以是以下幾種:

  1. 日期格式的字符串,比如 “2018-01-13” 或 “2018-01-13 12:10:30” ;
  2. long類型的毫秒數(shù)( milliseconds-since-the-epoch,epoch就是指UNIX誕生的UTC時間1970年1月1日0時0分0秒) ;
  3. integer的秒數(shù)(seconds-since-the-epoch);

ElasticSearch 內(nèi)部會將日期數(shù)據(jù)轉(zhuǎn)換為UTC,并存儲為milliseconds-since-the-epoch的long型整數(shù)。
例子:日期格式數(shù)據(jù)

  1. 創(chuàng)建索引
DELETE test

PUT test
{
  "mappings":{
    "my":{
      "properties": {
        "postdate":{
          "type":"date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
  1. 寫入文檔
PUT test/my/1
{
  "postdate":"2018-01-13"
}
PUT test/my/2
{
  "postdate":"2018-01-01 00:01:05"
}
PUT test/my/3
{
  "postdate":"1420077400001"
}
  1. 批量查詢
GET test/my/_mget
{
  "ids":["1","2","3"]
}

{
  "docs": [
    {
      "_index": "test",
      "_type": "my",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "postdate": "2018-01-13"
      }
    },
    {
      "_index": "test",
      "_type": "my",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "postdate": "2018-01-01 00:01:05"
      }
    },
    {
      "_index": "test",
      "_type": "my",
      "_id": "3",
      "_version": 2,
      "found": true,
      "_source": {
        "postdate": "1420077400001"
      }
    }
  ]
}

6. boolean類型

邏輯類型(布爾類型)可以接受true/false/”true”/”false”值

  1. 先刪除已經(jīng)存在的索引,再創(chuàng)建
DELETE test

PUT test
{
  "mappings":{
    "my":{
      "properties": {
        "empty":{"type":"boolean"}
      }
    }
  }
}
  1. 添加文檔
PUT test/my/1
{
  "empty":"true"
}

PUT test/my/2
{
  "empty":false
}
  1. 查看文檔
GET test/my/_mget
{
  "ids":["1","2"]
}

{
  "docs": [
    {
      "_index": "test",
      "_type": "my",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "empty": "true"
      }
    },
    {
      "_index": "test",
      "_type": "my",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "empty": false
      }
    }
  ]
}

7. binary類型

二進(jìn)制字段是指用base64來表示索引中存儲的二進(jìn)制數(shù)據(jù),可用來存儲二進(jìn)制形式的數(shù)據(jù),例如圖像。默認(rèn)情況下,該類型的字段只存儲不索引。二進(jìn)制類型只支持index_name屬性。

8. array類型

在ElasticSearch中,沒有專門的數(shù)組(Array)數(shù)據(jù)類型,但是,在默認(rèn)情況下,任意一個字段都可以包含0或多個值,這意味著每個字段默認(rèn)都是數(shù)組類型,只不過,數(shù)組類型的各個元素值的數(shù)據(jù)類型必須相同。在ElasticSearch中,數(shù)組是開箱即用的(out of box),不需要進(jìn)行任何配置,就可以直接使用。

在同一個數(shù)組中,數(shù)組元素的數(shù)據(jù)類型是相同的,ElasticSearch不支持元素為多個數(shù)據(jù)類型:[ 10, “some string” ],常用的數(shù)組類型是:

  1. 字符數(shù)組: [ “one”, “two” ] ;
  2. 整數(shù)數(shù)組: productid:[ 1, 2 ];
  3. 對象(文檔)數(shù)組: “user”:[ { “name”: “Mary”, “age”: 12 }, { “name”: “John”, “age”: 10 }],ElasticSearch內(nèi)部把對象數(shù)組展開為 {“user.name”: [“Mary”, “John”], “user.age”: [12,10]};

9. object類型

JSON天生具有層級關(guān)系,文檔會包含嵌套的對象

DELETE test

PUT test

PUT test/my/1
{
  "employee":{
    "age":30,
    "fullname":{
      "first":"hadron",
      "last":"cheng"
    }
  }
}

上面文檔整體是一個JSON,JSON中包含一個employee,employee又包含一個fullname

GET test/_mapping

{
  "test": {
    "mappings": {
      "my": {
        "properties": {
          "employee": {
            "properties": {
              "age": { "type": "long"},
              "fullname": {
                "properties": {
                  "first": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "last": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

10. ip類型

ip類型的字段用于存儲IPv4或者IPv6的地址

  1. 創(chuàng)建索引
DELETE test

PUT test
{
  "mappings": {
    "my":{
      "properties": {
        "nodeIP":{
          "type": "ip"
        }
      }
    }
  }
}
  1. 查詢字段
GET test/_search
{
  "query": {
    "term": {
      "nodeIP": "192.168.0.0/16"
    }
  }
}

{
  "took": 111,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "my",
        "_id": "1",
        "_score": 1,
        "_source": {
          "nodeIP": "192.168.1.2"
        }
      }
    ]
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容