How to use nested types with NEST client for Elastic Search

I ran into some problems trying to use statistical aspects in my documents in Elastic Search. This led to the following posts in the google elastic Search group - see https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY . I tried to apply this recommendation in the answer about using types of nested types in a document to provide different amounts in the collections property (see https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY )

That is, I would have many instances of MyType with the MyItem collection. Some collections of MyItem will have instances with matching amounts, i.e. The first document can have two instances of myitem, both of which are equal to 100. Without nested types, I do not think that the statistical aspects will aggregate each amount, since they are not unique.

So, I created a document structure (as shown below) and populated my index. Before filling out my index, I used the following code to create a subdocument.

client.MapFromAttributes<Page>(); [ElasticType(Name="page", DateDetection = true, NumericDetection = true, SearchAnalyzer = "standard",IndexAnalyzer = "standard")] public class MyType { public int TypeId { get; set; } public string Name { get; set; } public ANotherType AnotherProperty { get; set; } public DateTime Created { get; set; } [ElasticProperty(Type = FieldType.nested, Name="mycollection")] public List<MyItem> MyItems { get; } public class MyItem { public decimal Amount {get;set;} } 

However, when I run the following request via nest api, I do not get any results.

 query.Index("pages") .Type("page") .From(0) .Size(100) .FacetStatistical("TotalAmount", x => x.Nested("donations") .OnField("amount"))); 

Moreover, I also tried the following through the Chrome PostMan plugin:

 { "facets": { "test": { "statistical": { "field": "amount" }, "nested": "mycollection" } }, "size":0 }' 

and get an answer that notes:

".. the faceted nested path [mycollection] is not nested."

Any thoughts on this would be wonderful.

Tim

+3
source share
1 answer

Try matching your object as follows:

 client.MapFluent<MyType>(m=>m .MapFromAttributes() .NestedObject<MyItem>(no=>no .Name(p=>p.MyItems.First()) .Dynamic() .Enabled() .IncludeInAll() .IncludeInParent() .IncludeInRoot() .MapFromAttributes() .Path("full") .Properties(pprops => pprops .String(ps => ps .Name(p => p.FirstName) .Index(FieldIndexOption.not_analyzed) ) //etcetera ) ) ); 

client.MapFromAttributes() very limited and will probably be removed in version 1.0. Its great to comment on property names, but it quickly becomes limited by what it can express. MapFromAttributes () in a mapfluent call is still great for inputting int as int, float as float, DateTime as dates, etc.

+3
source

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


All Articles