简体   繁体   中英

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 )....

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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