Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene Java Sorting on TextField - Alphabetically

I am adding a field to a Document as such:

doc.add(new TextField("productName", prod.getProductName(), Field.Store.YES));

But when I do a search and try to sort, I do not get things in alphabetical order:

Sort sorter = new Sort(); 
SortField sortField = new SortField("productName", Type.STRING, false);
sorter.setSort(sortField);

Am I doing something wrong? How do I sort alphabetically?

like image 306
SoluableNonagon Avatar asked Dec 15 '22 00:12

SoluableNonagon


1 Answers

SortFields should not be tokenized. If you need to have a field tokenized for searching effectively, and also sort on it, you create a separate, untokenized field (StringField) on the same content, and sort on that one, like:

doc.add(new TextField("productName", prod.getProductName(), Field.Store.YES));
doc.add(new StringField("productNameSort", prod.getProductName(), Field.Store.NO));
like image 173
femtoRgon Avatar answered Dec 27 '22 17:12

femtoRgon