Try our conversational search powered by Generative AI!

ITypeSearch Filter not returning results when checking using IContentRepository to see if the Block is used.

Vote:
 

I am attempting to Get a list of blocks that not being used.   

 [ServiceConfiguration(typeof(IContentQuery)), ServiceConfiguration(typeof(IContentSlice))]
    public class MyNewSlice : ContentSliceBase<SitePageData>
    {
        private readonly IContentRepository _contentRepository;

        public MyNewSlice(IClient searchClient, IContentTypeRepository contentTypeRepository, IContentLoader contentLoader, IContentRepository contentRepository) : base(searchClient, contentTypeRepository, contentLoader)
        {
            _contentRepository = contentRepository;
        }

        public override string Name => "My New Slice";

        protected override ITypeSearch<SitePageData> Filter(ITypeSearch<SitePageData> searchRequest, ContentQueryParameters parameters)
        {
            return searchRequest.Filter(x => x.MatchTypeHierarchy(typeof(BlockData)) & _contentRepository.GetReferencesToContent(x.ContentLink, false).Any().Match(false));
        }

        public override int SortOrder => 505;
    }

#301930
May 16, 2023 21:16
Vote:
 

I don't think you'll be able to use _contentRepository.GetReferencesToContent as part of a search request filter. 

Filters that are going to be executed by S&N need to be able to be converted in to search queries for elastic and there's no indexed data for usages of a block in S&N.

The easiest thing to do is to return all your blocks fist then using the _contentRepository.GetReferencesToContent() on the result block values so that it's executed agains the content repo but be aware that's a direct database call and can be expensive so it depends where you're using this.

#302010
May 18, 2023 12:01
Vote:
 

Have a look at this blog post: https://www.codeart.dk/blog/2020/1/powerslice-identify-unused-blocks/

#302215
May 23, 2023 8:16
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.