1

我正在使用 Lucene.Net MoreLikeThis 功能。我试图弄清楚如何通过 id 过滤 MoreLikeThis 查询。例如,我不希望结果中有任何 ID 为 15 的记录。我该怎么做呢?

4

1 回答 1

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();
于 2011-10-24T07:22:57.413 回答