开发者

Search across fields in Lucene

开发者 https://www.devze.com 2023-01-08 17:04 出处:网络
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.

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.

开发者_JAVA技巧

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");


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);


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号