如何将嵌套类型与 NEST 客户端一起用于 Elastic Search

2023-12-06

我在尝试在 Elastic Search 中的文档上使用统计方面时遇到了一些问题。这导致 Elastic Search google 群组上出现以下帖子 - 请参阅https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY。我尝试应用关于在文档中使用嵌套类型的答案中的建议,以在集合属性上提供不同的总和(请参阅https://groups.google.com/forum/#!topic/elasticsearch/wNjrnAC_KOY)

也就是说,我将拥有许多带有 MyItem 集合的 MyType 实例。 MyItem 的某些集合将具有匹配金额的实例,即第一个文档可能有两个 myitem 实例,金额均为 100。如果没有嵌套类型,我不相信统计方面会聚合每个金额,因为它们不是唯一的。

因此,我创建了一个文档结构(类似于下面)并填充了我的索引。在填充索引之前,我使用以下代码来创建嵌套文档。

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

但是,当我通过 Nest api 运行以下查询时,我没有得到任何结果。

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

此外,我还通过 Chrome 插件 PostMan 尝试了以下操作:

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

并得到回复,其中注明:

“..facet 嵌套路径 [mycollection] 未嵌套..”

对此的任何想法都会很棒。

Tim


尝试按如下方式映射您的对象:

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()非常有限,可能会在 1.0 版本中被删除。注释属性名称非常有用,但很快它所能表达的内容就会受到限制。 MapFluent 调用中的 MapFromAttributes() 仍然是将 int 键入为 int、将 float 键入为 float、将 DateTime 键入为日期等的好方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将嵌套类型与 NEST 客户端一起用于 Elastic Search 的相关文章

随机推荐