8.8. 映射

简介:

8.8.1. 查看 _mapping

curl -XGET http://localhost:9200/information/news/_mapping?pretty		
			

数据结构如下

{
  "information" : {
    "mappings" : {
      "news" : {
        "_all" : {
          "analyzer" : "ik_max_word"
        },
        "properties" : {
          "content" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "ctime" : {
            "type" : "string"
          },
          "division_category_id" : {
            "type" : "long"
          },
          "tag" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "title" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          }
        }
      }
    }
  }
}
			

8.8.2. 删除 _mapping

curl -XDELETE http://localhost:9200/information/news/_mapping?pretty		
			

8.8.3. 创建 _mapping

curl -XPOST http://localhost:9200/information/news/_mapping?pretty -d'
{
    "news": {
        "_all": {
	       "analyzer": "ik_max_word",
	       "search_analyzer": "ik_max_word",
	       "term_vector": "no",
	       "store": "false"
        },
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'
			

8.8.4. 更新 mapping

注意:更新只能用于空的index,如果index中存在数据无法修改_mapping,必须重建,或者采用别名方案

更新已存在的 mapping,首先我们创建一个 _mapping

			
% curl "http://localhost:9200/information/article/_mapping?pretty"
{
  "information" : {
    "mappings" : {
      "article" : {
        "properties" : {
          "content" : {
            "type" : "text",
            "analyzer" : "ik_max_word"
          },
          "title" : {
            "type" : "text",
            "analyzer" : "ik_max_word"
          }
        }
      }
    }
  }
}

			
			

在这个 _mapping 中增加 ctime 字段,定义时间格式为 yyyy-MM-dd HH:mm:ss

			
% curl -XPOST http://localhost:9200/information/article/_mapping -d'
{
        "properties": {
        "ctime": {
               "type":   "date",
               "format": "yyyy-MM-dd HH:mm:ss"
           }
        }
}'

			
			

查看预期结果

			
% curl "http://localhost:9200/information/article/_mapping?pretty"  
{
  "information" : {
    "mappings" : {
      "article" : {
        "properties" : {
          "content" : {
            "type" : "text",
            "analyzer" : "ik_max_word"
          },
          "ctime" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss"
          },
          "title" : {
            "type" : "text",
            "analyzer" : "ik_max_word"
          }
        }
      }
    }
  }
}

			
			

8.8.5. 修改 _mapping

修改流程需要经历五步,首先创建新索引,创建新_mapping,导入数据,索引别名,删除旧索引。

当然你也可以删除重建索引,为什么会这么折腾呢?因为这样不用停止业务的情况下进行迁移。

			
# curl -XGET http://localhost:9200/information_v1/news/_mapping?pretty
{
  "information_v1" : {
    "mappings" : {
      "news" : {
        "_all" : {
          "analyzer" : "ik_max_word"
        },
        "properties" : {
          "content" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "ctime" : {
            "type" : "string"
          },
          "division_category_id" : {
            "type" : "long"
          },
          "tag" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "title" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          }
        }
      }
    }
  }
}

			
			

注意 ctime 数据类型定义错误,现在需要将它改为date日期类型。

创建 information_v2 索引

			
curl -XPUT http://localhost:9200/information_v2
curl -XPOST http://localhost:9200/information_v2/news/_mapping?pretty -d'
{
    "news": {
            "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        	},
		"properties": {
			"title": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
			},
			"content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
			},
			"tag": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            },
            "ctime": { 
				"type": "date"
		    }
        }
    }
}'	
			
			

查看全新 _mapping

			
# curl -XGET http://localhost:9200/information_v2/news/_mapping?pretty
{
  "information_v2" : {
    "mappings" : {
      "news" : {
        "_all" : {
          "analyzer" : "ik_max_word"
        },
        "properties" : {
          "content" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "ctime" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "tag" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          },
          "title" : {
            "type" : "string",
            "boost" : 8.0,
            "term_vector" : "with_positions_offsets",
            "analyzer" : "ik_max_word",
            "include_in_all" : true
          }
        }
      }
    }
  }
}
			
			

现在导入数据,导入完成后修改别名,将information 从 information_v1 切换到 information_v2

			
curl -XPOST http://localhost:9200/_aliases -d '
{
    "actions": [
        { "remove": {
            "alias": "information",
            "index": "information_v1"
        }},
        { "add": {
            "alias": "information",
            "index": "information_v2"
        }}
    ]
}
'
			
			

当所以切换完成information_v1 已经没有什么用处了,这时可以删除information_v1

			
curl -XDELETE http://localhost:9200/information_v1
			
			

8.8.6. 数据类型

string, date, long, double, boolean or ip.

8.8.6.1. date

elasticsearch 采用 ISO 8601 标准的 date 格式

				
{"LastUpdate": {
    "type" : "date",
    "format" : "yyyy-MM-dd HH:mm:ss"}
}				
				
				
				
{
  "mappings": {
    "my_type": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}				
				
			





原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
自然语言处理
如何定义标签词映射
如何定义标签词映射
|
SQL Java 数据库连接
26MyBatis - 输入映射和输出映射
26MyBatis - 输入映射和输出映射
69 0
|
12天前
映射关系(1-1 1-n n-n)
映射关系(1-1 1-n n-n)
20 1
|
3月前
|
XML JSON Java
2.映射关系(1-1 1-n n-n)
本文介绍了MyBatis框架中处理不同类型的关联关系(一对一、一对多、多对一及多对多)的方法。一对一关联可通过简单的属性字段映射实现;一对多关联需要在相关类中添加列表属性,并在Mapper文件中使用`<collection>`标签进行配置;多对一关联则需在Mapper文件中使用`<association>`标签来指定关联对象;对于多对多关联,通常需要定义一个中间类并在双方类中分别添加集合属性,通过`<collection>`标签完成映射。
|
4月前
|
Java API 数据库
使用JPA实现复杂数据模型映射
使用JPA实现复杂数据模型映射
|
搜索推荐 Java 程序员
对象映射你用哪个
对象映射你用哪个
92 0
|
SQL XML 存储
Hibernate框架【五】——基本映射——多对多映射
Hibernate框架【五】——基本映射——多对多映射
189 0
|
XML 存储 Java
Hibernate框架【三】——基本映射——一对一映射
Hibernate框架【三】——基本映射——一对一映射
80 0
直接取配置文件对应数据映射到对象中,可在代码中直接使用
直接取配置文件对应数据映射到对象中,可在代码中直接使用
|
机器学习/深度学习 算法 定位技术
一句话总结等距映射(流形学习)
一句话总结等距映射(流形学习)
一句话总结等距映射(流形学习)