我对弹性搜索很新,我使用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中实施的方法。
答案 0 :(得分:0)
要获得聚合,您需要使用结果的Aggs
属性。根据{{3}}:
使用请求中指定的密钥从响应的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
}
}
documentation中有更详细的内容。
要获取建议,请使用调用Suggestions
方法时指定的密钥来访问结果对象的ElasticClient.Suggest
属性。像
var suggestions = result.Suggestions["suggest"]
.FirstOrDefault()
.Options
.Select(suggest => suggest.Payload)
.ToList();
希望这有帮助。