elasticsearch:单个索引中的多种类型

时间:2018-06-27 07:06:35

标签: elasticsearch

我正在尝试在单个索引中创建多个类型。例如,我试图在host索引中创建两种类型(postytb),以便在它们之间创建父子关系。

PUT /ytb
{
  "mappings": {
      "post": {
          "_parent": {
              "type": "host" 
            },
          "properties":{
            "@timestamp": {
                  "type": "date"
              },
            "indexed": {
                  "type": "date"
              },
              "n_comments": {
                  "type": "long"
              }, 
              "n_harvested": {
                  "type": "long"
              }, 
              "n_likes": {
                  "type": "long"
              },
              "network": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              },
              "parent_id": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "post_dbid": {
                  "type": "long"
              }, 
              "post_id": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "post_netid": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "published": {
                  "type": "date"
              }
          }
      },
      "host": {
          "properties": {
              "@timestamp": {
                  "type": "date"
              }, 
              "@version": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "country": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "host_dbid": {
                  "type": "long"
              }, 
              "host_id": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "host_netid": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "id": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "indexed": {
                  "type": "date"
              }, 
              "language": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              },
              "name": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }, 
              "vertical": {
                  "fields": {
                      "keyword": {
                          "ignore_above": 256, 
                          "type": "keyword"
                      }
                  }, 
                  "type": "text"
              }
          }
      }
  }
}

但我遇到此错误:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [ytb] as the final mapping would have more than 1 type: [post, host]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Rejecting mapping update to [ytb] as the final mapping would have more than 1 type: [post, host]"
  },
  "status": 400
}

更新:Elasticsearch版本:6.3.0

2 个答案:

答案 0 :(得分:7)

如果您的ES 5.6或更高版本,则需要阅读。总而言之,从ES 6开始,将删除映射类型,并且每个索引将仅包含一种类型。

要在评论中回答您的问题,我知道另一个类似Kibana的工具(实际上是Kibana分支),该工具知道如何处理JOIN和关系数据。它被Siren Solutions称为Kibi。另请阅读blog announcement

答案 1 :(得分:-1)

此博客非常详细,原因、缺点和解决方法:https://www.elastic.co/guide/en/elasticsearch/reference/6.1/removal-of-types.html

总结:

  • 原因:同一索引中的不同类型不是独立的。这些类型中的公共字段应该具有相同的数据类型,因为它们在内部由相同的 Lucene 字段支持。
  • 缺点:两种公共字段很少的类型会导致数据存储稀疏。
  • 解决方法:定义一个包含两种类型中所有字段的自定义类型,使用自定义 type(或类似内容),以及使用自定义 type 字段的 CRUD。