Faceted Search With Lucene.net in Sitecore

Search Manager Class


    using System.Collections;
    using Extention;
    using Helper;
    using Sitecore.Search;
    using Sitecore.Sites;
    using global::Lucene.Net.QueryParsers;
    using global::Lucene.Net.Search;

    public static class FacetSearchManager
    {
        private static QueryParser _queryParser;
        private static QueryFilter _queryFilter;
        private static BitArray _bitArray;

        public static QueryParser QueryParser
        {
            get
            {
                if (_queryParser == null)
                {
                    using (Indexer indexer = new Indexer(Indexer.IndexerName(Sitecore.Context.Database.Name)))
                    {
                        _queryParser = new QueryParser(BuiltinFields.Content, indexer.Index.Analyzer);
                        _queryParser.SetDefaultOperator(QueryParser.Operator.AND);
                        _queryParser.SetAllowLeadingWildcard(true);

                        return _queryParser;
                    }

                }
                return _queryParser;
            }
        }

        public static int FacetedSearch(BooleanQuery booleanQuery)
        {
            using (Indexer indexer = new Indexer(Indexer.IndexerName(Sitecore.Context.Database.Name)))
            {
                using (IndexSearchContext indexSearchContext = new IndexSearchContext(indexer.Index))
                {
                    if (booleanQuery != null)
                    {
                        _queryFilter = new QueryFilter(booleanQuery);
                        _bitArray = _queryFilter.Bits(indexSearchContext.Searcher.GetIndexReader());

                        return Util.GetCardinality(_bitArray);
                    }
                }
            }
            return 0;
        }

        public static int FacetedSearch(BooleanQuery booleanQuery, IndexSearchContext indexSearchContext)
        {
            if (booleanQuery != null && indexSearchContext != null)
            {
                _queryFilter = new QueryFilter(booleanQuery);
                _bitArray = _queryFilter.Bits(indexSearchContext.Searcher.GetIndexReader());
                return Util.GetCardinality(_bitArray);
            }

            return 0;
        }
    }

The Cardinality method


        public static int GetCardinality(BitArray bitArray)
        {
            FieldInfo fieldInfo = bitArray.GetType().GetField("m_array", BindingFlags.NonPublic | BindingFlags.Instance);

            if (fieldInfo != null)
            {
                uint[] array = (uint[])fieldInfo.GetValue(bitArray);

                return array.Sum(value => Bitcounts[value & 65535] + Bitcounts[(value >> 16) & 65535]);
            }
            return 0;
        }

The Indexer Class


    using System;
    using Sitecore.Search;
    using global::Lucene.Net.Search;

   
    public class Indexer : IDisposable
    {
        public static string IndexerName(string databaseName)
        {
            string indexName;

            switch (databaseName)
            {
                case "web":
                    indexName = IndexConstants.WebIndexName;
                    break;
                case "master":
                    indexName = IndexConstants.MasterIndexName;
                    break;
                default:
                    indexName = IndexConstants.DefaultIndexName;
                    break;
            }
            return indexName;
        }

        public Indexer(string indexId)
        {
            Index = Sitecore.Search.SearchManager.GetIndex(indexId);
            BooleanQuery.SetMaxClauseCount(4096);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting Lucene Indexer resources.
        /// </summary>
        public void Dispose()
        {
            Index = null;
        }

        public Index Index { get; set; }
    }

Name IndexConstants for Lucene index folder name in Date Folder


    public class IndexConstants
    {
        public static readonly string MasterIndexName = "masterindex";
        public static readonly string WebIndexName = "webindex";
        public static readonly string DefaultIndexName = "masterindex";
    }

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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