I'm having trouble sorting by string fields in Lucene 5.0. Obviously, the way you can sort it, since Lucene 4 has changed. The following is a snippet of some of the fields that are indexes for my documents.
@Override
public Document generateDocument(Process entity)
{
Document doc = new Document();
doc.add(new IntField(id, entity.getID(), Field.Store.YES));
doc.add(new TextField(title, entity.getProcessName(), Field.Store.YES));
doc.add(new IntField(organizationID, entity.getOrganizationID(), Field.Store.YES));
doc.add(new StringField(versionDate, DateTools.dateToString(entity.getVersionDate(), DateTools.Resolution.SECOND), Field.Store.YES));
doc.add(new LongField(entityDate, entity.getVersionDate().getTime(), Field.Store.YES));
return doc;
}
First, I would like to talk about relevancy, which works great. The problem is that sorting by header field does not work. I created a sortfield field that I am trying to use with TopFieldCollector after a chain of method calls.
public BaseSearchCore<Process, ProcessSearchResultScore>.SearchContainer search(String searchQuery, Filter filter, int page, int hitsPerPage) throws IOException, ParseException
{
SortField titleSort = new SortField(title, SortField.Type.STRING, true);
return super.search(searchQuery, filter, page, hitsPerPage, title);
}
What's happening:
public SearchContainer search(String searchQuery, Filter filter, int page, int hitsPerPage, SortField... sortfields) throws IOException, ParseException
{
Query query = getQuery(searchQuery);
TopFieldCollector paginate = getCollector(sortfields);
int startIndex = (page -1) * hitsPerPage;
ScoreDoc[] hits = executeSearch(query, paginate, filter, startIndex, hitsPerPage);
return collectResults(query, filter, hitsPerPage, hits, page);
}
And finally, to the method that applies the sort field:
private TopFieldCollector getCollector(SortField sortfield) throws IOException
{
SortField[] sortFields = new SortField[] {SortField.FIELD_SCORE, sortField};
Sort sorter = new Sort(sortFields);
TopFieldCollector collector = TopFieldCollector.create(sorter, 25000, true, false, true);
return collector;
}
Using the returned collector, a regular query is executed and the result is returned. However, if I try to sort with this SortField, I will get this exception:
java.lang.IllegalStateException: docvalues NONE 'title' (expected = SORTED). UninvertingReader docvalues.
, ( ) Lucene 5? .
, , . , Lucene.