How can I use nested types with NEST client for Elastic Search

大兔子大兔子 提交于 2019-12-18 17:30:42

问题


I ran in to some issues whilst trying to use statistical facets on my documents in Elastic Search. This resulted in the following posts on the Elastic Search google group - see https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY. I tried to apply the recommendation in the answer about using Nested types with in the document to provide distinct sums on the collections property(see https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY)

That is I would have many instances of MyType with a collection of MyItem. Some collections of MyItem will have instances with matching amounts i.e. the first document could have two instances of myitem, both with an amount of 100. Without nested types I don't believe statistical facets will aggregate each amount as they're not unique.

So I've created a document structure (similar to below) and populated my index. Before populating my index I've used the following code in an effort to created a nested document.

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 query via the nest api I don't get any results.

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

More over I've also tried the following via the Chrome plugin PostMan :

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

and get a response that notes :

"..facet nested path [mycollection] is not nested.."

Any thoughts on this would be great.

Tim


回答1:


Try to map you object as followed:

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

The client.MapFromAttributes() is very limited and will probably be removed in the 1.0 release. Its great to annotate property names but quickly becomes limitted in what it can express. The MapFromAttributes() in the mapfluent call is still a great way to type int's as int, float's as floats, DateTime's as dates etcetera.



来源:https://stackoverflow.com/questions/17834767/how-can-i-use-nested-types-with-nest-client-for-elastic-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!