Elasticsearch NEST - Filtering on Multilevel Nested Types

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 more orders, and an order can have 0, 1 or more orderLines
(this is the model I created for this question, since I think it is data that everyone can understand, so if you notice any errors, let me know, but do not let them distract you from my actual question)

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

I looked. I need specific documentation / examples of building a complex index using the NEST ElasticSearch library and Full compliance of a nested collection element instead of individual elements using Elastic Search , but could not create a query. Based on the second question, I went so far as to write

 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 (List orders) as a generic type, but it is a client.

What is the code for the correct request?

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

+5
source share
1 answer

Assuming we model the client with something in 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; } }; 

Request in the above case:

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

In terms of documentation, the best I've come across is the same as you sent to the question and related resources.

+5
source

Source: https://habr.com/ru/post/1206410/


All Articles