首页 文章

如何将嵌套类型与NEST客户端一起用于弹性搜索

提问于
浏览
3

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

那就是我有许多MyType实例和MyItem集合 . 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]没有嵌套..”

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

蒂姆

1 回答

  • 3

    尝试将对象映射如下:

    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 's as int, float' s作为浮点数,DateTime作为日期等等 .

相关问题