This article explains the basic component parts of a search query and shows how to perform a basic search.
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.
Example:
/// <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.
Language
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:
- Litium.Foundation.Modules.CMS.Search.CmsSearchDomains
- Litium.Foundation.Modules.ECommerce.Search.ECommerceSearchDomains
- Litium.Foundation.Modules.ProductCatalog.Search.ProductCatalogSearchDomains
- Litium.Foundation.Modules.Relations.Search.RelationsSearchDomains
- Litium.Foundation.Modules.MediaArchive.Search.MediaArchiveSearchDomains
- 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)
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.