Home > Lucene Search > Step 1: Implement a Searcher Class

Step 1: Implement a Searcher Class

When you have completed this bidirectional example, if you view an office product or conduct a search of office products, a rendering control is displayed that shows all related items in the side bar of the page.

standard related items

The Searcher and the Query Runner make this possible by invoking a database crawler to search the indexes for any related items, depending on which Web control is used. The Web control then processes the results and displays them on the Web site.

Searcher.cs contains multiple methods that query the indexes in several different ways. The full code for each method is contained in the Query Runner; it is not visible in the Searcher. The Searcher simply contains a list of methods used.

The Searcher can only execute one method at a time. When a method in the Searcher is invoked by the Related Items Web control, the Runner is called. The Runner searches the indexes and sends a list of items to the Searcher used by the Related Items Web control to display related items to the site visitor.

Searcher.cs

To create a Searcher:

1. First include the following namespaces:

namespace Sitecore.Training.Index

{

using System;

using System.Collections.Generic;

using Search;

using Data;

using Data.Items;

using Collections;

2. Name the class Searcher: public class Searcher

3. Create a new instance of the Query Runner property to be used by the Searcher.

protected QueryRunner Runner { get; set; }

4. Create two constructors that are triggered when you run the Searcher. Only one of these constructors should accept parameters to specify which index is used when you conduct a search.

Constructors Parameters
public Searcher(){Runner = new QueryRunner(Constants.Index.Name);

}

() No parametersNo index id specified, so the query runner tries to find the index by calling index.name
public Searcher(string indexId){Runner = new QueryRunner(indexId);

}

(string indexId) is specified as the parameter to use to fetch the search index.

5. Define a new region in the code for search methods and then add each method that you want to use. Each search method is basically a query. This example shows the first method in Searcher.cs: GetItemsByFullTextQuery

#region Searching Methods

public virtual List<Item> GetItemsByFullTextQuery(string query)

{

var results = new List<SearchResult>(Runner.GetItemsByFullTextQuery(query));

return GetItemsFromSearchResult(results);

}

Search methods are only triggered when the Related Items control calls one. When a search method is called a new instance of the Query Runner is created in the Searcher. The new instance is called Runner.

var results = new List<SearchResult>(Runner.GetItemsByFullTextQuery(query));

Runner then searches the indexes and sends back all relevant search results to the method.

return GetItemsFromSearchResult(results);

The search results list returned to the Searcher is converted by the GetItemsFromSearchResult method into a result set containing items. This method iterates through the list of results and finds the url for each item.

It takes the search results extracted from the index and then stores them in a resultingSet list.

6. Next create a GetItemsFromSearchResult method. In this method the set of items stored in the resultingSet list is returned with the corresponding url so that you can click a link to get the item.

public static List<Item> GetItemsFromSearchResult(IList<SearchResult> searchResults)

{

var resultingSet = new List<Item>();

foreach (var result in searchResults)

{

var uriField = result.Document.GetField("_url");

if (uriField != null && !String.IsNullOrEmpty(uriField.StringValue()))

{

var itemUri = new ItemUri(uriField.StringValue());

resultingSet.Add(Context.Database.GetItem(new DataUri(itemUri)));

}

}

return resultingSet;

}

Methods and Parameters in the Searcher Class

Searcher Methods Parameters
GetItemsByFullTextQuery(string query) - full text query on a string
GetRelatedItemsByMultipleFields(string query, SafeDictionary<string> refinements) - full text query on a string- the second parameter searches for refinements using SafeDictionary, then creates a list of dictionary refinements – brand = IKEA, Category = Desk etc
GetRelatedItems(string ids) – Ids = returns the Ids of all items
GetRelatedItemsByField(string ids, string fieldName, bool partial) - Ids = Id of the product selected in the Related Items field. For example, the ‘Standard’ product.- fieldname – the related items field name- partial = boolean
ContainsItemsByFields(string ids, string fieldName, string fieldValue) - Applies an ID filter- fieldName,- string fieldValue = Brand e.g. IKEA
GetItemsFromSearchResult(IList<SearchResult> searchResults) - Search result list – for each item in the search result list this method gets the url and creates another list called resultingSet.

Next Step: Implementing a Query Runner Class

Categories: Lucene Search

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.