Elasticsearch对嵌套对象的过滤范围

时间:2016-10-28 18:20:27

标签: c# .net elasticsearch nest

我的对象PurchaseOrderList<VendorStatus> VendorStatusesVendorStatusEstimatedShipDate列。我需要按最新的EstimatedShipDate过滤采购订单。我怎么能这样做?

我试过了:

Filter<PurchaseOrder>.Range(r => r
    .OnField(x => x.VendorStatuses.OrderByDescending(v => v.StatusUpdateDate).First().EstimatedShipDate)
    .GreaterOrEquals(fromDate)
    .LowerOrEquals(toDate));

var f = Filter<PurchaseOrder>.Range(r => r
    .OnField("VendorStatuses.EstimatedShipDate")
    .GreaterOrEquals(fromDate)
    .LowerOrEquals(toDate));

var newFilter = Filter<PurchaseOrder>.Nested(nfd => nfd.Path(x => x.VendorStatuses.First().EstimatedShipDate))
    .Filter(f2 => f2.Range(x => x.OnField(f => f.VendorStatuses.First().EstimatedShipDate)
    .GreaterOrEquals(fromDate)
    .LowerOrEquals(toDate))));

但这些都没有奏效。我怎么能这样做?

直接对抗弹性跑时,我甚至无法使日期范围有效。以下查询返回无结果,即使使用head插件时我可以看到它们列在“po”下

{
    "query": {
        "range": {
            "po.vendorStatuses.estimatedShipDate": {
                "gte": "2016-10-01",
                "lte": "2016-11-01",
            }
        }
    }
}

我们在项目中使用Nest版本1.7.2,弹性为1.4.5。

以下是我们类型的更多信息:

//that builds our search query, contains many methods to build up the FilterContainer
public class PurchaseOrderSearchQueryBuilder 
{
    protected FilterContainer Filter { get; set; }

    public PurchaseOrderSearchQueryBuilder WithExpectedShipDate(DateTime fromDate, DateTime toDate)
    {
        var newFilter = Filter<PurchaseOrder>. 
        //method i need to implment

        Filter &= newFilter;

        return this;
    }
}

//example working filter method
public PurchaseOrderSearchQueryBuilder WithCustomerId(long customerId)
{
    if (customerId > 0)
        Filter &= Filter && Filter<PurchaseOrder>.Term(p => p.CustomerId, customerId);
    return this;
}

public class PurchaseOrder 
{
    ...
    public long CustomerId { get; set; }
    public List<PoVendorStatus> VendorStatuses { get; set; }
}

public class PoVendorStatus
{
    public long Id { get; set; } 
    ...
    public DateTime? EstimatedShipDate { get; set; }
    public DateTime StatusUpdateDate { get; set; }
}

1 个答案:

答案 0 :(得分:2)

使用nest,这将返回具有一个满足条件的内部对象的所有父对象。如果您想查询嵌套对象,则需要将其映射为nested,并且您需要使用nested query

//Fluent
client.Search<PurchaseOrder>(s=>s.Query(
                    q=>q.DateRange(
                        dr=>dr.Field(p=>p.VendorStatuses.First().StatusUpdateDate)
                              .GreaterThan(fromDate)
                              .LessThan(toDate))));
//Object
client.Search<PurchaseOrder>(new SearchRequest<PurchaseOrder>()
                {
                    Query =new DateRangeQuery
                    {
                        //If you used all default mappings this might be camelCase
                        Field = "VendorStatuses.StatusUpdateDate",
                        GreaterThan = fromDate,
                        LessThan = toDate
                    }
                });