Search

Search API

Simple example:

 

using N2.Persistence.Search;
...
  foreach(var hit in N2.Content.Search.Text.Search(Query.For("ipsum")).Hits)
  {
    Console.WriteLine(hit.Title);
  }

 

Search Providers

By default N2 provides a number of search providers. All use the same search API but may have different search characterstics, performance and implemented features.

Database search

Without any additional packages using the search API will resolve content using database queries.

 Lucene search

Installing the N2CMS.Search.Lucene package enables search using Lucene technology. After installing the package the search index is updated whenever a page is saved. Re-index of all content can be initiated from site settings in the management UI.

Remote lucene search

Installing the N2CMS.Search.Remote enables a client-server search option which creates an index which is managed by a process separate web process. After installing the package the server exe located below /RemoteSearchServer must be started. This server can be moved to a separate machine and be shared across multiple websites. Internally the server maintains a lucene index.

INSTALLATION

* Install the N2CMS.Search.Remote nuget package
* this is added to web.config:
  <n2><database>
    <search type="RemoteServer">
      <client sharedSecret="changemeHereAndOnServerConfig" />
* Run RemoteSearchServer\GrantListenPortPermissionToServer.bat
* Run RemoteSearchServer\N2.Search.Remote.Server.exe
* Go to the management UI and publish a page
* Search using N2's search API

CONFIGURE Shared Secret

* Exit the server
* Edit web.config on the website
* Change <client sharedSecret="..." to something really secret
* Edit RemoteSearchServer\N2.Search.Remote.Server.exe.config
* Change <server sharedSecret="..." to the same text
* Run RemoteSearchServer\N2.Search.Remote.Server.exe

CONFIGURE Index Path
* Exit the server
* Edit RemoteSearchServer\N2.Search.Remote.Server.exe.config
* Change indexPath="C:\ProgramData\N2\SearchIndex\" to a path where the index should be stored, e.g. indexPath="D:\N2\SearchIndex\"
* Run RemoteSearchServer\N2.Search.Remote.Server.exe
* Trigger re-index of all content

CONFIGURE Multiple Websites Sharing Same Server

* Edit web.config on the website
* Add instanceName attribute:
  <database>
    <search>
      <client instanceName="WebSite1" />
* Re-index all pages

REINDEXING pages

* In the mangement UI go to N2/Site Settings
* Press "Schedule index of all content"

MOVING Search Server

* XCOPY all files below RemoteSearchServer to a directory and server of choice
* Run N2.Search.Remote.Server.exe
* Edit web.config on the website
* Change <client url="http://myserver:7850/" /> to any new server (localhost is default)

How to EXCLUDE items from Search

If you're using DatabaseSearch.cs and Search.aspx in the Templates project, you should should see that DatabaseSearch inherits from AbstractSearch.  In AbstractSearch.GetFilters(), add a new filter to do just what you want.  For example, the following example introduces a filter that excludes TextPages from the search index:

protected virtual List<ItemFilter> GetFilters()
{
	List<ItemFilter> filters = new List<ItemFilter>();
	filters.Add(new NavigationFilter());
	filters.Add(new InverseFilter(new TypeFilter(typeof(TextPage)))); // this line excludes all TextPages from the search results
	if (SearchRoot != null)
		filters.Add(new ParentFilter(SearchRoot));
	return filters;
}