Prevent empty content items to be indexed (menu item)

Added an IsDirty property to IIndexDocument
Once handlers have contributed to index information, if a document is not dirty, don't add it to the index

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-06-08 11:42:32 -07:00
parent abf8357f1b
commit 9574529898
4 changed files with 59 additions and 7 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

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

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; }
}
}