可以使用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(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子句来实现

使用ElasticAPMNetCoreAll实现以下查询:ES里有类Sku结构如下:public class Sku public ListCarSpecMatch CarSpecMatchListget;set;public class CarSpecMatch public long Idget;set; public string Valuesget;set;现在传入参

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

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