This project is read-only.

Problem between IndexEntity and IndexEntities

Jul 27, 2011 at 11:33 AM

Code first:

public IndexResult IndexEntities<TEntity>(IEnumerable<TEntity> entities, IIndexDefinition<TEntity> definition) where TEntity : class {
    return IndexEntities(entities, definition.Convert);
}

public IndexResult IndexEntities<TEntity>(IEnumerable<TEntity> entities, Func<TEntity, Document> converter) {
    var result = new IndexResult();
    result.ExecutionTime = Helpers.TimeFunction(() => {
        foreach (TEntity entity in entities) {
            this.GetWriter().AddDocument(converter(entity)); // Note here
            result.Count++;
        }
        result.Success = true;
    });

    return result;
}

public IndexResult IndexEntity<TEntity>(TEntity entity, IIndexDefinition<TEntity> definition) where TEntity : class {
    var result = new IndexResult();
    result.ExecutionTime = Helpers.TimeFunction(() => {
        this.GetWriter().UpdateDocument(definition.GetIndex(entity), definition.Convert(entity)); // Note here
        result.Count++;
        result.Success = true;
    });

    return result;
}

Note the two comments, it seemd IndexEntities only adds document, but IndexEntity performs a update action on index, this the 2 blocks below produces entirely different behavior:

service.IndexEntities(entities, definition);

foreach (var entity in entities) {
    service.IndexEntity(entity, definition);
}

In my opinion, it's a missleading to user, because from the literal meaning of both method names, IndexEntities is expected as a foreach loop and performs IndexEntity on every entry, thus those who did not read source code would expect IndexEntities to update document, and was encoraged to use it because saving for code lines.

If this behavior is by design, then I strong recommended that the document of both methods giving a clear statement of the difference.

 

SimpleLucene is really great, thanks.

Jul 27, 2011 at 4:36 PM

Yes "IndexEntity" calls "UpdateDocument" which is effectively the equivelant of deleting the document(s) (if it exists) and adding it to the index.

"IndexEntities" would effecitvely allow duplicate entities to be indexed.

I'll consider adding UpdateEntity and UpdateEntities methods . This way people can choose whether they want to just add (allowing duplicates) or replace/update (would remove any duplicates).

 

Thanks for the feedback,

Ben