开发者

Filter index for searching in Lucene.net

I am currently working on a project involving the Lucene library within C# however I have reached an issue with design of my project concerning the retrevial of documents within the index. The documents within my index have been created with several fields and i'd like to be able to filter between two of these fields and then search this subset for terms however I am still familiarising myself with lucene and am not fully sure if this is possible. I have learnt how to perform basic queries but I think I should be using lucenes filter class but i am not exactly sure how. I would be greatful if anyone could offer advice on this

The project I am completing involves indexing email messages from var开发者_如何学Goious email accounts. the documents in my index have some of the following fields: Account: (e.g. fake@fake.com) Folder: (e.g. sent, trash, inbox...) Data: (the body of the email)

I'd like to be able to filter my index so i can have a subset which only containts documents from a particular account and folder and then after this I'd like to be able to search the data field of this subset.


As @Jf Beaulac suggested, you can do "filtering" with a BooleanQuery.

private Query CreateFilteredQuery (string account, string folder, Query criteria)
{
     BooleanQuery bq = new BooleanQuery();
     bq.Add(new TermQuery (new Lucene.Net.Index.Term ("account", account)), BooleanClause.Occur.MUST);
     bq.Add(new TermQuery (new Lucene.Net.Index.Term ("folder", folder)), BooleanClause.Occur.MUST);
     bq.Add(criteria, BooleanClause.Occur.MUST);
     return bq;
}


Query filteredQuery = CreateFilteredQuery ("fake@fake.com", "inbox", myQueryParser.Parse (criteria));
var hits = myIndexSearcher.Search (filteredQuery);

Here is a good question about the differences between queries and filters: Why do we use Filters while searching

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜