2011-10-24 93 views
1

我正在使用Lucene.Net MoreLikeThis功能。我想弄清楚如何通過一個id過濾一個MoreLikeThis Query。例如,我不希望結果中有任何ID爲15的記錄。我該怎麼做呢?如何按ID篩選

回答

1
RAMDirectory dir = new RAMDirectory(); 
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 
for (int i = 0; i < 20; i++) 
{ 
    Document doc = new Document(); 
    doc.Add(new Field("field1", "some text " + i.ToString(), Field.Store.YES, Field.Index.ANALYZED)); 
    doc.Add(new Field("ID", i.ToString(), Field.Store.YES, Field.Index.ANALYZED)); 
    writer.AddDocument(doc); 
} 
writer.Close(); 

IndexReader reader = IndexReader.Open(dir); 

Lucene.Net.Search.Similar.MoreLikeThisQuery mltq = new Lucene.Net.Search.Similar.MoreLikeThisQuery("text", new string[] { "field1" }, new WhitespaceAnalyzer()); 

BooleanQuery bq = new BooleanQuery(); 
bq.Add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); 
bq.Add(new TermQuery(new Term("ID","15")),BooleanClause.Occur.MUST_NOT); 
Filter filter = new CachingWrapperFilter(new QueryWrapperFilter(bq)); 

TopDocs td = new IndexSearcher(reader).Search(mltq, filter, 100); 
Debug.Assert(td.TotalHits == 19); 

reader.Close();