As usual, I call on the massive brain power, which is the Stackoverflow user base, to help solve the Lucene.NET problem I'm struggling with. First, I'm a complete noob when it comes to Lucene and Lucene.NET, and using disparate tutorials and code snippets on the Internet, I have combined the following solution for my scenario.
Scenario
I have an index of the following structure:
---------------------------------------------------------
| id | date | security | text |
---------------------------------------------------------
| 1 | 2011-01-01 | -1-12-4- | some analyzed text here |
---------------------------------------------------------
| 2 | 2011-01-01 | -11-3- | some analyzed text here |
---------------------------------------------------------
| 3 | 2011-01-01 | -1- | some analyzed text here |
---------------------------------------------------------
I need to be able to request a text box, but limit the results to users with certain roleId parameters.
What I came up with for this (after many, many trips to Google) is to use the "security field" and the Lucene filter to limit the result set, as described below:
class SecurityFilter : Lucene.Net.Search.Filter
{
public override System.Collections.BitArray Bits(Lucene.Net.Index.IndexReader indexReader)
{
BitArray bitarray = new BitArray(indexReader.MaxDoc());
for (int i = 0; i < bitarray.Length; i++)
{
if (indexReader.Document(i).Get("security").Contains("-1-"))
{
bitarray.Set(i, true);
}
}
return bitarray;
}
}
... and then...
Lucene.Net.Search.Sort sort = new Lucene.Net.Search.Sort(new Lucene.Net.Search.SortField("date", true));
Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Store.FSDirectory.Open(indexDirectory), true);
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "text", analyzer);
Lucene.Net.Search.Query query = parser.Parse("some search phrase");
SecurityFilter filter = new SecurityFilter();
Lucene.Net.Search.Hits hits = searcher.Search(query, filter, sort);
, , 1 3. , .
, ... - - , , , , , ?