lucene-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: indexSearcher using NumericRangeQuery doesn't gives result. Any help ?
Date Thu, 08 Dec 2011 08:31:34 GMT
Hi,

> I am new to lucene library. I need to write some numeric field in the doc
using
> indexwriter and using index searcher i need to search if the value range
(age >
> 40) as example.
> 
> IndexWriter Snippet below. The value is available in the doc properly.
> (Document<indexed,tokenized,omitNorms,omitTermFreqAndPositions&lt;title:4
> 0>>)
>                 Document doc = new Document();
> 		NumericField numericField = new NumericField("title",
> 				Integer.parseInt(value));
> 		numericField.setIntValue(Integer.parseInt(value));
> 		doc.add(numericField);
> 		iw.addDocument(doc);
>                iw.optimize();
> 		iw.close();

This code is wrong. The 2nd value passed to NumericFields ctor is a
configuration constant and not the value. Just leave the default, so use
'new NumericField("title")' - otherwise RTFM.

> indexSearcher snippet below. IndexSearcher prints the hits length
correctly.
> Having said the result is not getting printed from the doc. Any help ? I
spend so
> much time and failed to find.
> 
> Query queryParser = NumericRangeQuery.newIntRange("title", 40, 6000, true,
> true);

This one is correct and uses the default precisionStep. Because of the
mismatch between precision steps on NumericField and NumericRangeQuery, no
results are returned. For simple use cases it's better to use the default
precisionStep (so don't pass it to neither NRQ nor NF).

> 		// 3. Search
> 		int hitsPerPage = 10;
> 		IndexSearcher indexSearcher = new IndexSearcher(index,
true);
> 		TopScoreDocCollector collector =
> TopScoreDocCollector.create(
> 				hitsPerPage, true);
> 		indexSearcher.search(queryParser, collector);
> 
> 		ScoreDoc[] hits = collector.topDocs().scoreDocs;
> 
> 		// 4. Display result
> 		log.info("List of docs found : " + hits.length);
> 		for (int i = 0; i < hits.length; i++) {
> 			int docId = hits[i].doc;
> 			System.out.println(docId);
> 			Document doc = indexSearcher.doc(docId);
> 			log.info(i + 1 + " . " + doc.get("title"));
> 		}


Uwe


Mime
View raw message