
13-07-2009
|
Member | | Join Date: May 2008
Posts: 2,383
| |
Re: how to make a search engine The Following code shows the high-level SearchEngine class, which exposes properties such as ResultsPerPage, QueryText, and Highlight, and has a parameterless method called ExecuteQuery. Code: public class SearchEngine
{
private static ISearchProvider _provider = null;
static SearchEngine()
{
if (!string.IsNullOrEmpty(
WebConfigurationManager.AppSettings["Search.Provider"]))
{
_provider = (ISearchProvider)
Activator.CreateInstance(Type.GetType(
WebConfigurationManager.AppSettings[
"Search.Provider"]));
}
else _provider = new LiveSearchProvider();
}
public SearchEngine()
{
if (!string.IsNullOrEmpty(WebConfigurationManager.AppSettings[
"Search.ResultsPerPage"]))
this.ResultsPerPage = Convert.ToInt32(
WebConfigurationManager.AppSettings[
"Search.ResultsPerPage"]);
if (!string.IsNullOrEmpty(
WebConfigurationManager.AppSettings[
"Search.Culture"]))
this.Culture = WebConfigurationManager.AppSettings[
"Search.Culture"];
if (!string.IsNullOrEmpty(
WebConfigurationManager.AppSettings[
"Search.HighlightEnabled"]))
this.Highlight = Convert.ToBoolean(
WebConfigurationManager.AppSettings[
"Search.HighlightEnabled"]);
}
public string ProviderName
{
get { return _provider.GetType().Name; }
}
private int _resultsPerPage = 10;
public int ResultsPerPage
{
get { return _resultsPerPage; }
set { _resultsPerPage = value; }
}
private int _pageIndex = 0;
public int PageIndex
{
get { return _pageIndex; } set { _pageIndex = value; }
}
private string _culture = "en-US";
public string Culture
{
get { return _culture; } set { _culture = value; }
}
private string _queryText = "";
public string QueryText
{
get { return _queryText; } set { _queryText = value; }
}
private bool _highlight = true;
public bool Highlight
{
get { return _highlight; } set { _highlight = value; }
}
public SearchResults ExecuteQuery()
{
return _provider.ExecuteQuery(this.QueryText,
this.Culture, this.ResultsPerPage,
this.PageIndex, this.Highlight);
}
} |