敖胤

绳锯木断,水滴石穿;聚沙成塔,集腋成裘。

导航

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"
        }    
      }
    }
  }
}

posted on 2021-07-19 22:35  敖胤  阅读(719)  评论(0编辑  收藏  举报