Hi Paul,
Please try to apply the "Take" before you call the "GetContentResult" method
var totalHits = SearchClient.Instance.Search<ContentPage>()
.For(term)
.Take(5)
.GetContentResult()
.Select(x=>new SearchAutoCompleteItems
{
PageTitle = x.PageTitle,
Url = ContentService.Instance.GetExternalurl(x.PageLink)
});
Unfortunately moving the take up didn't resolve the speed issues, any other thoughts?
Try using GetResult() instead of GetContentResult().
https://world.episerver.com/blogs/scott-reed/dates/2019/3/episerver-find---performance-reloading-custom-data-and-getcontent-vs-getcontentresult/
Exactly what Tomas said. Projections would be the first thing I would try. Also, you could try caching your queries for a short duration to see if this improves overall site performance.
This is not "autocomplete" - if you want to use the autocomplete feature by Find, take a look at IStatisticsClient.GetAutocomplete
Replies above are good practices:
It's unclear from your question what 2 seconds is for. I'd suggest to profile to see where it's slowest - the request to Find server, or GetContentResult, or something else. If you don't know what is slow, you can't effectively optimize it.
Thanks everyone, I've finally had time to re-visit this.
Here is some simple code using the true autocomplete:
[HttpGet]
[AllowAnonymous]
public JsonResult AutoCompletev3(string term)
{
var result = SearchClient.Instance.Statistics()
.Autocomplete(term,5).Hits;
return Json(result, JsonRequestBehavior.AllowGet);
}
This for example returns:
[{"Query":"pet breeds","Type":"statistical"},{"Query":"pet","Type":"statistical"},{"Query":"pets","Type":"statistical"},{"Query":"pet ","Type":"statistical"},{"Query":"pet breed","Type":"statistical"}]
Our previous autocomplete would do a normal search and then onclick take the user directly to the page content as we'd have the page link, I'm guessing that using this autocomplete we then send the selected autocomplete term to the search page to do a full search?
I'm also guessing these results from autocomplete get generated based on the .Track() attribute? Is it best to run the tracking as part of the indexing to build the autocomplete results? for example when I index content to send an addtional track with the page name ?
Wondering if anyone has a really nice working autocomplete function that includes "did you mean" and spellchecking, I can see they are part of the IStatisticsClient class but I can find any great examples.
Any help/advise is muchly appreciated.
Regards,
Paul
Hi All,
We have a very basic service setup to search and return the top 5 items from our Find index:
[HttpGet]
[AllowAnonymous]
public JsonResult AutoComplete(string term)
{
var totalHits = SearchClient.Instance.Search<ContentPage>()
.For(term)
.GetContentResult()
.Take(5).Select(x=>new SearchAutoCompleteItems
{
PageTitle = x.PageTitle,
Url = ContentService.Instance.GetExternalurl(x.PageLink)
});
return Json(totalHits, JsonRequestBehavior.AllowGet);
}
The problem is that this is taking about 2 seconds to respond, it is a large index, any suggestions to speed it up?
Thanks in advance!
Paul