Reputation: 13
I have searched high and low for a plain-english example, with full instructions and all of the code needed, to set up an index for my live site database on a site running Sitecore.NET 6.5.0 (rev. 111230). The blogs from Sitecore MVP's and Sitecore's official documentation go some of the way but I'm frustrated that never have I seen a complete example, with all of the code. I am a beginning developer, thrust into this position (yes, I know, you recommend the training - well, my employer hasn't seen fit to pay for it yet). I have tried at least four different times using four different methods and I still don't have a search index that can find all of my live pages. Sitecore's official documentation is incomplete and thus of no help. So I am turning to this community. Please help.
(Edited to refine the question, after first comment) I know about this post Very basic usage of sitecore search but get stuck on step 2. There is a line of code there, but what do I do with that? I'm sorry to appear ignorant, but I do not know what to do with that, and nowhere does any documentation go to that level of detail. If not here, then where can I find this information?
Edit 2: this is how I have the index configured.
<!-- id must be unique -->
<index id="milo-live-index" type="Sitecore.Search.Index, Sitecore.Kernel">
<!-- name - not sure if necessary but use id and forget about it -->
<param desc="name">$(id)</param>
<!-- folder - name of directory on the hard drive -->
<param desc="folder">__milo-live-index</param>
<!-- analyzer - reference to analyzer defined in Sitecore.config -->
<Analyzer ref="search/analyzer" />
<!-- list of locations to index - each of the with unique xml tag -->
<locations hint="list:AddCrawler">
<!-- first location (and the only one in this case) - specific folder from you question -->
<!-- type attribute is the crawler type - use default one in this scenario -->
<specificfolder type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
<!-- indexing itmes from production database -->
<Database>production</Database>
<!-- your folder path -->
<Root>/sitecore/content/homeowners</Root>
</specificfolder>
</locations>
And I go to the sitecore desktop, control panel, database / rebuild the search index, and the only index i see is the "Quick Search".
Upvotes: 1
Views: 3191
Reputation: 2782
Here is example search index configuration. You should put this inside Web.config (under configuration/indexes) in section
<index id="help_pages_index" type="Sitecore.Search.Index, Sitecore.Kernel">
<param desc="name">$(id)</param>
<param desc="folder">help_pages_index</param>
<Analyzer ref="search/analyzer" />
<locations hint="list:AddCrawler">
<web-help type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
<Database>web</Database>
<Root>/sitecore/content/Pages/Help</Root>
<templates hint="list:IncludeTemplate">
<helparticle>{FF48919D-393C-4F8D-9D1A-AC6B58CEC896}</helparticle>
<helpfaq>{E66F97CF-18CF-4D73-AC84-0064D7626524}</helpfaq>
</templates>
</web-help>
</locations>
</index>
This configuration creates new index which indexes items inside root item (/sitecore/content/Pages/Help) with one of two specified templates (helparticle, helpfaq).
After you put this to Web.config you can create simple aspx page and in Page_Load put this:
SearchManager.GetIndex("help_pages_index").Rebuild();
Execute this page once. It should rebuild your index and it should show in control panel. Alternatively you could consider to upgrade to latest version of Sitecore 6.5. In latest version index should be added automatically to control panel.
Then you can use your index from code. This example is more advanced, because it gets also hits score from lucene.
public class SearchHelper
{
public static SearchResults SearchHelp(string searchExpression, int start, int end)
{
var searchResults = new SearchResults { SearchPhrase = string.Empty, TotalHits = 0, Items = new List<SearchResultItem>() };
if (!string.IsNullOrEmpty(searchExpression))
{
searchResults.SearchPhrase = searchExpression;
var searchIndex = SearchManager.GetIndex("help_pages_index");
if (searchIndex != null)
{
using (var searchContext = searchIndex.CreateSearchContext())
{
var queryParser = new QueryParser(BuiltinFields.Content, searchIndex.Analyzer);
queryParser.SetDefaultOperator(QueryParser.Operator.AND);
var fullTextQuery = queryParser.Parse(QueryParser.Escape(searchExpression.ToLowerInvariant()));
var languageQuery = new PhraseQuery();
languageQuery.Add(new Term(BuiltinFields.Language, Sitecore.Context.Language.Name.ToLowerInvariant()));
var innerQuery = new BooleanQuery();
innerQuery.Add(fullTextQuery, BooleanClause.Occur.MUST);
innerQuery.Add(languageQuery, BooleanClause.Occur.MUST);
var hits = searchContext.Searcher.Search(innerQuery);
var hitsLength = hits.Length();
searchResults.TotalHits = hitsLength;
if (hitsLength > 0 && start < hitsLength)
{
if (end >= hitsLength)
{
end = hitsLength - 1;
}
for (var i = start; i <= end; i++)
{
var url = hits.Doc(i).Get(BuiltinFields.Url);
if (!string.IsNullOrEmpty(url))
{
var item = Database.GetItem(ItemUri.Parse(url));
if (item != null && !item.Empty)
{
searchResults.Items.Add(new SearchResultItem { Item = item, Score = hits.Score(i) });
}
}
}
}
}
}
}
return searchResults;
}
}
Upvotes: 3