使用Elasticsearch在同一索引中区分不同类型的文档

在使用Elasticsearch时,有时我们需要在同一个索引中存放不同类型的文档,并且这些文档的字段可能不一致。在早期版本中,我们可以使用types来实现,但在Elasticsearch 7.x及更高版本中,types概念已被弃用。本文将介绍如何在新的版本中使用标识字段和索引别名来实现这一需求。

一、创建索引和添加映射

首先,我们需要创建一个索引,并定义映射以包含不同类型文档的字段。

json 复制代码
PUT /my_test_index
{
  "mappings": {
    "properties": {
      "doc_type": { "type": "keyword" },
      "field1": { "type": "text" },
      "field2": { "type": "integer" },
      "fieldA": { "type": "text" },
      "fieldB": { "type": "date" }
    }
  }
}

参考官方文档:Mapping - Elasticsearch

二、添加文档

接下来,我们添加一些文档,并在每个文档中使用doc_type字段来标识文档类型。以下是一些包含假数据的文档示例:

json 复制代码
POST /my_test_index/_doc/1
{
  "doc_type": "type1",
  "field1": "值1",
  "field2": 10
}

POST /my_test_index/_doc/2
{
  "doc_type": "type2",
  "fieldA": "值A",
  "fieldB": "2023-06-16"
}

POST /my_test_index/_doc/3
{
  "doc_type": "type1",
  "field1": "值2",
  "field2": 20
}

POST /my_test_index/_doc/4
{
  "doc_type": "type2",
  "fieldA": "值B",
  "fieldB": "2023-06-17"
}

参考官方文档:Create Index - Elasticsearch

三、使用标识字段进行查询

在创建别名之前,我们可以直接使用doc_type字段进行查询:

查询类型为type1的文档:

json 复制代码
GET /my_test_index/_search
{
  "query": {
    "term": { "doc_type": "type1" }
  }
}

查询类型为type2的文档:

json 复制代码
GET /my_test_index/_search
{
  "query": {
    "term": { "doc_type": "type2" }
  }
}

参考官方文档:Query DSL - Elasticsearch

四、创建索引别名并添加过滤器

为了方便查询,我们可以为索引创建别名,并为别名添加过滤器,这样可以在逻辑上将一个索引分成多个"虚拟索引"。

json 复制代码
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "my_test_index",
        "alias": "type1_index",
        "filter": { "term": { "doc_type": "type1" } }
      }
    },
    {
      "add": {
        "index": "my_test_index",
        "alias": "type2_index",
        "filter": { "term": { "doc_type": "type2" } }
      }
    }
  ]
}

参考官方文档:Index Aliases - Elasticsearch

五、查询文档

我们可以使用别名来查询不同类型的文档,这样可以有效地区分和管理不同类型的数据。

查询类型为type1的文档:

json 复制代码
GET /type1_index/_search
{
  "query": {
    "match_all": {}
  }
}

查询类型为type2的文档:

json 复制代码
GET /type2_index/_search
{
  "query": {
    "match_all": {}
  }
}
六、对比新的做法和老的做法

在Elasticsearch 6.x及之前版本中,我们可以使用types来区分不同类型的文档:

json 复制代码
PUT /my_index
{
  "mappings": {
    "type1": {
      "properties": {
        "field1": { "type": "text" },
        "field2": { "type": "integer" }
      }
    },
    "type2": {
      "properties": {
        "fieldA": { "type": "text" },
        "fieldB": { "type": "date" }
      }
    }
  }
}

然而,在Elasticsearch 7.x中,types概念被移除了,推荐使用标识字段和索引别名的方式来管理和区分不同类型的文档。

老的做法: 使用多个types在同一个索引中区分文档。优点是直观且易于管理,但已被弃用。

新的做法: 使用标识字段和索引别名来管理不同类型的文档。优点是符合最新的Elasticsearch版本,灵活性更高,但需要在文档设计和查询时额外注意区分。

如果对你有帮助的话点个赞呗~

相关推荐
IT新视界7 小时前
大数据开发工具-Transwarp Data Studio
大数据
donoot7 小时前
Linux系统下图书馆级电子书全自动标准化分类整理完整实施方案
大数据·linux·运维·电子书管理
Elastic 中国社区官方博客8 小时前
如何比较两个 Elasticsearch 索引并找出缺失的文档
大数据·运维·数据库·elasticsearch·搜索引擎
xixixi777779 小时前
三大 AI 安全里程碑:Akamai 高危风险预警、智能体水印强制落地、PQC 量子安全全产业链统一
大数据·人工智能·安全·ai·大模型·智能体·政策
imbackneverdie10 小时前
AI4S不止于分子药物:以MedPeer为代表的科研基建打开产业新增量
大数据·人工智能·算法·aigc·科研·学术·ai 4s
ApacheSeaTunnel12 小时前
为什么 Data Ingestion 还没有被 Fivetran/Airbyte 完全解决?
大数据·开源·数据集成·seatunnel·技术分享·数据同步·airbyte·fivetran
启雀AI12 小时前
科技企业如何沉淀研发知识,减少重复开发和经验流失?
大数据·科技·软件需求·知识库·知识管理
大大大大晴天12 小时前
Hudi技术内幕:Schema Evolution原理与实践
大数据
kiros_wang13 小时前
ExploreYC 新手快速入门与实战指南
大数据·人工智能·物联网
阿里云大数据AI技术13 小时前
Elasticsearch 智能助手:Agent 让运维从经验驱动迈向智能协同
人工智能·elasticsearch·agent