Lucene 中的跨字段搜索

发布于 2024-09-10 21:05:28 字数 765 浏览 1 评论 0原文

我对 Lucene 完全是个菜鸟,但到目前为止还是一个超级粉丝。

我现在正在寻找一些有关如何通过 C# 和 dotnet 存储数据和搜索的资源。任何 LINQ 示例对我来说都是一个很大的好处。

特别是,如果我的文档有两个定义为标题和描述的字段,我如何在这两个字段中进行搜索?

在下面的示例中,我想搜索标题和描述字段。

例如:

        doc = new Document();
        text = "Oven leek pie";
        doc.Add(new Field("title", text, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("instructions", "Bake for 40 minutes", Field.Store.YES, Field.Index.TOKENIZED));
        iwriter.AddDocument(doc);

然后;

        // Parse a simple query that searches for "text":
        Lucene.Net.QueryParsers.QueryParser parser = new QueryParser("title", analyzer);

        Query query = parser.Parse("baked bacon and leek pizza");

I'm a complete noobie with Lucene and so far a huge, huge fan.

I'm now looking for some resources on how to store data and search through c# and dotnet. Any LINQ samples would be a big bonus to me.

In particular if I have a document that has two fields defined as say title and description, how can i search in both?

in the sample below i'd like to search both title and description fields.

eg:

        doc = new Document();
        text = "Oven leek pie";
        doc.Add(new Field("title", text, Field.Store.YES, Field.Index.TOKENIZED));
        doc.Add(new Field("instructions", "Bake for 40 minutes", Field.Store.YES, Field.Index.TOKENIZED));
        iwriter.AddDocument(doc);

and then;

        // Parse a simple query that searches for "text":
        Lucene.Net.QueryParsers.QueryParser parser = new QueryParser("title", analyzer);

        Query query = parser.Parse("baked bacon and leek pizza");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2024-09-17 21:05:28
string[] fields = new string[2];
fields[0] = "title";
fields[1] = "instructions";

Lucene.Net.QueryParsers.MultiFieldQueryParser multiFieldParser = new MultiFieldQueryParser(fields, analyzer);
Query multiFieldQuery = multiFieldParser.Parse("20");
Hits multiHits = isearcher.Search(multiFieldQuery);
string[] fields = new string[2];
fields[0] = "title";
fields[1] = "instructions";

Lucene.Net.QueryParsers.MultiFieldQueryParser multiFieldParser = new MultiFieldQueryParser(fields, analyzer);
Query multiFieldQuery = multiFieldParser.Parse("20");
Hits multiHits = isearcher.Search(multiFieldQuery);
擦肩而过的背影 2024-09-17 21:05:28

Lucene 中有多种跨字段搜索的方法。 Sam Doshi 在另一个 StackOverflow 问题的回答中描述了一些内容:
如何在 QueryParser 中合并多个字段?

There are many ways to search across fields in Lucene. Sam Doshi describes several in this answer to another StackOverflow question:
How to incorporate multiple fields in QueryParser?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文