如何按 ID 过滤

发布于 2024-12-11 15:55:04 字数 106 浏览 0 评论 0原文

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

I am using Lucene.Net MoreLikeThis feature. I am trying to figure out how to filter a MoreLikeThis Query by an id. For example, i do not want any records with an ID of 15 in the results. How do i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-12-18 15:55:04
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文