--HG--
branch : dev
This commit is contained in:
Louis DeJardin
2010-06-08 12:06:47 -07:00
7 changed files with 81 additions and 9 deletions

View File

@@ -216,5 +216,38 @@ namespace Orchard.Tests.Indexing {
_provider.Store("default", _provider.New(1).Add("body", null));
Assert.That(_provider.IsEmpty("default"), Is.False);
}
[Test]
public void IsDirtyShouldBeFalseForNewDocuments() {
IIndexDocument doc = _provider.New(1);
Assert.That(doc.IsDirty, Is.False);
}
[Test]
public void IsDirtyShouldBeTrueWhenIndexIsModified() {
IIndexDocument doc;
doc = _provider.New(1);
doc.Add("foo", "value");
Assert.That(doc.IsDirty, Is.True);
doc = _provider.New(1);
doc.Add("foo", false);
Assert.That(doc.IsDirty, Is.True);
doc = _provider.New(1);
doc.Add("foo", (float)1.0);
Assert.That(doc.IsDirty, Is.True);
doc = _provider.New(1);
doc.Add("foo", 1);
Assert.That(doc.IsDirty, Is.True);
doc = _provider.New(1);
doc.Add("foo", DateTime.Now);
Assert.That(doc.IsDirty, Is.True);
}
}
}

View File

@@ -12,13 +12,17 @@ namespace Orchard.Core.Indexing.Lucene {
public List<AbstractField> Fields { get; private set; }
private AbstractField _previousField;
public int Id { get; private set; }
public DefaultIndexDocument(int documentId) {
Fields = new List<AbstractField>();
SetContentItemId(documentId);
IsDirty = false;
}
public bool IsDirty { get; private set; }
public IIndexDocument Add(string name, string value) {
return Add(name, value, false);
}
@@ -35,36 +39,42 @@ namespace Orchard.Core.Indexing.Lucene {
}
_previousField = new Field(name, value, Field.Store.YES, Field.Index.ANALYZED);
IsDirty = true;
return this;
}
public IIndexDocument Add(string name, DateTime value) {
AppendPreviousField();
_previousField = new Field(name, DateTools.DateToString(value, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.NOT_ANALYZED);
IsDirty = true;
return this;
}
public IIndexDocument Add(string name, int value) {
AppendPreviousField();
_previousField = new NumericField(name, Field.Store.YES, true).SetIntValue(value);
IsDirty = true;
return this;
}
public IIndexDocument Add(string name, bool value) {
AppendPreviousField();
_previousField = new Field(name, value.ToString().ToLower(), Field.Store.YES, Field.Index.NOT_ANALYZED);
IsDirty = true;
return this;
}
public IIndexDocument Add(string name, float value) {
AppendPreviousField();
_previousField = new NumericField(name, Field.Store.YES, true).SetFloatValue(value);
IsDirty = true;
return this;
}
public IIndexDocument Add(string name, object value) {
AppendPreviousField();
_previousField = new Field(name, value.ToString(), Field.Store.NO, Field.Index.NOT_ANALYZED);
IsDirty = true;
return this;
}

View File

@@ -155,7 +155,16 @@ namespace Orchard.Core.Indexing.Lucene {
public IEnumerable<ISearchHit> Search() {
var query = CreateQuery();
var searcher = new IndexSearcher(_directory, true);
IndexSearcher searcher;
try {
searcher = new IndexSearcher(_directory, true);
}
catch {
// index might not exist if it has been rebuilt
Logger.Information("Attempt to read a none existing index");
return Enumerable.Empty<ISearchHit>();
}
try {
var sort = String.IsNullOrEmpty(_sort)
@@ -188,8 +197,17 @@ namespace Orchard.Core.Indexing.Lucene {
public int Count() {
var query = CreateQuery();
IndexSearcher searcher;
try {
searcher = new IndexSearcher(_directory, true);
}
catch {
// index might not exist if it has been rebuilt
Logger.Information("Attempt to read a none existing index");
return 0;
}
var searcher = new IndexSearcher(_directory, true);
try {
var hits = searcher.Search(query, Int16.MaxValue);
Logger.Information("Search results: {0}", hits.scoreDocs.Length);

View File

@@ -91,12 +91,14 @@ namespace Orchard.Core.Indexing.Services {
handler.Indexing(context);
}
if ( context.IndexDocument.IsDirty ) {
updateIndexDocuments.Add(context.IndexDocument);
foreach (var handler in _handlers) {
foreach ( var handler in _handlers ) {
handler.Indexed(context);
}
}
}
catch (Exception ex) {
Logger.Warning(ex, "Unable to index content item #{0} during rebuild", contentItem.Id);
}
@@ -146,12 +148,15 @@ namespace Orchard.Core.Indexing.Services {
handler.Indexing(context);
}
if ( context.IndexDocument.IsDirty ) {
updateIndexDocuments.Add(context.IndexDocument);
foreach (var handler in _handlers) {
handler.Indexed(context);
}
}
}
catch (Exception ex) {
Logger.Warning(ex, "Unable to process indexing task #{0}", taskRecord.Id);
}

View File

@@ -43,6 +43,7 @@ namespace Orchard.Search.Controllers {
return new HttpUnauthorizedResult();
_searchService.RebuildIndex();
_searchService.UpdateIndex();
return RedirectToAction("Index");
}

View File

@@ -63,6 +63,7 @@ namespace Orchard.Search.Services
searchProvider.DeleteIndex(SearchIndexName);
searchProvider.CreateIndex(SearchIndexName); // or just reset the updated date and let the background process recreate the index
Services.Notifier.Information(T("The search index has been rebuilt."));
}

View File

@@ -24,6 +24,10 @@ namespace Orchard.Indexing {
/// </summary>
IIndexDocument Analyze(bool analyze);
/// <summary>
/// Whether some property have been added to this document, or otherwise if it's empty
/// </summary>
bool IsDirty { get; }
}
}