Elasticsearch NEST

编程入门 行业动态 更新时间:2024-10-26 12:22:07
本文介绍了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库构建复杂索引的示例和使用弹性搜索匹配完整的复杂嵌套集合项,而不是单独的成员,但是无法创建查询.根据第二个问题,我到达了写作的地步

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

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

正确查询的代码是什么?

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

本文发布于:2023-10-16 10:40:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Elasticsearch   NEST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!