14 February 2006

Two useful tips for dtSearch customization in Sitecore

dtSearch and Mixing Text Query
In dtSearch when I search with multiple words its always exact match search. For example:
If I write query "Product Foobar". When in my page there is text "Foobar Product".

But dtSearch has the following search options defined:

public enum Options
AllWords = 2,
AnyWords = 4,
CaseSensitive = 8,
ExactPhrase = 1,
Logic = 0x20,
Ranked = 0x40,
WildCards = 0x10

To use these options, you should create the custom xslt extension which will return Sitecore.Modules.Search.dtSearch.Search(searchString, Sitecore.Modules.Search.Options.AllWords)

Multiple indexes

Sometimes you might have multiple sites on same installation of the Sitecore and you want search index also be separated to each sites.
For example if you Sitecore content tree looks like:

Then create normally separate indexes in dtSearch indexer on the server and add the following lines in the web.config file.

Intranet" value="Intranet" />

WebSite" value="/index/" />
WebSite" valueWebSite/>

ExtranetSite" value="/index/" />
ExtranetSite" value="ExtranetSite" />

You of course need XSL helper or custom function below to switch between sites

private string GetSearchResultIndex(string dist)
string searchText = txbSearch.Text;

// Instantiate the Search Engine
ISearch engine = Sitecore.Modules.Search.Factory.CreateSearchEngine();

// Get the dtSearch engine implementation
dtSearch your_dtSearch = engine as dtSearch;

// Set index
string pathIndex = Server.MapPath(pathIndexDtSearch(dist) + nameIndexDtSearch(dist) + "\\");
your_dtSearch.Index = pathIndex;

// Execure search
your_dtSearch.SearchTextOptions(searchText, Options.AllWords);

// Get search resalts as XML
System.Xml.XmlDocument searchResult = your_dtSearch.SearchResults;

// Format result
string result = string.Empty;
System.Xml.XmlNodeList list = searchResult.SelectNodes("/sitecore/result/item");
foreach(System.Xml.XmlNode node in list)
string url = node.SelectSingleNode("url").InnerText;
result += "" + url + "

return result;

No comments: