简体   繁体   中英

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 various email accounts. the documents in my index have some of the following fields: Account: (eg fake@fake.com) Folder: (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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