Elasticsearch:处理 Elasticsearch 中的字段名称不一致

在 Elasticsearch 中,经常会遇到类似数据的不同索引具有不同字段名称的情况。 例如,一个索引可能使用字段名 level 来表示日志级别,而另一个索引可能使用 log_level 来达到相同目的。 出现这种不一致的原因有多种,例如不同的团队使用不同的命名约定、不断发展的数据模型或集成来自不同来源的数据。

跨两个索引搜索日志级别

要在 loglevel_test1 和 loglevel_test2 索引中搜索日志级别为 "info" 的所有文档,考虑到不同的字段名称,你可以按如下方式构建查询:

bash 复制代码
1.  PUT loglevel_test1/_doc/1
2.  {
3.    "level":"This is a very useful info in test1"
4.  }

6.  DELETE loglevel_test2
7.  PUT loglevel_test2/_doc/1
8.  {
9.    "log_level":"This is a very useful info in test2"
10.  }

13.  GET loglevel_test1,loglevel_test2/_search?filter_path=**.hits
14.  {
15.    "query": {
16.      "bool": {
17.        "should": [
18.          {
19.            "term": {
20.              "level": "info"
21.            }
22.          },
23.          {
24.            "term": {
25.              "log_level": "info"
26.            }
27.          }
28.        ],
29.        "minimum_should_match": 1
30.      }
31.    }
32.  }

此查询检查 loglevel_test1 中的 level 字段和 loglevel_test2 中的 log_level 字段的值 "info"。 在 bool 查询中使用 should 子句可以灵活地匹配任一条件。 minimum_should_match 参数确保文档匹配时至少有一个条件为真,从而从两个索引返回所有相关文档。

上述查询的结果为:

json 复制代码
1.  {
2.    "hits": {
3.      "hits": [
4.        {
5.          "_index": "loglevel_test2",
6.          "_id": "1",
7.          "_score": 0.2876821,
8.          "_source": {
9.            "log_level": "This is a very useful info in test2"
10.          }
11.        },
12.        {
13.          "_index": "loglevel_test1",
14.          "_id": "1",
15.          "_score": 0.12343237,
16.          "_source": {
17.            "level": "This is a very useful info in test1"
18.          }
19.        }
20.      ]
21.    }
22.  }

使用 alias 来完成

我们可以参考文章 "Elasticsearch : alias 数据类型"。在索引中,我们可以为 loglevel_test2 定义如下的 alias:

bash 复制代码
1.  PUT loglevel_test2/_mapping
2.  {
3.    "properties": {
4.      "level": {
5.        "type": "alias",
6.        "path": "log_level"
7.      }
8.    }
9.  }

这样 level 也就是 log_level 的别名。我们再次使用如下的命令来进行搜索:

bash 复制代码
1.  GET loglevel_test1,loglevel_test2/_search?filter_path=**.hits
2.  {
3.    "query": {
4.      "match": {
5.        "level": "info"
6.      }
7.    }
8.  }

上面搜索的结果是:

json 复制代码
1.  {
2.    "hits": {
3.      "hits": [
4.        {
5.          "_index": "loglevel_test1",
6.          "_id": "1",
7.          "_score": 0.2876821,
8.          "_source": {
9.            "level": "This is a very useful info in test1"
10.          }
11.        },
12.        {
13.          "_index": "loglevel_test2",
14.          "_id": "1",
15.          "_score": 0.2876821,
16.          "_source": {
17.            "log_level": "This is a very useful info in test2"
18.          }
19.        }
20.      ]
21.    }
22.  }

当然上面的搜索索引名称也有一大串。在使用的时候并不方便。我们可以再次使用如下的技巧来进行精简:

bash 复制代码
1.  GET loglevel_test*/_search?filter_path=**.hits
2.  {
3.    "query": {
4.      "match": {
5.        "level": "info"
6.      }
7.    }
8.  }
相关推荐
考虑考虑14 小时前
ElasticSearch索引命令
运维·后端·elasticsearch
Elastic 中国社区官方博客18 小时前
在 Elasticsearch 中回填时间序列数据:通过批量 API 加载数月的历史指标数据
大数据·运维·数据库·人工智能·elasticsearch·搜索引擎·全文检索
2401_8616786221 小时前
Git 在 Windows 使用
windows·git·elasticsearch
ShineWinsu1 天前
对于 Vue 3:开发前需要掌握的 JavaScript 核心基础:从变量、数组方法到 Promise、Async/Await 与 ES Module 的解析
javascript·vue.js·elasticsearch
Elasticsearch1 天前
如何通过一条 ES|QL 查询为 Elasticsearch 中的每个指标构建指标图表
elasticsearch
Han.miracle2 天前
Elasticsearch深度分页问题完整解析
大数据·elasticsearch·搜索引擎
深海鱼肝油ya2 天前
向量数据库Elasticsearch(一)
人工智能·elasticsearch·向量数据库·es数据库增加索引·es数据库相似度查询·knn查询
大模型丫丫2 天前
工业级 RAG 为什么需要 Elasticsearch?
elasticsearch·django·jenkins
Sayai2 天前
Elasticsearch 9 安装验证实战:start-local 一键部署 + Docker 手动安装踩坑指南
大数据·elasticsearch·docker
瑞码空间3 天前
git知识点黄金笔记
笔记·git·elasticsearch