简体   繁体   English

Lucene - 搜索数值字段

[英]Lucene - searching for a numeric value field

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 ).... 现在,我想搜索字段ID其中值等于0 (以获得我在那里的单个记录)....

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. 我已经阅读了NumericRangeKeywordAnalyzer等等,但由于它们都没有提供样本我无法弄清楚如何去做。

please, kind people, give me an example of how to make this thing work. 请亲切的人,给我一个如何使这件事工作的例子。

I have used NumericField and a NumericRangeQuery to search Lucene indexes for numbers. 我使用NumericField和NumericRangeQuery来搜索Lucene索引的数字。

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. 将使用TaxonRankSortOrder等于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. 您必须自己创建查询而不是使用QueryParser,因此我们希望看看是否有更好的方法。

改变Field.Index.NOField.Index.ANALYZED (或Field.Index.NOT_ANALYZED在ID字段)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM