Previously I published an article explaining how to fix a glitch in Sitecore index that keeps old data after updating an item. In this article I updated the code since some issues were fixed in Sitecore 6.2 (Update-4) and 6.3.
Starting from 6.2 (Update-4) and 6.3 the _template field in Lucene index stores an item ID in ShortID format. Now it’s even easier to customize the DatabaseCrawler as the following methods were made virtual: AddItem, IndexVersion, DeleteItem and DeleteVersion. Also fields _hasIncludes, _hasExludes and _templateFilder were made protected which helps to shrink the fix even more.
Here is the code that fixes the outlined problem for these releases:
- namespace Lucene.Search.Crawlers
- {
- public class DatabaseCrawler : Sitecore.Search.Crawlers.DatabaseCrawler
- {
- protected override void AddMatchCriteria(Net.Search.BooleanQuery query)
- {
- query.Add(new TermQuery(new Term(BuiltinFields.Database, RootItem.Database.Name)), BooleanClause.Occur.MUST);
- query.Add(new TermQuery(new Term(BuiltinFields.Path, ShortID.Encode(RootItem.ID).ToLowerInvariant())), BooleanClause.Occur.MUST);
- if (this._hasIncludes || this._hasExcludes)
- {
- foreach (KeyValuePair<string, bool> pair in this._templateFilter)
- {
- query.Add(new TermQuery(new Term(BuiltinFields.Template, ShortID.Encode(pair.Key).ToLowerInvariant())), pair.Value ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST_NOT);
- }
- }
- }
- protected Item RootItem
- {
- get
- {
- return Sitecore.Data.Managers.ItemManager.GetItem(Root, Sitecore.Globalization.Language.Invariant,
- Sitecore.Data.Version.Latest,
- Sitecore.Data.Database.GetDatabase(Database),
- Sitecore.SecurityModel.SecurityCheck.Disable);
- }
- }
- protected override Query GetVersionQuery(ID id, string language, string version)
- {
- Assert.ArgumentNotNull(id, "id");
- Assert.ArgumentNotNullOrEmpty(language, "language");
- Assert.ArgumentNotNullOrEmpty(version, "version");
- BooleanQuery query = new BooleanQuery();
- query.Add(new TermQuery(new Term(BuiltinFields.ID, GetItemID(id, language, version).ToLowerInvariant())), BooleanClause.Occur.MUST);
- this.AddMatchCriteria(query);
- return query;
- }
- }
- }
Basically you need to make sure that the result of GetItemID method is in lower case as well as a term for _path field, in AddMatchCriteria method, is constructed with lower case query.
No comments:
Post a Comment