indexing Sitecore item properties in Lucene

if you want to index properties of an item like ID, TemplateId etc. do the following:
Add to you web.config


                <ItemProperties hint="raw:AddItemProperties">
                  <itemProperty name="TemplateID" target="templateid" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
                  <itemProperty name="ID" target="itemid" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
                  <itemProperty name="Name" target="itemname" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" />
                </ItemProperties>

C# code:


private readonly List<ItemProperty> _itemPropertyCrawlers = new List<ItemProperty>();

        public virtual void AddItemProperties(XmlNode configNode)
        {
            Assert.ArgumentNotNull(configNode, "configNode");
            var name = XmlUtil.GetAttribute("name", configNode);
            var target = XmlUtil.GetAttribute("target", configNode);
            var storageType = XmlUtil.GetAttribute("storageType", configNode);
            var indexType = XmlUtil.GetAttribute("indexType", configNode);
            var vectorType = XmlUtil.GetAttribute("vectorType", configNode);
            var boost = XmlUtil.GetAttribute("boost", configNode);

            ItemProperty itemProperty = new ItemProperty { PropertyName = name, FieldKey = target.ToLowerInvariant() };

            itemProperty.SetStorageType(storageType);
            itemProperty.SetIndexType(indexType);
            itemProperty.SetVectorType(vectorType);
            itemProperty.SetBoost(boost);

            ItemPropertyCrawlers.Add(itemProperty);
        }

Call this method during the indexing in AddCrawler method


        protected void ProcessItemProperties(Document document, Item item)
        {
            foreach (ItemProperty itemPropertyCrawler in ItemPropertyCrawlers)
            {
                string value = itemPropertyCrawler.ResolveValue(item, itemPropertyCrawler.PropertyName);

                if (value.IsNullOrEmpty())
                    return;

                value = IdHelper.ProcessGuiDs(value);

                ProcessField(document, itemPropertyCrawler.FieldKey, value, itemPropertyCrawler.StorageType,
                             itemPropertyCrawler.IndexType, itemPropertyCrawler.VectorType);
            }
        }

ItemProperty class


    public class ItemProperty : BaseItemProperty
    {
        public override string ResolveValue(Item item, string nameOfTheProperty)
        {
            var itemProperty = item.GetType().GetProperty(nameOfTheProperty);

            if (itemProperty == null)
                return String.Empty;

            object[] objects = new Object[0];

            return itemProperty.GetValue(item, objects).ToString();
        }
    }

abstract class for ItemProperty


   public abstract class BaseItemProperty : SearchField
    {
        public string FieldKey { get; set; }
        public string PropertyName { get; set; }
        public abstract string ResolveValue(Item item, string nameOfTheProperty);
    }

This class is already in the AdvancedDatabaseCrawler module but i just added for understaning.


    public class SearchField
    {
        public SearchField()
        {
        }

        public SearchField(string storageType, string indexType, string vectorType, string boost)
        {
            SetStorageType(storageType);
            SetIndexType(indexType);
            SetVectorType(vectorType);
            SetBoost(boost);
        }

        #region Public Properties

        public Field.Store StorageType { get; set; }

        public Field.Index IndexType { get; set; }

        public Field.TermVector VectorType { get; set; }

        public float Boost { get; set; }

        #endregion

        #region Setters

        public void SetStorageType(string storageType)
        {
            switch (storageType)
            {
                case "YES":
                    {
                        StorageType = Field.Store.YES;
                        break;
                    }
                case "NO":
                    {
                        StorageType = Field.Store.NO;
                        break;
                    }
                case "COMPRESS":
                    {
                        StorageType = Field.Store.COMPRESS;
                        break;
                    }
            }
        }

        public void SetIndexType(string indexType)
        {
            switch (indexType)
            {
                case "NO":
                    {
                        IndexType = Field.Index.NO;
                        break;
                    }
                case "NO_NORMS":
                    {
                        IndexType = Field.Index.NO_NORMS;
                        break;
                    }
                case "TOKENIZED":
                    {
                        IndexType = Field.Index.TOKENIZED;
                        break;
                    }
                case "UN_TOKENIZED":
                    {
                        IndexType = Field.Index.UN_TOKENIZED;
                        break;
                    }
            }
        }

        public void SetVectorType(string vectorType)
        {
            switch (vectorType)
            {
                case "NO":
                    {
                        VectorType = Field.TermVector.NO;
                        break;
                    }
                case "WITH_OFFSETS":
                    {
                        VectorType = Field.TermVector.WITH_OFFSETS;
                        break;
                    }
                case "WITH_POSITIONS":
                    {
                        VectorType = Field.TermVector.WITH_POSITIONS;
                        break;
                    }
                case "WITH_POSITIONS_OFFSETS":
                    {
                        VectorType = Field.TermVector.WITH_POSITIONS_OFFSETS;
                        break;
                    }
                case "YES":
                    {
                        VectorType = Field.TermVector.YES;
                        break;
                    }
            }
        }

        public void SetBoost(string boost)
        {
            float boostReturn;

            if (float.TryParse(boost, out boostReturn))
            {
                Boost = boostReturn;
            }

            Boost = 1;
        }

        #endregion
    }


Leave a comment