Remove expired page data from search results

Vote:
 

Hi, I use a Find query on few different page types to retrieves Episerver.Find.SearchResults type data set. The results contain the pages that are 'Expired'. I simply need to remove the 'Expired' pages from the results.

1. Can we avoid indexing 'Expired' pages?

2. How to remove 'Expired' pages from the index? Or

3. How to remove 'Expired' pages from query results?

I tried 'SkipWhile' on the Results, but then it returned no values.

I also tried to add a query filter with 'page.CheckPublishedStatus(PagePublishedStatus.Published)', but it also returned no values.

I believe there must be ab simple easy way to do this. Please help. 

#141276
Nov 11, 2015 7:57
Vote:
 

Have you tried adding .FilterForVisitor() to your search query? This should take care of this.

#141277
Nov 11, 2015 8:16
Vote:
 

Thanks for the quick reply. 

I tried to add FilterForVisitor() in the query and getting this error;

'ISearch<SearchResult>' does not contain a definition for 'FilterForVisitor' and no extension method 'FilterForVisitor' accepting a first argument of type 'ISearch<SearchResult>' could be found 

are there any examples of the usage of this filter? 

#141286
Nov 11, 2015 11:32
Vote:
 

It will only work for IContent. Here is the documentation: http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Find/10/Integration/EPiServer-75/Filters/

Perhaps you could post your query

#141290
Nov 11, 2015 12:51
Vote:
 

I use initialization module to delete expired or deleted pages (inside recycle bin):

[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class EpiFindInitializationModule : IInitializableModule
{
    private IContentEvents _contentEvents;
    private IContentEvents ContentEvents
    {
        get { return _contentEvents ?? (_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>()); }
    }

    public void Initialize(InitializationEngine context)
    {
        ContentEvents.SavedContent += ContentEvents_SavedContent;

        // exclude deleted (inside recycle bin) or expired pages
        ContentIndexer.Instance.Conventions
                        .ForInstancesOf<PageData>()
                        .ShouldIndex(x => x.ContentLink.ID != ContentReference.WasteBasket.ID &&
                                        !x.Ancestors().Contains(ContentReference.WasteBasket.ID.ToString()) &&
                                        x.StopPublish > DateTime.Now);
    }

    private void ContentEvents_SavedContent(object sender, EPiServer.ContentEventArgs e)
    {
        var page = e.Content as PageData;
            
        // delete expired page
        if (page != null && page.StopPublish <= DateTime.Now)
        {
            SearchClient.Instance.Delete<PageData>(x => x.PageLink.Match(page.PageLink));
        }
    }  

    public void Preload(string[] parameters) { }

    public void Uninitialize(InitializationEngine context)
    {
        ContentEvents.SavedContent -= ContentEvents_SavedContent;
    }
}


And a scheduled job that calls:

SearchClient.Instance.Delete<PageData>(x => x.StopPublish.Before(DateTime.Now))
#141301
Edited, Nov 11, 2015 14:48
Vote:
 

I'm trying using FilterForVisitor() with no success. I'm still having the expired content in results. Does Find update the index when a user sets expiration on a page in editor mode?

#174435
Edited, Jan 26, 2017 11:07
Vote:
 

Maybe run scheduled job and reindex?

#174437
Jan 26, 2017 11:24
Vote:
 

Should I reindex the whole website everytime a single page expires?! That's nonsense... I was hoping that Find has some functionality which removes pages from index if they expire...

#174439
Jan 26, 2017 11:35
Vote:
 

It should remove those from index automatically from what I gather with newer versions of Find at least. I know there was a bug with files for expired pages but that was fixed in Episerver Find 12. So I would try running the scheduled job once and see if it works automatically after that. 

#174466
Jan 26, 2017 16:18