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

如何在Elasticsearch中对对象数组应用组合筛选查询符合条件的商品

Elasticsearch 查询解决方案

前置注意事项

  • 首先需要确认你的索引中attributes字段的映射类型为nested,否则数组内对象的属性关联关系会被ES扁平化处理,导致跨对象匹配的错误结果。

完整查询DSL

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "category.keyword": "blouses"
                }
              },
              {
                "nested": {
                  "path": "attributes",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "attributes.attribute.keyword": "women-blouse-neckline"
                          }
                        },
                        {
                          "term": {
                            "attributes.tag.keyword": "round-neck"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "category.keyword": "dresses"
                }
              },
              {
                "nested": {
                  "path": "attributes",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "attributes.attribute.keyword": "women-dress-neckline"
                          }
                        },
                        {
                          "term": {
                            "attributes.tag.keyword": "v-neck"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

逻辑说明

  • 外层bool.should用来匹配两类符合要求的商品,minimum_should_match: 1保证至少命中其中一类条件
  • 每个类目对应的条件使用bool.must同时匹配类目值和对应的属性筛选规则
  • 嵌套查询nested指定查询路径为attributes,保证同一个attribute对象内的两个字段同时满足筛选要求,避免匹配到不同对象下的属性组合
  • 所有term查询使用.keyword后缀是默认对应字段为text+keyword双类型,如果你直接设置对应字段为keyword类型,可以去掉后缀。

内容的提问来源于stack exchange,提问作者Naresh Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 19:06:03