Elasticsearch6.x 后如何替代之前的type

方法1:一个索引一个文档类型

第一种选择是每个文档类型一个索引,作为存储tweet和user在同一个tweeter索引的替代,你可以存储tweet在tweets索引里,user在users索引里。索引之间是完全相互隔离的,所以索引里的字段类型不会冲突。

这种方法有两个好处:

  • 数据更紧凑,这样会更好的利用Lucene的压缩技术
  • 用来做全文检索评分的字段统计值会更准确,因为在同一个索引里的文档都代表这同一个实体。

从多type的索引迁移到单type索引的方法:

POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "user"
  },
  "dest": {
    "index": "users"
  }
}

POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "tweet"
  },
  "dest": {
    "index": "tweets"
  }
}

根据将来要存储的文档数量,每个索引大小都可以被合适的度量:你可以把users索引设置更少的主分片,tweets设置更多的主分片。

方法2:自定义type字段

当然,一个集群中主分片的数量是有限制的,所以你可能不想为了几千个文档浪费一个分片。这种情况,你可以实现自己的自定义的type字段,它和原来的_type元字段工作原理类似。

让我们拿上面的tweet/user来举例,原始的,工作流程像这样:

PUT twitter
{
  "mappings": {
    "user": {
      "properties": {
        "name": { "type": "text" },
        "user_name": { "type": "keyword" },
        "email": { "type": "keyword" }
      }
    },
    "tweet": {
      "properties": {
        "content": { "type": "text" },
        "user_name": { "type": "keyword" },
        "tweeted_at": { "type": "date" }
      }
    }
  }
}

PUT twitter/user/kimchy
{
  "name": "Shay Banon",
  "user_name": "kimchy",
  "email": "shay@kimchy.com"
}

PUT twitter/tweet/1
{
  "user_name": "kimchy",
  "tweeted_at": "2017-10-24T09:00:00Z",
  "content": "Types are going away"
}

GET twitter/tweet/_search
{
  "query": {
    "match": {
      "user_name": "kimchy"
    }
  }
}

现在,你可以达到同样的效果,通过添加自定义的type字段:

PUT twitter
{
  "mappings": {
    "_doc": {
      "properties": {
        "type": { "type": "keyword" }, 
        "name": { "type": "text" },
        "user_name": { "type": "keyword" },
        "email": { "type": "keyword" },
        "content": { "type": "text" },
        "tweeted_at": { "type": "date" }
      }
    }
  }
}

PUT twitter/_doc/user-kimchy
{
  "type": "user", 
  "name": "Shay Banon",
  "user_name": "kimchy",
  "email": "shay@kimchy.com"
}

PUT twitter/_doc/tweet-1
{
  "type": "tweet", 
  "user_name": "kimchy",
  "tweeted_at": "2017-10-24T09:00:00Z",
  "content": "Types are going away"
}

GET twitter/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "user_name": "kimchy"
        }
      },
      "filter": {
        "match": {
          "type": "tweet" 
        }
      }
    }
  }
}
无type映射的父子关系

之前,一个父子关系是通过设置一个文档类型为父,一个或多个映射类型为子,没有类型(type)的情况下,我们不能使用这种语法。除了父子关系通过新的join字段表示,父子关系的特性和之前一样起作用。

转载于:https://www.cnblogs.com/wangzhen3798/p/10768247.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值