【问题标题】:How can I use nested types with NEST client for Elastic Search如何将嵌套类型与 NEST 客户端一起用于 Elastic Search
【发布时间】:2013-07-23 23:22:00
【问题描述】:

我在 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] 未嵌套.."

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

提姆

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢 Martijn。今天早上我设法让它与原始解决方案一起工作。我没有看过来自 client.MapFromAttributes() 的响应。一旦我注意到反应,原因就很清楚了。嵌套映射没有生效,因为我将字符串属性设置为不通过属性进行分析,并且没有将 int 属性限定为 int 映射。一旦我解决了这个问题。有效。如果这是更好的映射方式,我会尝试您的解决方案。再次感谢。
    • NestedObject 现在似乎不是当前 NEST 版本中 API 的一部分。现在有没有其他方法可以做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多