I'm quite new to elasticsearch, I am using NEST to query to elastic following is my code snippet.
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.
To get to your aggregations you need to use the Aggs
property of the result. 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...
In your example this would be "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"
. 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. Something like
var suggestions = result.Suggestions["suggest"]
.FirstOrDefault()
.Options
.Select(suggest => suggest.Payload)
.ToList();
Hope this helps .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.