【问题标题】:Elasticsearch NEST - Filtering on multilevel nested typesElasticsearch NEST - 过滤多级嵌套类型
【发布时间】:2015-01-04 04:52:06
【问题描述】:

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

"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 concrete documentation / examples of building complex index using NEST ElasticSearch libraryMatching a complete complex nested collection item instead of separate members with 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.???))))
             )
            );

我希望第二个路径使用订单(订单是列表)作为泛型类型,但它是客户。

正确查询的代码是什么?

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

【问题讨论】:

    标签: elasticsearch nest


    【解决方案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>)))))
                   ));
    

    就文档而言,我遇到的最好的文档与问题中的you posted 以及那里链接的资源相同。

    【讨论】:

    • 顺便说一句:我使用.Path(c =&gt; c.Orders).Path(c =&gt; c.Orders.First().OrderLines) 的形式,因为它感觉与其他参数更一致。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多