问题
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.
回答1:
Change Field.Index.NO
to Field.Index.ANALYZED
(or Field.Index.NOT_ANALYZED
) in ID field
回答2:
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.
来源:https://stackoverflow.com/questions/7866376/lucene-searching-for-a-numeric-value-field