mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-19 18:27:55 +08:00
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:
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
|
@@ -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; }
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user