I'm having issues sorting on string fields in Lucene 5.0. Apparantly the way you could sort since Lucene 4 has changed. Below shows a snippet of some of the fields that are being index 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;
}
I would like to sort on relevance first, which works just fine. The issue I have is that sorting on the title field doesn't work. I've created a sortfield which i'm trying to use with a 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);
}
Which goes to:
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 performed, and a result is returned. However, if I try to sort with this SortField i'll get this exception:
java.lang.IllegalStateException: unexpected docvalues type NONE for field 'title' (expected=SORTED). Use UninvertingReader or index with docvalues.
How am I supposed to index a string field to be able to sort it alphabetically(using sortfields) in Lucene 5? Any code examples or snippets would be much appriciated.
Searching by relevancy works just fine, but when users enter empty search queries all the results have the same relevancy. With those queries I'd rather sort by the results titles, which is causing issues in this iteration of Lucene.
Aucun commentaire:
Enregistrer un commentaire