elasticsearch之mapping配置




本文主要记录es的schema mapping的一些配置项

mapping定义

{
  "mappings": {
    "post": {
      "properties": {                
        "id": {"type":"long", "store":"yes", "precision_step":"8" },
        "name": {"type":"string", "store":"yes", "index":"analyzed" },
        "published": {"type":"date", "store":"yes", "precision_step":"8" },
        "contents": {"type":"string", "store":"no", "index":"analyzed" }             
      }
    }
  }
}

或者

{
  "book" : {
    "_index" : { 
      "enabled" : true 
    },
    "_id" : {
      "index": "not_analyzed", 
      "store" : "yes"
    },
    "properties" : {
      "author" : {
        "type" : "string"
      },
      "characters" : {
        "type" : "string"
      },
      "copies" : {
        "type" : "long",
        "ignore_malformed" : false
      },
      "otitle" : {
        "type" : "string"
      },
      "tags" : {
        "type" : "string"
      },
      "title" : {
        "type" : "string",
        "fields":{
          "sort":{"type":"string","index":"not_analyzed"}
        }
      },
      "year" : {
        "type" : "long",
        "ignore_malformed" : false,
        "index" : "analyzed"
      },
      "available" : {
        "type" : "boolean"
      }
    }
  }
}

属性解说

  • index
    可选值为analyzed(默认)和no,如果是字段是字符串类型的,则可以是not_analyzed.

  • store
    可选值为yes或no,指定该字段的原始值是否被写入索引中,默认为no,即结果中不能返回该字段。

  • boost
    默认为1,定义了文档中该字段的重要性,越高越重要

  • null_value
    如果一个字段为null值(空数组或者数组都是null值)的话不会被索引及搜索到,null_value参数可以显示替代null values为指定值,这样使得字段可以被搜索到。

  • include_in_all
    指定该字段是否应该包括在_all字段里头,默认情况下都会包含。

mapping操作

新建mapping

curl -s -XPOST '192.168.99.100:9200/library' --data-binary @mapping.json

更新mapping

curl -XPOST '192.168.99.100:9200/library/book/_mapping' -d'
{
    "book": {
        "properties": {
            "description": {
                "type": "string", 
                "store": "yes", 
                "index": "analyzed"
            }
        }
    }
}
'

查看mapping

curl -XGET '192.168.99.100:9200/library/book/_mapping?pretty'

返回

{
  "library" : {
    "mappings" : {
      "book" : {
        "properties" : {
          "author" : {
            "type" : "string"
          },
          "available" : {
            "type" : "boolean"
          },
          "characters" : {
            "type" : "string"
          },
          "copies" : {
            "type" : "long"
          },
          "description" : {
            "type" : "string",
            "store" : true
          },
          "otitle" : {
            "type" : "string"
          },
          "section" : {
            "type" : "long"
          },
          "tags" : {
            "type" : "string"
          },
          "title" : {
            "type" : "string"
          },
          "year" : {
            "type" : "long"
          }
        }
      }
    }
  }
}

可以修改的项:

  • 增加新的类型定义

  • 增加新的字段

  • 增加新的分析器

不允许修改的项:

  • 更改字段类型(比如文本改为数字)

  • 更改存储为不存储,反之亦然

  • 更改索引属性的值

  • 更改已索引文档的分析器

注意的是新增字段或更改分析器之后,需要再次对所有文档进行索引重建

字段的数据类型

简单类型

  • string(指定分词器)

  • date(默认使用UTC保持,也可以使用format指定格式)

  • 数值类型(byte,short,integer,long,float,double)

  • boolean

  • binary(存储在索引中的二进制数据的base64表示,比如图像,只存储不索引)

  • ip(以数字形式简化IPV4地址的使用,可以被索引、排序并使用IP值做范围查询).

有层级结构的类型

比如object 或者 nested.

特殊类型

比如geo_point, geo_shape, or completion.

参考

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值