/// <summary>
 /// The number of documents deleted in the index
 /// </summary>
 /// <param name="indexer"></param>
 /// <returns></returns>
 public static int GetDeletedDocumentsCount(this LuceneIndexer indexer)
 {
     using (var reader = indexer.GetIndexWriter().GetReader())
     {
         return(reader.NumDeletedDocs());
     }
 }
 /// <summary>
 /// Returns true if the index is optimized or not
 /// </summary>
 /// <param name="indexer"></param>
 /// <returns></returns>
 public static bool IsIndexOptimized(this LuceneIndexer indexer)
 {
     using (var reader = indexer.GetIndexWriter().GetReader())
     {
         return(reader.IsOptimized());
     }
 }
 /// <summary>
 /// Return the total number of fields in the index
 /// </summary>
 /// <param name="indexer"></param>
 /// <returns></returns>
 public static int GetIndexFieldCount(this LuceneIndexer indexer)
 {
     using (var reader = indexer.GetIndexWriter().GetReader())
     {
         return(reader.GetFieldNames(IndexReader.FieldOption.ALL).Count);
     }
 }
Пример #4
0
        private void IndexContent()
        {
            log.AuditInfo("Start index search plugin");

            var writer = indexer.GetIndexWriter(true);

            using (var database = new Database(Connection, Database.MsSqlClientProvider)
            {
                CommandTimeout = 122333
            })
            {
                var content = database.Fetch <dynamic>(@"
          SELECT DISTINCT
            SC.ProductID, 
            ISNULL(SC.ProductName, SC.VendorItemNumber) AS ProductName, 
            SC.VendorItemNumber, 
            ISNULL(SC.CustomItemNumber, SC.vendoritemnumber) AS CustomItemNumber,
            ISNULL(SC.SearchText, SC.VendorItemNumber) AS SearchText
          FROM [dbo].[SearchContent] AS SC 
          INNER JOIN [dbo].[Product] AS P ON P.ProductID = SC.ProductID
          WHERE P.IsConfigurable = 1
          ORDER BY SC.VendorItemNumber");

                try
                {
                    foreach (var c in content)
                    {
                        var doc = new Document();

                        doc.Add(new Field("ID", c.ProductID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                        doc.Add(new Field("productName", c.ProductName, Field.Store.YES, Field.Index.NOT_ANALYZED));
                        doc.Add(new Field("vendorItemNumber", c.VendorItemNumber, Field.Store.YES, Field.Index.NOT_ANALYZED));
                        doc.Add(new Field("customItemNumber", c.CustomItemNumber, Field.Store.YES, Field.Index.NOT_ANALYZED));
                        doc.Add(new Field("searchText", c.SearchText, Field.Store.YES, Field.Index.ANALYZED));

                        writer.AddDocument(doc);
                    }
                }
                catch (Exception e)
                {
                    log.Debug(e.InnerException == null ? e.Message : e.InnerException.Message);
                }
                finally
                {
                    indexer.CloseIndexWriter(); // removes the write.lock file
                }
            }

            log.AuditSuccess("Finished search index plugin");
        }
 /// <summary>
 /// Returns true if the index is optimized or not
 /// </summary>
 /// <param name="indexer"></param>
 /// <returns></returns>
 public static bool IsIndexOptimized(this LuceneIndexer indexer)
 {
     try
     {
         using (var reader = indexer.GetIndexWriter().GetReader())
         {
             return(reader.IsOptimized());
         }
     }
     catch (AlreadyClosedException)
     {
         LogHelper.Warn(typeof(ExamineExtensions), "Cannot get IsIndexOptimized, the writer is already closed");
         return(false);
     }
 }
 /// <summary>
 /// Return the number of indexed documents in Lucene
 /// </summary>
 /// <param name="indexer"></param>
 /// <returns></returns>
 public static int GetIndexDocumentCount(this LuceneIndexer indexer)
 {
     try
     {
         using (var reader = indexer.GetIndexWriter().GetReader())
         {
             return(reader.NumDocs());
         }
     }
     catch (AlreadyClosedException)
     {
         LogHelper.Warn(typeof(ExamineExtensions), "Cannot get GetIndexDocumentCount, the writer is already closed");
         return(0);
     }
 }
 /// <summary>
 /// Checks if the index can be read/opened
 /// </summary>
 /// <param name="indexer"></param>
 /// <param name="ex">The exception returned if there was an error</param>
 /// <returns></returns>
 public static bool IsHealthy(this LuceneIndexer indexer, out Exception ex)
 {
     try
     {
         using (indexer.GetIndexWriter().GetReader())
         {
             ex = null;
             return(true);
         }
     }
     catch (Exception e)
     {
         ex = e;
         return(false);
     }
 }
        /// <summary>
        /// Return the total number of fields in the index
        /// </summary>
        /// <param name="indexer"></param>
        /// <returns></returns>
        public static int GetIndexFieldCount(this LuceneIndexer indexer)
        {
            //TODO: check for closing! and AlreadyClosedException

            try
            {
                using (var reader = indexer.GetIndexWriter().GetReader())
                {
                    return(reader.GetFieldNames(IndexReader.FieldOption.ALL).Count);
                }
            }
            catch (AlreadyClosedException)
            {
                LogHelper.Warn(typeof(ExamineExtensions), "Cannot get GetIndexFieldCount, the writer is already closed");
                return(0);
            }
        }
        protected override void ExecuteTask()
        {
            using (var indexer = new LuceneIndexer())
            {
                TraceVerbose("Generating product search information...");

                Database.Execute("EXEC [dbo].[GenerateSearchResults]");

                var query = new QueryBuilder()
                            .From("[dbo].[SearchContent] AS [SC]")
                            .Join(JoinType.Inner, "[dbo].[Product] AS [P]", "[P].[ProductID] = [SC].[ProductID]")
                            .Select("[SC].[ProductID]", "[SC].[VendorItemNumber]")
                            .Column("ISNULL([SC].[ProductName], [SC].[VendorItemNumber]) AS [ProductName]")
                            .Column("ISNULL([SC].[CustomItemNumber], [SC].[VendorItemNumber]) AS [CustomItemNumber]")
                            .Column("ISNULL([SC].[SearchText], [SC].[VendorItemNumber]) AS [SearchText]")
                            .OrderBy("[SC].[VendorItemNumber]");

                var searchContent = Database.Query <SearchContentModel>(query).ToArray();

                TraceVerbose("{0} product search records found.", searchContent.Length);

                var writer = indexer.GetIndexWriter(true);

                foreach (var searchContentModel in searchContent)
                {
                    var document = new Document();

                    document.Add(new Field("ID", searchContentModel.ProductID, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("productName", searchContentModel.ProductName, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("vendorItemNumber", searchContentModel.VendorItemNumber, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("customItemNumber", searchContentModel.CustomItemNumber, Field.Store.YES, Field.Index.NOT_ANALYZED));
                    document.Add(new Field("searchText", searchContentModel.SearchText, Field.Store.YES, Field.Index.ANALYZED));

                    writer.AddDocument(document);
                }

                writer.Commit();
            }
        }