简体   繁体   English

Lucene到.NET的关系映射

[英]Lucene to relational mapping for .NET

Is there a Lucene (Lucene.NET) to relational database mapping framework recommended for .NET? 是否建议为.NET使用Lucene(Lucene.NET)到关系数据库映射框架?

I wanted to use Lucene for search purpose off-loading all search from my relational database. 我想将Lucene用于搜索目的,以便从我的关系数据库中卸载所有搜索。

There are some open source project like SimpleLucene but I haven't used any one of them. 有一些开源项目,例如SimpleLucene,但我从未使用过其中任何一个。

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 ). 正如您所说,您可以将lucene视为一个单独的DB表( 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. 因此,我认为您不需要为单个表使用复杂的relational database mapping framework Some extension methods like below can make you start to play with Lucene.Net. 像下面这样的扩展方法,可以让您开始使用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 将使用字段textid索引文档

You could also check Lucene2Objects hosted in Nuget and with sample introduction article in my blog . 您还可以查看Nuget中托管的Lucene2Objects以及我的博客中的示例介绍文章。 Basically allows you to abstract from Lucene and think on objects, you could even annotate your domain entities, like this: 基本上,您可以从Lucene中抽象出来并思考对象,甚至可以对域实体进行注释,如下所示:

[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. 免责声明:正如您可能想象的那样,我编写了该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM