How to search for pages of base type with EPiServer built-in search

Member since: 2010

Can't I use this?

//Search for pages using the provided language
var pageTypeQuery = new GroupQuery(LuceneOperator.AND);
pageTypeQuery.QueryExpressions.Add(new ContentQuery<SearchableSitePageData>());
pageTypeQuery.QueryExpressions.Add(new FieldQuery(languageBranch, Field.Culture));

//Search for blocks using the provided language
var blockTypeQuery = new GroupQuery(LuceneOperator.AND);
blockTypeQuery.QueryExpressions.Add(new ContentQuery<SearchableSiteBlockData>());
blockTypeQuery.QueryExpressions.Add(new FieldQuery(languageBranch, Field.Culture));


where SearchableSitePageData is

    public abstract class SearchableSitePageData : SitePageData, IShowInSearchResults
    {
    }

and SitePageData inherits from PageData.

#86515 May 22, 2014 18:59
  • Member since: 2007

    Similar code works for me:

    var typeQuery = new ContentTypesQuery();
    
    foreach (string type in SearchPage.ContentTypesCategories)
    {
        typeQuery.AddContentType(type);
    }
    
    var accessQuery = new AccessControlListQuery();
    accessQuery.AddAclForUser(PrincipalInfo.Current, HttpContext.Current);
    
    var query = new GroupQuery(LuceneOperator.AND);
    query.QueryExpressions.Add(typeQuery);
    query.QueryExpressions.Add(accessQuery);
    query.QueryExpressions.Add(new FieldQuery(searchText));
    #86518 May 22, 2014 22:15
  • Member since: 2010

    Hey, but is ContentTypesQuery a class of your own?

    #86533 May 23, 2014 9:08
  • Member since: 2007

    Haha, sorry. Sure it was. But it's a really simple class.

    using EPiServer.Search.Queries.Lucene;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    public class ContentTypesQuery : CollectionQueryBase
    {
        private readonly string AssemblyName;
    
        public ContentTypesQuery()
            : base("EPISERVER_SEARCH_TYPE", LuceneOperator.OR)
        {
            var assembly = this.GetType().Assembly;
    
            AssemblyName = assembly.GetName().Name;
        }
    
        public void AddContentType(string name)
        {
            base.Items.Add(string.Format("{0},{1}", name, AssemblyName));
        }
    }
    #86570 May 23, 2014 19:22
  • Member since: 2007

    And the reason was that we wanted the editors to be able to chose which types that should be searchable.

    I guess ContentQuery<SearchableSitePageData>() is implemented in a similar way.

    #86571 May 23, 2014 19:26