如何在Lucene.Net.Linq中添加文档? [英] How to add documents in Lucene.Net.Linq?

查看:81
本文介绍了如何在Lucene.Net.Linq中添加文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lucene.Net.Linq 项目看起来非常强大,而查询看起来很简单,我不太确定如何添加/更新文档.可以提供一个或两个示例吗?

The Lucene.Net.Linq project seems pretty powerful and while querying seems pretty simple, I'm not quite sure how to add/update documents. Can an example or two be provided?

推荐答案

测试项目中的一些完整示例位于

There are some full examples in the test project at https://github.com/themotleyfool/Lucene.Net.Linq/tree/master/source/Lucene.Net.Linq.Tests/Samples.

配置了映射并初始化了提供程序之后,您可以通过打开会话进行更新:

Once you've configured your mappings and initialized your provider, you make updates by opening a session:

var directory = new RAMDirectory();

var provider = new LuceneDataProvider(directory, Version.LUCENE_30);

using (var session = provider.OpenSession<Article>())
{
    session.Add(new Article {Author = "John Doe", BodyText = "some body text", PublishDate = DateTimeOffset.UtcNow});
}

您还可以更新现有文档.只需从会话中检索项目,会话将检测是否进行了修改:

You can also update existing documents. Simply retrieve the item from the session, and the session will detect if a modification was made:

using (var session = provider.OpenSession<Article>())
{
    var item = session.Query().Single(i => i.Id == someId);
    item.Name = "updated";
}

或者您可以删除文档:

using (var session = provider.OpenSession<Article>())
{
    var item = session.Query().Single(i => i.Id == someId);
    session.Delete(item);
}

处置会话后,会话中所有未决的更改都将写入索引,然后提交.这是在同步上下文中完成的,以确保会话中的所有更改都已提交,并且在其他线程上执行查询时可以自动看到.

When the session is disposed, all pending changes in the session are written to the index and then committed. This is done within a synchronization context to ensure all changes in the session are committed and seen atomically when queries are being executed on other threads.

这篇关于如何在Lucene.Net.Linq中添加文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆