Building a search query

This article explains the basic component parts of a search query and shows how to perform a basic search.

Build your query

First step in searching is to build a search query, and then to submit the query into the "SearchService" for execution. The results returned contains the search hits.


        /// <summary>
        /// Searches for the given text in web pages.
        /// </summary>
        /// <param name="websiteId">The website id.</param>
        /// <param name="searchText">The search text.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public static List<Page> SearchForTextInPages(Guid websiteId, string searchText, SecurityToken token)
            var webSite = WebSite.GetFromID(ModuleCMS.Instance, websiteId);

            //request is initiated with the language, Lucene index name and security token parameters
            var request = new QueryRequest(webSite.Language.ID, CmsSearchDomains.Pages, token);
            //text to search.            
            request.Text = searchText;

            //Filter the result.
            request.FilterTags.Add(new Tag(TagNames.WebSiteId, websiteId)); //only pages in this website.
            request.FilterTags.Add(new Tag(TagNames.IsPublished, bool.TrueString)); //page must be published
            request.FilterTags.Add(new Tag(TagNames.IsSearchable, bool.TrueString)); //page should be searchable
            request.FilterTags.Add(new Tag(TagNames.IsArchived, bool.FalseString)); //not archived
            request.FilterTags.Add(new Tag(TagNames.IsInTrashcan, bool.FalseString)); //not in trashcan

            var hits = Solution.Instance.SearchService.Search(request);

            return hits.Hits.Select(x => Page.GetFromID(new Guid(x.Id), token)).ToList();

Initializing the Query Request

The search request is initialized with the language, index name and the security token.



This parameter determines the language in which the search text is given. It is important when searching in multi language property fields. A web page is written only in a single language, so the language will most probably be the language of the website. However, in other cases such as searching for products, it might be important in which language the search is performed.

In certain cases, language can be immaterial. For example, if the search if for a article number in the product catalog, in which case the filed is language independant, which means what ever the language used, we will get hits.

Search index name

This parameter defines which index to search. It is important to search in the correct index, otherwise search result can be incorrect. The indexes are defined in each module and can be accessed from following classes:

  1. Litium.Foundation.Modules.CMS.Search.CmsSearchDomains
  2. Litium.Foundation.Modules.ECommerce.Search.ECommerceSearchDomains
  3. Litium.Foundation.Modules.ProductCatalog.Search.ProductCatalogSearchDomains
  4. Litium.Foundation.Modules.Relations.Search.RelationsSearchDomains
  5. Litium.Foundation.Modules.MediaArchive.Search.MediaArchiveSearchDomains
  6. Litium.Foundation.Modules.Newsletter.Search.NewsletterSearchDomains

Under each module, more information are given on indexes available.

Security Token

The token is used to check the access permissions to the search. Search results returned are filtered based on read permissions the user has.

Search Text

Specified in "request.Text" in above example.

This is the text that is searched for. The text should be in the language specified in the query request. The search text can be omitted, and a search performed to retrieve all the indexed items and filter them. If the search text is not specified, there must be atleast one filter added. (See below).

Filtering the result

The search result can be filtered by instructing the search engine by way of adding filter tags. Tags are name-value pairs, and the names are defined for each module.


It is important that the tag name and the type of the tag value should match. For example, if the tag name indicates that it is expecting a boolean, the value should either be a "true" or a "false"; in this instance passing 1 or 0 to mean true or false might not yeild the expected results. (Details are explained under each module)

Getting tagname examples:

using Litium.Foundation.Modules.ProductCatalog.ExtensionMethods;

public string GetTagNameForFieldFrameork(Litium.Products.FieldDefinitionService fieldDefinitionService, string fieldName, System.Globalization.CultureInfo culture = null)
    // Tagnames for fields of products are avaliable through the GetTagName method on the fielddefinition. 
    Litium.Products.FieldDefinition fieldDefinition = fieldDefinitionService.Get(fieldName);

    // With culture the tagname is created with the correct language if the field is language dependent
    return fieldDefinition.GetTagName(culture);

public string GetTagNameForSystemDescriptionField(Litium.Products.FieldDefinitionService fieldDefinitionService, System.Globalization.CultureInfo culture = null)
    // System fields are avaliable in the Litium.FieldFramework.SystemFieldDefinitionConstants
    return GetTagNameForFieldFrameork(fieldDefinitionService, Litium.FieldFramework.SystemFieldDefinitionConstants.Description, culture);

public string GetTagNameForNonFieldFramework(string propertyName)
    // Replace CMS with the module which is target for the searchquery
    return Litium.Foundation.Modules.CMS.Search.TagNames.GetTagNameForProperty(propertyName);

Finally the search is done by submitting the request to the Solution.Instance.SearchService.Search() method. The results returned is a list of Hits, where Id is the id of the document searched for. For example, if the search index is CmsSearchDomains.Pages, the results Id will contain the page id. If the search index was ProductCatalogSearchDomains.Products, the id of a search hit is the product id.




Is this page helpful?
Thank you for your feedback!