ElasticSearch Field數據類型


官網地址:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/mapping-types.html#_geo_datatypes

核心數據類型

官方文檔:

Core datatypes

String—字符串

  1. text:一般用於全文檢索。會將當前field進行分詞。
  2. keyword:不會分詞。

Numeric—數字

數據類型 說明
long A signed 64-bit integer with a minimum value of -263 and a maximum value of 263-1
integer A signed 32-bit integer with a minimum value of -231 and a maximum value of 231-1
short A signed 16-bit integer with a minimum value of -32,768 and a maximum value of 32,767.
byte A signed 8-bit integer with a minimum value of -128 and a maximum value of 127
double A double-precision 64-bit IEEE 754 floating point number, restricted to finite values
float A single-precision 32-bit IEEE 754 floating point number, restricted to finite values
half_float A half-precision 16-bit IEEE 754 floating point number, restricted to finite values. 精度是float的一半
scaled_float A finite floating point number that is backed by a long, scaled by a fixed double scaling factor. 表示方式:long—235 + scaled—100 ==> 2.35

Date—時間

date:可以指定時間的具體格式

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

Boolean—布爾

true/false

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "is_published": {
          "type": "boolean"
        }
      }
    }
  }
}

POST my_index/_doc/1
{
  "is_published": "true" 
}

GET my_index/_search
{
  "query": {
    "term": {
      "is_published": true 
    }
  }
}

Binary—二進制

一個Base64編碼的二進制字符串

The binary type accepts a binary value as a Base64 encoded string. The field is not stored by default and is not searchable.

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

Range—范圍數

范圍類型在賦值時無需指定具體數值,只需要指定一個范圍。指定方式:lt、gt、lte、gte...

數據類型 描述
integer_range A range of signed 32-bit integers with a minimum value of -231 and maximum of 231-1.
float_range A range of single-precision 32-bit IEEE 754 floating point values.
long_range A range of signed 64-bit integers with a minimum value of -263 and maximum of 263-1.
double_range A range of double-precision 64-bit IEEE 754 floating point values.
date_range A range of date values represented as unsigned 64-bit integer milliseconds elapsed since system epoch.
ip_range A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.
PUT range_index
{
  "settings": {
    "number_of_shards": 2
  },
  "mappings": {
    "_doc": {
      "properties": {
        "expected_attendees": {
          "type": "integer_range"
        },
        "time_frame": {
          "type": "date_range", 
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

PUT range_index/_doc/1?refresh
{
  "expected_attendees" : { 
    "gte" : 10,
    "lte" : 20
  },
  "time_frame" : { 
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

其他數據類型

GEO—經緯度

geo_point:經緯度

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "text": "Geo-point as an object",
  "location": { 
    "lat": 41.12,
    "lon": -71.34
  }
}

IP—IP地址

用於存儲IP地址

An ip field can index/store either IPv4 or IPv6 addresses.

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "ip_addr": {
          "type": "ip"
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
  "ip_addr": "192.168.1.1"
}

GET my_index/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.0.0/16"
    }
  }
}

示例

創建一個book的索引

PUT /book
{
  "settings": {
      // 分片數
      "number_of_shards": 5,
      // 備份數
      "number_of_replicas": 1		    
  },
    //指定數據結構
    "mappings": {
      //聲明一個type
      "IT":{
        //文檔存儲的filed
        "properties":{
          //filed屬性名
          "name":{
            //field數據類型
            "type":"text",
            //分詞器
            "analyzer":"ik_max_word",
            //當前field可以作為查詢條件
            "index":true,
            "store":false
        },
        "author":{
            "type":"keyword"
        },
        "count":{
            "type":"long"
        },
        "onsale":{
            "type":"date",
            //指定時間的格式
            "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
        },
        "descr":{
            "type":"text"
        },
        "price":{
            "type":"double"
        }    
      }
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM