Elasticsearch NEST-过滤多层嵌套类型 [英] Elasticsearch NEST - Filtering on multilevel nested types

查看:128
本文介绍了Elasticsearch NEST-过滤多层嵌套类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

"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.

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

解决方案

假设我们正在将客户建模为这些行上的

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>)))))
               ));

据我所知,最好的文档与问题中的发布的文档相同.以及在那里链接的资源.

I have a document model like this:

"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" }
                    }
                }
            }
        }
    }
}  

A customer can have 0, 1 or multiple orders and an order can have 0, 1 or multiple orderLines
(this is a model I created for this question, as I think this is data everyone can understand, so if you spot any mistakes, please let me know, but don't let them distract you from my actual question)

I want to create a query with NEST which selects a (or all) customers with a specific value for customer.id, but only if they have at least one orderLine with a specific articleId.

I've looked at Need concrete documentation / examples of building complex index using NEST ElasticSearch library and Matching a complete complex nested collection item instead of separate members with Elastic Search, but was unable to create the query. Based upon the second question, I got to the point where I wrote

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.???))))
             )
            );

I expected the second Path to use order (orders is List) as generic type, but it is customer.

What is the code for the correct query?

In addition: is there more elaborate documentation of the search/query/filtering methods of NEST than the documentation on http://nest.azurewebsites.net/? In the first referenced question, both the links to the complex query tutorial (in question) and the unit test examples (accepted answer) do not work (timeout and 404 respectively).

解决方案

Assuming we are modelling the customer to something on these lines

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; }

    };

The query in the above case would be :

 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>)))))
               ));

As far as documentation the best I have come across is the same as the one you posted in the question and the resources linked there.

这篇关于Elasticsearch NEST-过滤多层嵌套类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆