我在Lucene 5.0中遇到了对字符串字段进行排序的问题。显然,自从Lucene 4以来,排序的方式已经改变。下面展示了一些为我的文档索引的字段片段。
@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;
}
我希望首先按相关性进行排序,这部分已经很好地实现了。但是我遇到的问题是按标题字段排序无法正常工作。我创建了一个排序字段,然后在一系列方法调用之后尝试使用TopFieldCollector。
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);
}
这将会导致:
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);
}
最后是应用排序字段的方法:
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;
}
使用返回的收集器执行常规查询,并返回结果。但是,如果我尝试使用此SortField进行排序,将会出现以下异常:
java.lang.IllegalStateException:字段“title”的docvalues类型为NONE(期望=SORTED)。使用UninvertingReader或具有docvalues的索引。
我应该如何索引字符串字段以便能够按字母顺序对其进行排序(使用sortfields)在Lucene 5中?任何代码示例或片段都将非常赞赏。
通过相关性搜索工作正常,但当用户输入空搜索查询时,所有结果的相关性相同。对于这些查询,我宁愿按结果标题排序,在这个Lucene迭代中会出现问题。
FieldType
中。(https://lucene.apache.org/core/5_3_1/core/org/apache/lucene/document/FieldType.html)。 - femtoRgon