可以使用 Elasticsearch 的 Nested Query 和 Bool Query 来实现这个查询。

首先需要在 ES 中创建一个 nested 类型的 mapping,以便支持 Nested Query。可以使用以下代码创建 mapping:

PUT /my_index
{
  "mappings": {
    "properties": {
      "CarSpecMatchList": {
        "type": "nested",
        "properties": {
          "Id": {"type": "long"},
          "Values": {"type": "keyword"}
        }
      }
    }
  }
}

然后可以使用以下代码来查询:

var client = new ElasticClient(settings);
var searchResponse = client.Search<Sku>(s => s
    .Query(q => q
        .Nested(n => n
            .Path(p => p.CarSpecMatchList)
            .Query(nq => nq
                .Bool(b => b
                    .Must(m => m
                        .Terms(t => t
                            .Field(f => f.CarSpecMatchList.First().Id)
                            .Terms(carSpecList.Select(cs => cs.Id))
                        ),
                        m => m
                        .Nested(nn => nn
                            .Path(p => p.CarSpecMatchList)
                            .Query(nq => nq
                                .Terms(t => t
                                    .Field(f => f.CarSpecMatchList.First().Values)
                                    .Terms(carSpecList.Select(cs => cs.Value))
                                )
                            )
                        )
                    )
                )
            )
        )
    )
);

这个查询首先使用 Nested Query 来匹配 Sku 的 CarSpecMatchList,然后使用 Bool Query 来同时匹配 CarSpecMatchList 的 Id 和 Values。其中,Terms Query 用来匹配多个 Id 或 Value。注意,这个查询只匹配 Sku 的 CarSpecMatchList 中至少有一个元素与参数列表匹配,如果需要匹配所有元素,则需要使用 Nested Query 和 Bool Query 的 Must 子句来实现。

使用 Elastic.APM.NetCoreAll 实现基于 CarSpec 列表的 SKU 查询

原文地址: https://www.cveoy.top/t/topic/f57k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录