zoukankan      html  css  js  c++  java
  • 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字段表示,父子关系的特性和之前一样起作用。

  • 相关阅读:
    jsonp
    web系统中上下移动功能的实现
    重载的目的是什么
    重写和重载
    final和static
    static的应用
    shiro认证
    做阉割版Salesforce难成伟大的TOB企业
    Go语言在国产CPU平台上应用前景的探索与思考
    101 More Security Best Practices for Kubernetes
  • 原文地址:https://www.cnblogs.com/wangzhen3798/p/10768247.html
Copyright © 2011-2022 走看看