Our monthly Virtual Happy Hour is happening today (Fri Feb 23)!
Hi, I am trying to list all pages of type that have category value set on them.
Code snippet looks like this
var search = _findClient.Search<NewsArticlePage>()
.Filter(x => x.ChangedPublishedDate.BeforeNow())
.OrderByDescending(x => x.ChangedPublishedDate);
if (currentBlock.PublishCategories?.Any() ?? false)
search = search.MatchCategories(currentBlock.PublishCategories);
var result = search.GetContentResult().ToList();
This works fine till the moment new news article gets published. It gets indexed but it doesn't show in the result unless indexing jobs starts manually.
I know that GetContentResult caches and am wondering if there is either a way of invalidating cache during the published event or even better way of getting list of speciffic page type that still uses the latest cache after newly published event.
GetContentResult() reloads data from the database. I'd suggest swapping it to GetContent() and using projections instead. Read my blog on it https://world.episerver.com/blogs/scott-reed/dates/2019/3/episerver-find---performance-reloading-custom-data-and-getcontent-vs-getcontentresult/
This way you won't need to worry about not Find cache
Thanks Scott on your answer. I have tried to get projections and use GetResult but it still has lag - not showing in results. If I run reindexing job then it shows up. This is Alloy code so no fancy custom caching. Do you know what cache key is used on publish? Maybe I could get away with invalidating it in OnPublished event handler?
that's strange I wouldn't usually expect there to be a lag. WHat find index are you using, there's often problems with the developer ones and there#s also a serious Find outage at the moment that's effecting a lot of sites.
The cache key is the name of the item pushed in, so if you want to do it yourself you should usually only need to push the content item to the indexing service. It's strange however there shouldn't usually be much lag between a change and push unless the service is having issues.
When you say it gets indexed, are you seeing your changes in the Index browser in the admin?
Yes, I can see all changes in index browser. For example: I had 1 article that had Category A set as a value. That artcle would be present in an list obtained by my code snippet. Then I would create a new article with the same category. Published it and went to index browser. It was present in the list among article types indexed with all the proper values. I would return to the page with the search feature in question and it would still return only 1 article in the resulting list. If I would to run job manually (note that article was already in index) it would show in the list after next visit to the page where search feature was used. I asume that it has to do something with invalidation of the cache used when either FilterForVisitor() or GetContentResult() (or GetResult() - with projection) gets called for the first time. I didn't use StaticallyCacheFor() method either.. As said before.. this is almost vanilla implementation.