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

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

相关推荐
人类群星闪耀时1 小时前
物联网与大数据:揭秘万物互联的新纪元
大数据·物联网·struts
chenbin5201 小时前
Jenkins 自动构建Job
运维·jenkins
risc1234561 小时前
【Elasticsearch】Search Templates(搜索模板)
elasticsearch
java 凯1 小时前
Jenkins插件管理切换国内源地址
运维·jenkins
陆鳐LuLu4 小时前
日志管理利器:基于 ELK 的日志收集、存储与可视化实战
运维·elk·jenkins
[奋斗不止]5 小时前
Jenkins 配置 Credentials 凭证
jenkins·jenkins 配置凭证
桃林春风一杯酒7 小时前
HADOOP_HOME and hadoop.home.dir are unset.
大数据·hadoop·分布式
桃木山人8 小时前
BigData File Viewer报错
大数据·java-ee·github·bigdata
B站计算机毕业设计超人8 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
数造科技8 小时前
紧随“可信数据空间”政策风潮,数造科技正式加入开放数据空间联盟
大数据·人工智能·科技·安全·敏捷开发