You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch中数组字段tags.name的子字符串匹配查询问题

Elasticsearch 按子串匹配聚合tags.name字段问题

我在Elasticsearch中存储了如下结构的文档,希望查找包含指定子串的tags.name字段值。尝试通过聚合与过滤实现,但返回了全部tags.name值,未得到符合匹配要求的结果。

文档结构

"hits" : [
      {
          "tags" : [
            {
              "datatype" : "STRING",
              "values" : [
                {
                  "value_concat" : "DOBTEST22",
                  "unit" : "",
                  "value" : "DOBTEST22",
                  "operator" : "",
                  "ordernr" : 0
                }
              ],
              "name" : "hierarchy"
            },
              {
              "datatype" : "STRING",
              "values" : [
                {
                  "value_concat" : "DOBTEST223",
                  "unit" : "",
                  "value" : "DOBTEST223",
                  "operator" : "",
                  "ordernr" : 0
                }
              ],
              "name" : "test24"
            }
          ]
      }
]

尝试的无效查询语句

{
  "size": 0,
  "aggregations": {
    "agg": {
      "nested": {
        "path": "tags"
      },
      "aggregations": {
        "aggregation": {
          "terms": {
            "field": "tags.name",
            "size": 5000,
            "min_doc_count": 0,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        },
        "filter": {
          "filter": {
            "nested": {
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "tags.name": "*hier*"
                      }
                    }
                  ],
                  "adjust_pure_negative": true,
                  "boost": 1
                }
              },
              "path": "tags",
              "ignore_unmapped": false,
              "score_mode": "sum",
              "boost": 1
            }
          }
        }
      }
    }
  }
}

问题原因与解决方案

问题分析

  1. 聚合层级错误:原查询中terms聚合和filter聚合是同级关系,filter并未对terms聚合产生过滤作用,terms依然会聚合所有tags.name值。
  2. 查询类型误用:使用match查询搭配通配符*是错误的,match会对输入分词后匹配,通配符匹配应使用wildcard查询。

正确查询语句

需要将terms嵌套在filter聚合内部,确保只聚合符合过滤条件的tags.name:

{
  "size": 0,
  "aggregations": {
    "nested_tags": {
      "nested": {
        "path": "tags"
      },
      "aggregations": {
        "filter_name_substring": {
          "filter": {
            "wildcard": {
              "tags.name": "*hier*"
            }
          },
          "aggregations": {
            "matched_names": {
              "terms": {
                "field": "tags.name",
                "size": 5000,
                "order": [
                  { "_count": "desc" },
                  { "_key": "asc" }
                ]
              }
            }
          }
        }
      }
    }
  }
}

说明

  • nested_tags:指定针对tags嵌套字段进行聚合。
  • filter_name_substring:通过wildcard查询过滤出包含hier子串的tags.name。
  • matched_names:在过滤后的结果上对tags.name进行terms聚合,得到符合条件的字段值及其计数。

内容的提问来源于stack exchange,提问作者Ionut Logofatu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 13:31:13