Corrected search index bug preventing dynamic items from being indexed if they didn't have a common aspect

- Added a lock on write operations
- Changed Indexing event handler to intercept all ContentPart instances

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-07-29 15:44:15 -07:00
parent 78493d3f75
commit 8d9925a75b
5 changed files with 69 additions and 49 deletions

View File

@@ -114,13 +114,16 @@ namespace Lucene.Services {
} }
public void CreateIndex(string indexName) { public void CreateIndex(string indexName) {
lock ( _appDataFolder ) {
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); var writer = new IndexWriter(GetDirectory(indexName), _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
writer.Close(); writer.Close();
}
Logger.Information("Index [{0}] created", indexName); Logger.Information("Index [{0}] created", indexName);
} }
public void DeleteIndex(string indexName) { public void DeleteIndex(string indexName) {
lock ( _appDataFolder ) {
new DirectoryInfo(_appDataFolder.MapPath(_appDataFolder.Combine(_basePath, indexName))) new DirectoryInfo(_appDataFolder.MapPath(_appDataFolder.Combine(_basePath, indexName)))
.Delete(true); .Delete(true);
@@ -129,6 +132,7 @@ namespace Lucene.Services {
File.Delete(settingsFileName); File.Delete(settingsFileName);
} }
} }
}
public void Store(string indexName, IDocumentIndex indexDocument) { public void Store(string indexName, IDocumentIndex indexDocument) {
Store(indexName, new [] { (LuceneDocumentIndex)indexDocument }); Store(indexName, new [] { (LuceneDocumentIndex)indexDocument });
@@ -146,6 +150,7 @@ namespace Lucene.Services {
// Remove any previous document for these content items // Remove any previous document for these content items
Delete(indexName, indexDocuments.Select(i => i.ContentItemId)); Delete(indexName, indexDocuments.Select(i => i.ContentItemId));
lock ( _appDataFolder ) {
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED); var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
LuceneDocumentIndex current = null; LuceneDocumentIndex current = null;
@@ -167,6 +172,7 @@ namespace Lucene.Services {
writer.Close(); writer.Close();
} }
} }
}
public void Delete(string indexName, int documentId) { public void Delete(string indexName, int documentId) {
Delete(indexName, new[] { documentId }); Delete(indexName, new[] { documentId });
@@ -177,6 +183,8 @@ namespace Lucene.Services {
return; return;
} }
lock ( _appDataFolder ) {
var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED); var writer = new IndexWriter(GetDirectory(indexName), _analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
try { try {
@@ -197,6 +205,7 @@ namespace Lucene.Services {
writer.Close(); writer.Close();
} }
} }
}
public IDocumentIndex New(int documentId) { public IDocumentIndex New(int documentId) {
return new LuceneDocumentIndex(documentId, T); return new LuceneDocumentIndex(documentId, T);

View File

@@ -47,6 +47,7 @@ namespace Lucene.Services {
InitPendingClause(); InitPendingClause();
} }
public ISearchBuilder Parse(string defaultField, string query) { public ISearchBuilder Parse(string defaultField, string query) {
return Parse(new string[] {defaultField}, query); return Parse(new string[] {defaultField}, query);
} }

View File

@@ -1,4 +1,5 @@
using Orchard.ContentManagement.Handlers; using Orchard.ContentManagement.FieldStorage.InfosetStorage;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Core.Common.Models; using Orchard.Core.Common.Models;
using Orchard.Tasks.Indexing; using Orchard.Tasks.Indexing;
@@ -21,16 +22,18 @@ namespace Orchard.Indexing.Handlers {
_indexingTaskManager = indexingTaskManager; _indexingTaskManager = indexingTaskManager;
_indexNotifierHandlers = indexNotifierHandlers; _indexNotifierHandlers = indexNotifierHandlers;
OnPublishing<ContentPart<CommonPartRecord>>(CreateIndexingTask); OnPublishing<ContentPart>(CreateIndexingTask);
OnRemoved<ContentPart<CommonPartRecord>>(RemoveIndexingTask); OnRemoved<ContentPart>(RemoveIndexingTask);
} }
void CreateIndexingTask(PublishContentContext context, ContentPart<CommonPartRecord> part) { void CreateIndexingTask(PublishContentContext context, ContentPart part) {
_indexingTaskManager.CreateUpdateIndexTask(context.ContentItem); _indexingTaskManager.CreateUpdateIndexTask(context.ContentItem);
UpdateIndex();
} }
void RemoveIndexingTask(RemoveContentContext context, ContentPart<CommonPartRecord> part) { void RemoveIndexingTask(RemoveContentContext context, ContentPart part) {
_indexingTaskManager.CreateDeleteIndexTask(context.ContentItem); _indexingTaskManager.CreateDeleteIndexTask(context.ContentItem);
UpdateIndex();
} }
private void UpdateIndex() { private void UpdateIndex() {

View File

@@ -13,15 +13,20 @@ namespace Orchard.Indexing.Handlers {
OnIndexing<InfosetPart>( OnIndexing<InfosetPart>(
(context, cp) => { (context, cp) => {
var infosetPart = context.ContentItem.As<InfosetPart>(); var infosetPart = context.ContentItem.As<InfosetPart>();
if ( infosetPart != null ) { if (infosetPart == null) {
return;
}
// part fields
foreach ( var part in infosetPart.ContentItem.Parts ) { foreach ( var part in infosetPart.ContentItem.Parts ) {
foreach ( var field in part.PartDefinition.Fields ) { foreach ( var field in part.PartDefinition.Fields ) {
if ( field.Settings.GetModel<FieldIndexing>().Included ) { if (!field.Settings.GetModel<FieldIndexing>().Included) {
continue;
}
var fieldName = field.Name; var fieldName = field.Name;
var value = part.Fields.Where(f => f.Name == fieldName).First().Storage.Get<string>(null); var value = part.Fields.Where(f => f.Name == fieldName).First().Storage.Get<string>(null);
context.DocumentIndex.Add(String.Format("{0}-{1}", infosetPart.TypeDefinition.Name, fieldName.ToLower()), value).RemoveTags().Analyze(); context.DocumentIndex.Add(String.Format("{0}-{1}", infosetPart.TypeDefinition.Name.ToLower(), fieldName.ToLower()), value).RemoveTags().Analyze();
}
}
} }
} }
}); });

View File

@@ -103,8 +103,10 @@ namespace Orchard.Indexing.Services {
// nothing to do ? // nothing to do ?
if (taskRecords.Length + updateIndexDocuments.Count == 0) if (taskRecords.Length + updateIndexDocuments.Count == 0) {
Logger.Information("Index update requested, nothing to do");
return; return;
}
Logger.Information("Processing {0} indexing tasks", taskRecords.Length); Logger.Information("Processing {0} indexing tasks", taskRecords.Length);