簡體   English   中英

聚合,ElasticSearch(Nest,Elasticsearch.net)中的建議獲取完整的對象

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

我是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)))
                        )
                    )
                )
            )
);

我確實獲得了文檔列表(我指定的域對象的列表),但是在建議和匯總的情況下,它不會返回域對象?

非常抱歉,希望您能指出正確的方向。

我正在尋找在NEST中實施的方法。

要進行匯總,您需要使用結果的Aggs屬性。 根據文檔

聚合的結果是使用請求上指定的鍵從響應的Aggs屬性訪問的...

在您的示例中,這將是"term_items" 您還正在執行子聚合,因此需要使用為子聚合指定的鍵- "bucket_agg"為每個頂級聚合提取這些子聚合。 您的代碼應類似於

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

文檔中有更多詳細信息

為了讓您建議您訪問Suggestions你的結果對象的屬性,使用您調用時指定鍵ElasticClient.Suggest方法。 就像是

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

希望這可以幫助 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM