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

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

一、创建索引和添加映射

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

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字段来标识文档类型。以下是一些包含假数据的文档示例:

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的文档:

GET /my_test_index/_search
{
  "query": {
    "term": { "doc_type": "type1" }
  }
}

查询类型为type2的文档:

GET /my_test_index/_search
{
  "query": {
    "term": { "doc_type": "type2" }
  }
}

参考官方文档:Query DSL - Elasticsearch

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

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

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的文档:

GET /type1_index/_search
{
  "query": {
    "match_all": {}
  }
}

查询类型为type2的文档:

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

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

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版本,灵活性更高,但需要在文档设计和查询时额外注意区分。

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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 19:24:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 19:24:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 19:24:02       82 阅读
  4. Python语言-面向对象

    2024-06-16 19:24:02       91 阅读

热门阅读

  1. make menuconfig | allyesconfig | allnoconfig【笔记】

    2024-06-16 19:24:02       30 阅读
  2. Verilog-学习verilog中遇到的问题总结

    2024-06-16 19:24:02       29 阅读
  3. 洛谷 AT_arc168_a [ARC168A] <Inversion> 题解

    2024-06-16 19:24:02       34 阅读
  4. 用户组的概念(linux篇)

    2024-06-16 19:24:02       27 阅读
  5. CentOS下 conda环境设置

    2024-06-16 19:24:02       27 阅读
  6. HTTP!!!

    HTTP!!!

    2024-06-16 19:24:02      33 阅读
  7. Android基础-ANR详解

    2024-06-16 19:24:02       32 阅读
  8. oracle的xmlagg的用法

    2024-06-16 19:24:02       30 阅读
  9. 异常处理与IO

    2024-06-16 19:24:02       25 阅读