Updating existing field mappings
Other than where documented, existing field mappings cannot be updated. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index.
已有的字段映射类型不能更改。不能之前是long类型,现在想改成keyword类型。只能是重新建一个索引,设置正确的映射类型,然后执行reindex操作。
es支持的字段类型有:
array
在es中,没有专门的array类型。任何字段在默认情况下都可以包含0个或者多个值。比如说我们设置hobbies字段类型为keyword,则这个hobbies字段对应的值可以是"basketball",也可以是["football", "swimming"],如下:
put /my_index
put /my_index/_mapping/_doc
{
"properties":{
"hobbies":{
"type":"keyword"
}
}
}
put my_index/_doc/1
{
"hobbies": "basketball"
}
put my_index/_doc/2
{
"hobbies": ["football", "swimming"]
}
binary
binary类型的字段接受base64编码的字符串值,默认不存储也不可搜索。
put /my_index/_mapping/_doc
{
"properties":{
"blob":{
"type":"binary"
}
}
}
put /my_index/_doc/3
{
"blob":"5byg5LiJ"
}
如果执行where blob = '5byg5LiJ'的搜索,如下,会报错,提示Binary fields do not support searching
get /my_index/_search
{
"query":{
"term":{
"blob":{
"value":"5byg5LiJ"
}
}
}
}
设置binary类型时,有两个参数可以用:doc_values、store,都默认为false。以后再讲是做什么的。
range
range是一个总类,就好像numeric一样,其中具体类型有integer_range、long_range、float_range、double_range、date_range、ip_range。
给my_index添加一个integer_range类型的字段
put /my_index/_mapping/_doc
{
"properties":{
"age_range":{
"type":"integer_range"
}
}
}
插入一条数据(某狼性企业招工年龄范围)
put /my_index/_doc/4
{
"age_range":{
"gte":18,
"lt":30
}
}
range类型的字段怎么查呢?可以用range query,也可以用term query。
range query:
get my_index/_search
{
"query" : {
"range" : {
"age_range" : {
"gte" : 20,
"lte" : 40,
"relation" : "within"
}
}
}
}
用range query查询range类型字段时,可以额外指定一个relation参数,默认值是intersects,其他可选值有within、contains。intersects意思是只要文档range类型字段值指定的范围和range query指定的范围有交叉,就能查出来。within和contains表示只有文档range类型字段值指定的范围和range query指定的范围是包含关系时,才能查出来。具体within和contains各表示谁包谁,这个不好记,用的时候测一下就好了。
term query:
get /my_index/_search
{
"query" : {
"term" : {
"age_range" : 27
}
}
}
设置range类型时,有4个参数可以用:coerce、boost、index、store。
coerce表示是否尝试把字符串转成数字,以及是否尝试把浮点数转成整数,默认值是true。
boost默认值为1.0。
index,表示这个字段是否是可搜索的,默认值是true。
store,默认值是false。
boolean
put /my_index/_mapping/_doc
{
"properties":{
"married":{
"type":"boolean"
}
}
}
put /my_index/_doc/5
{
"married":false
}
可以接受值true、false,也可以接受对应的字符串"true"、"false"。
设置boolean类型时,有5个参数可以用:boost、doc_values、index、null_value、store。
boost默认值为1.0,doc_values默认值是true,index默认值是true。
null_value表示当前字段值为null时启用的值,可以为null或者以上任意一种布尔值,默认值是null。
store默认值是false。
date
见https://www.cnblogs.com/koushr/p/9498888.html
geo_point、geo_shape
这个是地理位置的,较少用,用到的时候再看。
ip
put /my_index/_mapping/_doc
{
"properties":{
"ip_addr":{
"type":"ip"
}
}
}
ip类型既可以接受ipv4数据,又可以接受ipv6数据。
设置ip类型时,有5个参数可以用,boost、doc_values、index、null_value、store。
boost默认值为1.0,doc_values默认值是true,index默认值是true,null_value默认值是null,store默认值是false。
keyword
设置keyword类型时,boost默认值为1.0,doc_values默认值是true,index默认值是true,null_value默认值是null,store默认值是false,
eager_global_ordinals,表示在refresh时是否加载全局序号,默认值是false。在terms aggregations操作中经常使用的字段,eager_global_ordinals最好设置为true。
fields,
ignore_above,