Example #1
0
        public void Store(string indexName, IEnumerable <LuceneDocumentIndex> indexDocuments)
        {
            if (indexDocuments.AsQueryable().Count() == 0)
            {
                return;
            }

            // Remove any previous document for these content items
            Delete(indexName, indexDocuments.Select(i => i.ContentItemId));

            var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
            LuceneDocumentIndex current = null;

            try {
                foreach (var indexDocument in indexDocuments)
                {
                    current = indexDocument;
                    var doc = CreateDocument(indexDocument);

                    writer.AddDocument(doc);
                    Logger.Debug("Document [{0}] indexed", indexDocument.ContentItemId);
                }
            }
            catch (Exception ex) {
                Logger.Error(ex, "An unexpected error occured while add the document [{0}] from the index [{1}].", current.ContentItemId, indexName);
            }
            finally {
                writer.Optimize();
                writer.Close();
            }
        }
Example #2
0
        private static Document CreateDocument(LuceneDocumentIndex indexDocument)
        {
            var doc = new Document();

            indexDocument.PrepareForIndexing();
            foreach (var field in indexDocument.Fields)
            {
                doc.Add(field);
            }
            return(doc);
        }