Monday, June 6, 2011

Lucene Search on Numeric Long field

Something new to me, I have previously enabled Lucene search using pure text fields, but stumbled recently when trying to search using a Long field:
IndexWriterConfig indexConfig = new IndexWriterConfig(Version.LUCENE_30,  new StandardAnalyzer(Version.LUCENE_30));
IndexWriter indexWriter = new IndexWriter(directory, indexConfig );

Document doc = new Document();
doc.add(new NumericField("id", Store.YES, true).setLongValue(123L));
and to search on this field:
IndexSearcher is = new IndexSearcher(dir);
Query query = new TermQuery(new Term("id", NumericUtils.longToPrefixCoded(123L)));
TopDocs hits =, 10);

No comments:

Post a Comment