Using Lucene Analyzers
In Litium Studio, Lucene search index is built using the Standard Analyzer in Lucene. There are particular instances where it is required to use other different analyzers when searching: for example when a word contain special characters it is better to use the WhitespaceAnalyzer when searching.
This article explains how to change the analyzer when searching.
Following function makes a Lucene search query, with WhiteSpaceAnalyzer.
/// <summary>
/// Builds a query to search for words which are seperated by a special charactors, such a '-'.
/// forexample, if a searchText = forget-me-not no search hits will result in without this following line,
/// even if there is a product called 'forget-me-not'.
/// </summary>
/// <param name="tagName">Name of the tag.</param>
/// <param name="tagValue">The tag value.</param>
/// <returns></returns>
private static Lucene.Net.Search.Query ParseTagWhiteSpace(string tagName, string tagValue)
{
string searchText = QueryParser.Escape(tagValue.ToLower(CultureInfo.CurrentCulture));
var parser = new MultiFieldQueryParser(LuceneSearchService.CurrentVersion, new[] { tagName },
new WhitespaceAnalyzer()) { DefaultOperator = QueryParser.Operator.AND };
var q = parser.Parse(searchText);
return q;
}
The above method can be used in a search query using the following way.
var request = new QueryRequest(languageId, ProductCatalogSearchDomains.Products, FoundationContext.Token);
request.FilterTags.Add(new QueryTag { Query = ParseTagWhiteSpace(FieldNames.Body, term) });
More comprehensive example is described under the Product Catalog search section, under free text search article.