Neo4j 3中手动(旧版)索引中的数字排序无法正常工作

时间:2016-05-23 12:39:15

标签: neo4j lucene

我正在使用Legacy索引(现在称为手动索引)。从Neo4j 2迁移到版本3后,我在数字排序方面遇到了一些问题。

Neo4j 2中正确陈述的例子:

queryContext.sort(new Sort(new SortField(AGE, SortField.INT, false)));

对于Neo4j 3(Lucene 5),应该改变这个例子:

queryContext.sort(new Sort(new SortField(AGE, SortField.Type.INT, false)));

但是如果使用这种排序语句,你会得到一个例外:

java.lang.IllegalStateException: unexpected docvalues type SORTED_SET for field 'firstName' (expected=SORTED). Use UninvertingReader or index with docvalues.
at org.apache.lucene.index.DocValues.checkField(DocValues.java:208)
at org.apache.lucene.index.DocValues.getSorted(DocValues.java:264)
at org.apache.lucene.search.FieldComparator$TermOrdValComparator.getSortedDocValues(FieldComparator.java:762)
at org.apache.lucene.search.FieldComparator$TermOrdValComparator.getLeafComparator(FieldComparator.java:767)
at org.apache.lucene.search.FieldValueHitQueue.getComparators(FieldValueHitQueue.java:183)
at org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector.getLeafCollector(TopFieldCollector.java:164)
at org.neo4j.kernel.api.impl.index.collector.DocValuesCollector.replayTo(DocValuesCollector.java:297)
at org.neo4j.kernel.api.impl.index.collector.DocValuesCollector.getTopDocs(DocValuesCollector.java:275)
at org.neo4j.kernel.api.impl.index.collector.DocValuesCollector.getIndexHits(DocValuesCollector.java:150)
at org.neo4j.index.impl.lucene.legacy.LuceneLegacyIndex.search(LuceneLegacyIndex.java:346)
at org.neo4j.index.impl.lucene.legacy.LuceneLegacyIndex.query(LuceneLegacyIndex.java:261)
at org.neo4j.index.impl.lucene.legacy.LuceneLegacyIndex.query(LuceneLegacyIndex.java:205)
at org.neo4j.index.impl.lucene.legacy.LuceneLegacyIndex.query(LuceneLegacyIndex.java:217)
at org.neo4j.kernel.impl.api.StateHandlingStatementOperations.nodeLegacyIndexQuery(StateHandlingStatementOperations.java:1440)
at org.neo4j.kernel.impl.api.OperationsFacade.nodeLegacyIndexQuery(OperationsFacade.java:1162)
at org.neo4j.kernel.impl.coreapi.LegacyIndexProxy$Type$1.query(LegacyIndexProxy.java:83)
at org.neo4j.kernel.impl.coreapi.LegacyIndexProxy.query(LegacyIndexProxy.java:365)

我认为这是由Neo4j索引器类中新添加的语句引起的(Neo4j现在是自动排序的索引字段吗?)。见:

org.neo4j.index.impl.lucene.legacy.IndexType CustomType addToDocument( Document document, String key, Object value )

新行:

document.add( instantiateSortField( key, value ) );

和方法instantiateSortField正在创建SortedSetDocValuesField

所以我将代码更改为:

queryContext.sort(new Sort(new SortedSetSortField(AGE, false)));

这样运行正常,但排序不起作用,因为数字按字符串排序。我看到"价值"每次在方法" addToDocument"中,参数都是String。我认为根本原因可以解释为这个旧评论:

see comment in class org.neo4j.index.impl.lucene.legacy.IndexType CustomType
// TODO We should honor ValueContext instead of doing value.toString() here.
// if changing it, also change #get to honor ValueContext.

我是否缺少一些新方法如何在Neo4j 3中对数据进行索引,搜索和排序?或者这是在Neo4j中将值索引为字符串的问题吗?

Neo4j 2和Neo4j 3的简单单元测试可以是downloaded

1 个答案:

答案 0 :(得分:0)

MishaDemianenko在GH issue

添加的解决方案