Lucene - 搜索数值字段
好吧,我在过去的两个小时里搜索了这个,结果只给出了提示, 甚至没有一个完整的代码来救援(如果菜鸟看不到一些样本,他们如何学习?)
我已经创建了一个索引,如下所示:
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);
不返回任何结果。 我读过有关 NumericRange
、KeywordAnalyzer
以及更多内容, 但由于他们都没有提供示例,我不知道该怎么做。
请善良的人们给我一个如何让这件事发挥作用的例子。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 NumericField 和 NumericRangeQuery 来搜索 Lucene 索引中的数字。
创建索引时:
然后使用查询:
将返回 TaxonRankSortOrder 等于 3000 的所有文档。
您必须自己创建查询,而不是使用 QueryParser,因此很想看看是否还有更好的方法。
I have used NumericField and a NumericRangeQuery to search Lucene indexes for numbers.
When creating the index:
And then using a query:
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.
将 ID 字段中的
Field.Index.NO
更改为Field.Index.ANALYZED
(或Field.Index.NOT_ANALYZED
)Change
Field.Index.NO
toField.Index.ANALYZED
(orField.Index.NOT_ANALYZED
) in ID field