Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 435
Rafael Herscovici Avatar asked Oct 23 '11 13:10

Rafael Herscovici


People also ask

How do you search in Lucene?

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries). To perform a single character wildcard search use the "?" symbol. To perform a multiple character wildcard search use the "*" symbol. You can also use the wildcard searches in the middle of a term.

How does Lucene index search work?

Lucene is an inverted full-text index. This means that it takes all the documents, splits them into words, and then builds an index for each word. Since the index is an exact string-match, unordered, it can be extremely fast.

Why is Lucene so fast?

Why is Lucene faster? Lucene is very fast at searching for data because of its inverted index technique. Normally, datasources structure the data as an object or record, which in turn have fields and values.


1 Answers

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.

like image 170
Mike Avatar answered Sep 25 '22 18:09

Mike