ES Mapping ,1 字段类型

本文详细介绍了Elasticsearch中的Mapping,包括动态映射和显式映射,以及基本类型如字符串、数值、日期、布尔和二进制的配置。还探讨了复杂类型,如对象、数组、嵌套类型及其参数设置,以及特殊类型如IP、range、token_count和geo_point的应用。这些内容对于理解Elasticsearch中数据存储和索引至关重要。
摘要由CSDN通过智能技术生成

mapping

映射是定义一个文档以及其所包含的字段如何被存储和索引的方法。

  • 动态映射(dynamic mapping)
  • 显式映射(explicit mappings)

maping 创建

PUT index1
{
	"mappings":{		// 类型定义关键字
		"properties":{
			"索引字段1":{
				"type" : "text"  // 字段类型定义
			} , 
			"索引字段2":{
				"type" : "text"
				"index" : false  // 不索引,不能查询的字段
			}"索引字段3":{
				"type" : "text"
				"null_value" : "null"  // 可以索引null 值
			} 
			
		}
	}
}
properties
  • type
  • index
  • null_value : “null”
  • copy_to: 多字段合共

copy_to 用法

PUT index1
{
	"mappings":{		
		"properties":{
			"first_name":{
				"type" : "text"  
				"copy_to" : "full_name"
			} ,   
			"last_name":{
				"type" : "text"
				"copy_to" : "full_name"  
			}}
	}
}

PUT index1/_doc/1
{
	"first_name": "xue"
	"last_name" : "xiaoxiao"
}

GET index1/_search?q=full_name(xue xiaoxiao)
结果包含 xue xiaoxiao

一 基本类型

字段类型定义关键字
字符串text , keywords
数值long , integer , short , byte , double ,float
日期long , int
布尔false , “false”, “off”, “no”, “0”, “” (empty string), 0, 0.0 。
二进制binary 二进制类型以 Base64 编码方式接收一个二进制值,二进制类型字段默认不存储,也不可搜索。
1. 字符串
  • text , (分词后,存入数据结构)
  • keyword,(不分词,存入数据结构)
  • string,(5.x废弃)

参数:
analyzer、boost、doc_values、fielddata、fields、ignore_above、include_in_all、index、index_options、norms、null_value、position_increment_gap、store、search_analyzer、search_quote_analyzer、similarity、term_vector

2. 数值

数值类型包括: long, integer, short, byte, double, float 。

参数:
coerce、boost、doc_values、ignore_malformed、include_in_all、index、null_value、precision_step、store

3. 日期
  • 日期字符串

  • 毫秒时间戳 (long 类型)

  • 秒时间戳,(int 类型)

  • 日期类型, 默认被转为 UTC 毫秒时间戳,以 long型存储。

  • 日期类型, 默认 format : “strict_date_optional_time||epoch_millis”

  • 日期类型, 推荐 format : “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”

	"properties": {
	        "字段1": {
	          	"type":   "date",
	        	 "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
	        }
	 }

参数:
boost、doc_values、format、ignore_malformed、include_in_all、index、null_value、precision_step、store

4. 布尔
	# 不为假的值,即为真
	# False 关键字:`false`  、`"false"` 、 `"off"` 、  `"no"`
	# False 特殊情况,Empty string:""
	# Flase 数字情况: 0 , 0.0 ,  "0"				

参数:
boost、doc_values、index、null_value、store

5. 二进制, binary (Base64 编码)

二进制类型以 Base64 编码方式接收一个二进制值,二进制类型字段默认不存储,也不可搜索。
参数:doc_values、store

二 复杂类型

  • 对象
  • 数组
  • 嵌套(nested)
  • 对象数组

1. 对象

PUT my_index/my_type/1
{ 
  "region": "US",
  "manager": { 
    "age":     30,
    "name": { 
      "first": "John",
      "last":  "Smith"
    }
  }
}
// 转换为
{
  "region":   "US",
  "manager.age": 30,
  "manager.name.first": 	"John",
  "manager.name.last":  	"Smith"  		//层级结构被以 "." 来表示。
}

2. 数组 Array

字符串数组: [ "one", "two" ]
数字数组:    [ 1, 2 ]
数组数组:    [ 1, [ 2, 3 ]] which is the equivalent of [ 1, 2, 3 ]
对象数组:   [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }] 
  • 数组类型,要求数组元素的数据类型必须一致。
  • 数组元素的数据类型,将会由其第一个元素的数据类型决定。
  • 对象数组,在 ES 内部将会被转换为 “多值” 的扁平数据类型。后面将会详解这一点。

例如:

PUT my_index/my_type/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}
// 转换为
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

3. 对象数组

对象数组在 ES 内部,会把所有数组元素(即对象)合并,对象中的每一个字段被索引为一个 “多值” 字段。
这将导致每个数组元素(对象)内部的字段关联性丢失,解决的方法是使用 nested 类型。

例如:

PUT my_index/my_type/1
{ 
  "region": "US",
  "manager": { 
    "age":     30,
    "name": [
    { 
      "first": "John",
      "last":  "Smith"
    },
    { 
      "first": "Bob",
      "last":  "Leo"
    }
    ]
  }
}


// 转换为:
{
  "region":             "US",
  "manager.age":        30,
  "manager.name.first": "John Bob",
  "manager.name.last": "Smith Leo" 
}


// 如果我们搜索:
"bool": {
      "must": [
        { "match": { "manager.name.first": "John" }},   // John Smith
        { "match": { "manager.name.last": "Leo"}}       // Bob Leo
      ]
}

//这将会导致导致文档被命中,显然,John Smith 、Bob Leo 两组字段它们内在的关联性都丢失了
参数:

dynamic、enabled、include_in_all、properties

4. 嵌套(nested)

嵌套类型是一个特殊对象类型,嵌套类型允许对对象数组的每一个元素(对象)相互独立的进行查询,也即他们不会被合并为一个对象。

嵌套类型的文档可以:

用 nested 查询来查询
用 nested来分析以及 reverse_nested 来聚合
用 nested sorting 来排序
用 nested inner hits 来检索或高亮
例如:

PUT my_index/my_type/1
{ 
  "region": "US",
  "manager": { 
    "age":     30,
    "name": [
    { 
      "first": "John",
      "last":  "Smith"
    },
    { 
      "first": "Bob",
      "last":  "Leo"
    }
    ]
  }
}

// 转换为:
{
  "region":             "US",
  "manager.age":        30,
  {
      "manager.name.first": "John",
      "manager.name.last": "Smith"
  },
  {
      "manager.name.first": "Bob",
      "manager.name.last": "Leo" 
  }
}

// 如果我们搜索:
"bool": {
      "must": [
        { "match": { "manager.name.first": "John" }},   // John Smith
        { "match": { "manager.name.last": "Leo"}}       // Bob Leo
      ]
}

// 这样的查询将不能命中文档!!!

参数:
dynamic、include_in_all、properties

三 特殊类型

字段类型定义关键字
IPIPV4 数据类型其实质是个 long 类型
Rangeinteger_range,float_range , long_range , double_range , date_range
token_count
geo point

IP类型

IPV4 数据类型其实质是个 long 类型,不过其能接收一个 IPV4 地址并且将他转换为 long 类型存放。
参数:
boost、doc_values、include_in_all、index、null_value、precision_step、store

range类型

range类型的使用场景:比如前端的时间选择表单、年龄范围选择表单等。

类型范围
integer_range-2 ^31 ~ 2^31-1
float_range32-bit IEEE 754
long_range-2 ^63 ~ 2^63-1
double_range64-bit IEEE 754
date_range64位整数,毫秒计时
PUT range_index
{
	"mappings": {
		"my_type": {
			"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/my_type/1
{
  "expected_attendees" : { 
    "gte" : 10,
    "lte" : 20
  },
  "time_frame" : { 
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

token_count类型

token_count用于统计词频:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "length": { 
              "type":     "token_count",
              "analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

PUT my_index/my_type/1
{ "name": "John Smith" }

PUT my_index/my_type/2
{ "name": "Rachel Alice Williams" }

GET my_index/_search
{
  "query": {
    "term": {
      "name.length": 3 
    }
  }
}

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

参考

  • https://blog.csdn.net/ZYC88888/article/details/83027458
  • https://blog.csdn.net/napoay/article/details/73100110#21-all
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值