小编典典

Elasticsearch NEST-过滤多层嵌套类型

elasticsearch

我有一个这样的文档模型:

"customer": {  
    "properties": {  
        "id": { "type": "integer" },
        "name": { "type": "string" },
        "orders": {
            "type": "nested",
            "properties": {
                "id": { "type": "integer" },
                "orderDate" : { "type": "date", "format" : "YYYY-MM-dd" },
                "orderLines": {
                    "type": "nested",
                    "properties": {
                        "seqno": { "type": "integer" },
                        "quantity": { "type": "integer" },
                        "articleId": { "type": "integer" }
                    }
                }
            }
        }
    }
}

一个客户可以有0个,1个或多个订单,一个订单可以有0个,1个或多个orderLines
(这是我为这个问题创建的模型,因为我认为这是每个人都可以理解的数据,因此,如果发现任何错误,请让我知道,但不要让他们分散我的实际问题)

我想使用NEST创建一个查询,该查询选择一个(或所有)具有customer.id特定值的客户,但前提是他们至少具有一个具有特定articleId的orderLine。

我看过Need的具体文档/使用NESTElasticSearch库构建复杂索引的示例,以及使用Elastic
Search将完整的复杂嵌套集合项而不是单独的成员进行匹配的示例,但是无法创建查询。根据第二个问题,我写到了

var results = client.Search<customer>(s => s
    .From(0)
    .Size(10)
    .Types(typeof(customer))
    .Query(q =>
        q.Term(c => c.id, 12345)
        && q.Nested(n => n
            .Path(c => c.order)
            .Query(q2 => q2.Nested(n2 => n2
                .Path(o => o.???))))
             )
            );

我希望第二条路径将订单(订单是列表)用作通用类型,但它是客户。

正确查询的代码是什么?

另外:与http://nest.azurewebsites.net/上的文档相比,NEST的搜索/查询/过滤方法是否有更详尽的文档?在第一个引用的问题中,指向复杂查询教程(正在讨论)的链接和单元测试示例(已接受的答案)都不起作用(分别是超时和404)。


阅读 551

收藏
2020-06-22

共1个答案

小编典典

假设我们正在按照这些路线对 客户 进行建模

class customer
    {
        public int id { get; set; }
        public string name { get; set;}

        public class Orders {
            public int id { get; set;}
            public string orderData { get; set;}
            public class OrderLines
            {
                public int seqno { get; set; }
                public int quantity { get; set; }
                public int articleId { get; set; }
            }
            [ElasticProperty(Type = FieldType.Nested)]
            public List<OrderLines> orderLines { get; set; }
        }
         [ElasticProperty(Type = FieldType.Nested)]
        public List<Orders> orders { get; set; }

    };

在上述情况下的查询为:

 var response = client.Search<customer>(
                s => s.Index(<index_name_here>).Type("customer")
                .Query(q => q.Term(p=>p.id, 1) 
                &&
                q.Nested(n =>
                    n.Path("orders")
                    .Query(q2=> q2.Nested(
                        n2 => n2.Path("orders.orderLines")
                        .Query(q3 => 
                            q3.Term(c=>c.orders.First().orderLines.First().articleId, <article_id_here>)))))
               ));

就文档而言,我遇到的最好的事情与在问题中发布的内容以及链接到该问题的资源相同。

2020-06-22