Lucene - 搜索数值字段

发布于 2024-12-11 08:12:24 字数 1395 浏览 0 评论 0原文

好吧,我在过去的两个小时里搜索了这个,结果只给出了提示, 甚至没有一个完整的代码来救援(如果菜鸟看不到一些样本,他们如何学习?)

我已经创建了一个索引,如下所示:

Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/data/channels/")));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
IndexWriter writer = new IndexWriter(directory, analyzer, true, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.Add(new Field("ID", "0", Field.Store.YES, Field.Index.NO));
doc.Add(new Field("parentID", "0", Field.Store.YES, Field.Index.NO));
doc.Add(new Field("Title", "Root", Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
writer.Optimize();
writer.Close();

现在,我想搜索字段 ID 值所在的位置等于0(获取我在那里的单个记录)......

但是,像这样的简单搜索:

Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/data/channels")));
Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_29);
Searcher searcher = new Lucene.Net.Search.IndexSearcher(IndexReader.Open(directory, true));
Query query = new Lucene.Net.QueryParsers.QueryParser(Version.LUCENE_29, "ID", analyzer).Parse("0");
Hits hits = searcher.Search(query);

不返回任何结果。 我读过有关 NumericRangeKeywordAnalyzer 以及更多内容, 但由于他们都没有提供示例,我不知道该怎么做。

请善良的人们给我一个如何让这件事发挥作用的例子。

ok, i have searched for this in the past two hours with results that only give's tips,
and not even one complete code to the rescue ( how would noobs learn if they cant see some samples ? )

i have created an index like so:

Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/data/channels/")));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
IndexWriter writer = new IndexWriter(directory, analyzer, true, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.Add(new Field("ID", "0", Field.Store.YES, Field.Index.NO));
doc.Add(new Field("parentID", "0", Field.Store.YES, Field.Index.NO));
doc.Add(new Field("Title", "Root", Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
writer.Optimize();
writer.Close();

Now, i want to search for the field ID Where the value equals 0 ( to get the single record i have there )....

but, a simple search like this:

Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(Server.MapPath("/data/channels")));
Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Version.LUCENE_29);
Searcher searcher = new Lucene.Net.Search.IndexSearcher(IndexReader.Open(directory, true));
Query query = new Lucene.Net.QueryParsers.QueryParser(Version.LUCENE_29, "ID", analyzer).Parse("0");
Hits hits = searcher.Search(query);

returns no results.
i have read about NumericRange, KeywordAnalyzer and some more things,
but since none of them provides a sample i could not figure out how to do it.

please, kind people, give me an example of how to make this thing work.

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

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

发布评论

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

评论(2

暖阳 2024-12-18 08:12:24

我使用 NumericField 和 NumericRangeQuery 来搜索 Lucene 索引中的数字。

创建索引时:

  NumericField taxonRankSortOrder = new NumericField("TaxonRankSortOrder", Field.Store.YES, true);
  taxonRankSortOrder.SetIntValue(rank);
  document.Add(taxonRankSortOrder);

然后使用查询:

  NumericRangeQuery query = NumericRangeQuery.NewIntRange("TaxonRankSortOrder", 3000, 3000, true, true);

将返回 TaxonRankSortOrder 等于 3000 的所有文档。

您必须自己创建查询,而不是使用 QueryParser,因此很想看看是否还有更好的方法。

I have used NumericField and a NumericRangeQuery to search Lucene indexes for numbers.

When creating the index:

  NumericField taxonRankSortOrder = new NumericField("TaxonRankSortOrder", Field.Store.YES, true);
  taxonRankSortOrder.SetIntValue(rank);
  document.Add(taxonRankSortOrder);

And then using a query:

  NumericRangeQuery query = NumericRangeQuery.NewIntRange("TaxonRankSortOrder", 3000, 3000, true, true);

Will return all documents with a TaxonRankSortOrder equal to 3000.

You have to create the query yourself rather than using a QueryParser so would be keen to see if there is a better approach as well.

会傲 2024-12-18 08:12:24

将 ID 字段中的 Field.Index.NO 更改为 Field.Index.ANALYZED(或 Field.Index.NOT_ANALYZED

Change Field.Index.NO to Field.Index.ANALYZED (or Field.Index.NOT_ANALYZED) in ID field

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文