Is there a Lucene (Lucene.NET) to relational database mapping framework recommended for .NET?
I wanted to use Lucene for search purpose off-loading all search from my relational database.
There are some open source project like SimpleLucene but I haven't used any one of them.
As you said you can think of lucene as a single DB table ( i thought of, in lucene we have document that we can theoretically treat it as a single relational table
). So I don't think that you will need a complex relational database mapping framework
for a single table. Some extension methods like below can make you start to play with Lucene.Net.
public static class LuceneExtension
{
public static void Index(this IndexWriter writer, object obj)
{
Document doc = new Document();
obj.GetType()
.GetProperties()
.Select(p => new { Name = p.Name, Value = p.GetValue(obj, null) })
.ToList()
.ForEach(f=>doc.Add( new Field(f.Name,f.Value.ToString(),Field.Store.YES,Field.Index.ANALYZED) ));
writer.AddDocument(doc);
}
}
For example
indexWriter.Index(new { text = "some text to index" , id = "555" });
would index a document with fields text
and id
You could also check Lucene2Objects hosted in Nuget and with sample introduction article in my blog . Basically allows you to abstract from Lucene and think on objects, you could even annotate your domain entities, like this:
[SearchableEntity(DefaultSearchProperty = "Text")]
public class Message
{
public int Id { get; set; }
[Indexed]
public string Text { get; set; }
[Indexed]
public string Title { get; set; }
public DateTime Sent { get; set; }
public DateTime? Read { get; set; }
}
And then, save like this:
var iWriter = new IndexWriter(Environment.CurrentDirectory + @"\index");
var message = new Message { Id = 12, Sent = DateTime.Now,
Text = "Some text on the message!",
Title = "This is the title"
};
iWriter.AddEntity(message);
iWriter.Close();
And search your index like this
var iReader = new IndexReader(Environment.CurrentDirectory + @"\index");
var messages = iReader.Search<Message>("text");
foreach (var message in messages) {
Console.WriteLine("Message: {0}", message.Title);
}
Sorry if the answer is too long. Hope I can help!
DISCLAIMER: As you may have imagined, I wrote the library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.