Elasticsearch整理之Field datatype

本文详细介绍了Elasticsearch中的各种数据类型,包括text、keyword、数字类型、date、boolean等,并提供了具体示例说明每种类型的应用场景。

一、Field datatype

1.  text类型

ES的新版本不再支持string,而是将string分为text和keyword。

text 数据类型被用来索引长文本,比如说电子邮件的主体部分或者一款产品的介绍。这些文本会被分析,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许 ES来检索这些词语。text 数据类型不能用来排序和聚合。

2. keyword类型

keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。

3. 数字类型

                  类型取值范围
long-2^63至2^63-1
integer-2^31至2^31-1
short-32,768至32768
byte-128至127
double64位双精度
float32位单精度
half_float16位半精度
scaled_float缩放类型的的浮点数(比如价格只需要精确到小数点后两位,price为57.34的字段缩放因子为100,存起来就是5734)

其中,使用scaled_float时需要指定缩放因子

"price": {
      "type": "scaled_float",
      "scaling_factor": 100
 }

4. date类型

日期格式可以是以下三种类型:

(1)包含日期格式的字符串:"2015-01-01" or "2015/01/01 12:10:30".

(2)用long表示的毫秒数 milliseconds-since-the-epoch.

(3)用integer表示的秒数

推荐自己设定日期格式,更多的格式可参照(https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-date-format.html

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

5. boolean类型

值为true、false或"true"、"false"

6. binary类型

binary类型接受base64编码的字符串,默认不存储也不可搜索。

7. ip类型

用于存储IPv4或IPv6的地址

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

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

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

8. geo point 类型

用于存储地理位置的经纬度

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

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

PUT my_index/my_type/2
{
  "text": "Geo-point as a string",
  "location": "41.12,-71.34" 
}

PUT my_index/my_type/3
{
  "text": "Geo-point as a geohash",
  "location": "drm3btev3e86" 
}

PUT my_index/my_type/4
{
  "text": "Geo-point as an array",
  "location": [ -71.34, 41.12 ] 
}

GET my_index/_search
{
  "query": {
    "geo_bounding_box": { 
      "location": {
        "top_left": {
          "lat": 42,
          "lon": -72
        },
        "bottom_right": {
          "lat": 40,
          "lon": -74
        }
      }
    }
  }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值