如何使用NEST客户端的嵌套类型进行弹性搜索 [英] How can I use nested types with NEST client for Elastic Search

查看:131
本文介绍了如何使用NEST客户端的嵌套类型进行弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



那是我将有许多MyType的实例与MyItem的集合。 MyItem的一些集合将具有匹配数量的实例,即第一个文档可能有两个myitem实例,其数量为100.没有嵌套类型我不认为统计方面会聚合每个数量,因为它们不是唯一的。 / p>

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

  client.MapFromAttributes< Page>(); 


[ElasticType(Name =page,DateDetection = true,NumericDetection = true,SearchAnalyzer =standard,IndexAnalyzer =standard)]
public class MyType
{
public int TypeId {get;组; }
public string Name {get;组; }
public ANotherType AnotherProperty {get;组; }
public DateTime Created {get;组; }

[ElasticProperty(Type = FieldType.nested,Name =mycollection)]
public List< MyItem> MyItems {get;
}

public class MyItem
{
public decimal金额{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:{
statistics:{
field:amount
},
嵌套:mycollection
}
},
size:0
}'

并获得注释的回复:



.. facet嵌套路径[mycollection]不是嵌套的..



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



Tim

解决方案

尝试将对象映射为: / p>

  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作为浮点数,DateTime作为日期等等。


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

解决方案

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.

这篇关于如何使用NEST客户端的嵌套类型进行弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆