简体   繁体   English

聚合,ElasticSearch(Nest,Elasticsearch.net)中的建议获取完整的对象

[英]Aggregation, suggestion in ElasticSearch (Nest, Elasticsearch.net) get complete object

I'm quite new to elasticsearch, I am using NEST to query to elastic following is my code snippet. 我是Elasticsearch的新手,我正在使用NEST查询弹性代码,这是我的代码段。

var searchResults =
            elasticClient.Client.Search<T>(
            s => s
                    .Size(20)
                    .Fields(core)
                    .QueryString(String.Format("*{0}*", query)).MinScore(1).QueryString(String.Format("*{0}*", query.Replace(" ", "")))
                    .Highlight(h => h
                    .PreTags("<b>")
                    .PostTags("</b>")
                    .OnFields(f => f
                        .PreTags("<em>")
                        .PostTags("</em>")
                    )
                )
            );

var suggestResults = elasticClient.Client.Suggest<T>(s => s
                                        .Term("suggest", m => m
                                            .SuggestMode(SuggestMode.Always)
                                            .Text(query)
                                            .Size(10)
                                            .OnField(core)
                                        ));

var aggregation = elasticClient.Client.Search<T>(s => s
            .Aggregations(a => a
                .Terms("term_items", gh=>gh
                    .Field(p=>p.Town)
                    .Aggregations(gha=>gha
                        .SignificantTerms("bucket_agg", m => m
                            .Field(p => p.Town)
                            .Size(2)
                            .Aggregations(ma => ma.Terms("Town", t => t.Field(p => p.Town)))
                        )
                    )
                )
            )
);

I do get list of documents (list of my specified domain object) , but in case of suggest and aggregation it doesn't return domain object ? 我确实获得了文档列表(我指定的域对象的列表),但是在建议和汇总的情况下,它不会返回域对象?

I apologize in advanced and I hope you can point me to the correct direction. 非常抱歉,希望您能指出正确的方向。

I am looking for a way to implement in NEST. 我正在寻找在NEST中实施的方法。

To get to your aggregations you need to use the Aggs property of the result. 要进行汇总,您需要使用结果的Aggs属性。 According to the documentation : 根据文档

The result of the aggregations are accessed from the Aggs property of the response using the key that was specified on the request... 聚合的结果是使用请求上指定的键从响应的Aggs属性访问的...

In your example this would be "term_items" . 在您的示例中,这将是"term_items" You are also doing a sub-aggregation, so these need to be extracted for each top-level aggregation, again using the key specified for the sub-aggregation - "bucket_agg" . 您还正在执行子聚合,因此需要使用为子聚合指定的键- "bucket_agg"为每个顶级聚合提取这些子聚合。 Your code should look something like 您的代码应类似于

var agg = results.Aggs.Terms("term_items");
if (agg!= null && agg.Items.Count > 0)
{
    foreach (var termItemAgg in agg.Items)
    {
        // do something with the aggregations
        // the term is accessed using the Key property
        var term = termItemAgg.Key;

        // the count is accessed through the DocCount property
        var count = termItemAgg.Count;

        // now access the result of the sub-aggregation
        var bucketAgg = termItemAgg.Aggs.SignificantTerms("bucket_agg");

        // do something with your sub-aggregation results
    }
}

There is more detail in the documentation . 文档中有更多详细信息

To get your suggestions you access the Suggestions property of your results object, using the key you specify when calling the ElasticClient.Suggest method. 为了让您建议您访问Suggestions你的结果对象的属性,使用您调用时指定键ElasticClient.Suggest方法。 Something like 就像是

var suggestions = result.Suggestions["suggest"]
                .FirstOrDefault()
                .Options
                .Select(suggest => suggest.Payload)
                .ToList();

Hope this helps . 希望这可以帮助 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM