Elasticsearch NEST - 过滤多级嵌套类型

 沸腾的热水_948 发布于 2022-12-12 17:03

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

"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库构建复杂索引的具体文档/示例,并使用Elastic Search匹配完整的复杂嵌套集合项而不是单独的成员,但无法创建查询.根据第二个问题,我达到了我写作的地步

var results = client.Search(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.???))))
             )
            );

我期望第二个路径使用order(orders is List)作为泛型类型,但它是customer.

正确查询的代码是什么?

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

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

    就文档而言,我遇到的最好的文档与您在问题中发布的文档以及与之关联的资源相同.

    2022-12-12 17:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有