nuclear kote
nuclear kote

Reputation: 500

Lucene doesn't search number fields

I'm trying to index and then search integer field with lucene. But it doesn't find anything (Text fields search well).

Document doc = new Document();    
//UserType = 1
doc.add(new IntField("userType", user.getType().getId(), Field.Store.YES));
FSDirectory dir = FSDirectory.open(FileSystems.getDefault().getPath(indexDir));
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
writer = new IndexWriter(dir, config);
writer.addDocument(doc);

For search I tried to use next queries:

1) new QueryParser(defautField, new StandartAnalyzer()).parse("userType:1");
2) new QueryParser(defautField, new StandartAnalyzer()).parse("userType:[1 TO 1]");
3) new QueryParser(defautField, new StandartAnalyzer()).parse("userType:\"1\"");

But it doesn't work.

Upvotes: 1

Views: 222

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

QueryParser doesn't handle numerics. You can search using NumericRangeQuery:

Query query = NumericRangeQuery.newIntRange("userType", 1, 1, true, true);

Upvotes: 1

Related Questions