欢迎投稿

今日深度:

Elasticsearch整理之Field datatype,

Elasticsearch整理之Field datatype,


一、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
double 64位双精度
float 32位单精度
half_float 16位半精度
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
        }
      }
    }
  }
}

 

 

www.htsjk.Com true http://www.htsjk.com/Elasticsearch/35307.html NewsArticle Elasticsearch整理之Field datatype, 一、Field datatype 1.  text类型 ES的新版本不再支持string,而是将string分为text和keyword。 text 数据类型被用来索引长文本,比如说电子邮件的主体部分或者一款...
相关文章
    暂无相关文章
评论暂时关闭