- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Lucene 5.2.1 内置的搜索引擎,但我在搜索的排序更新选项方面遇到了问题。使用排序选项搜索时出现错误:
Exception in thread "main" java.lang.IllegalStateException: unexpected docvalues type NONE for field 'stars' (expected=NUMERIC). Use UninvertingReader or index with docvalues.
at org.apache.lucene.index.DocValues.checkField(DocValues.java:208)
at org.apache.lucene.index.DocValues.getNumeric(DocValues.java:227)
at org.apache.lucene.search.FieldComparator$NumericComparator.getNumericDocValues(FieldComparator.java:167)
at org.apache.lucene.search.FieldComparator$NumericComparator.doSetNextReader(FieldComparator.java:153)
at org.apache.lucene.search.SimpleFieldComparator.getLeafComparator(SimpleFieldComparator.java:36)
at org.apache.lucene.search.FieldValueHitQueue.getComparators(FieldValueHitQueue.java:183)
at org.apache.lucene.search.TopFieldCollector$NonScoringCollector.getLeafCollector(TopFieldCollector.java:141)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:762)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:485)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:694)
at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:679)
at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:621)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:527)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:577)
at SearchEngine.searchBusinessByCategory(SearchEngine.java:145)
at Tests.searchEngineTest(Tests.java:137)
at Main.main(Main.java:13)
我已经索引了一个包含双字段“stars”的文档,我需要使用它来对搜索结果进行排序(具有最高“stars”值的文档需要在顶部)。
Document doc = new Document();
doc.add(new DoubleField("stars", stars, Store.YES));
然后我在那个“星星”字段上实现了一个带有排序选项的搜索命令,如下所示:
SortField sortfield = new SortField("stars", SortField.Type.DOUBLE, true);
Sort sort = new Sort(sortfield);
mySearcher.search(query, maxdocs, sort);
我在网上找到了类似的讨论,但他们都在谈论 SolR(有时是 Elastic Search),但他们既没有提供 Lucene 片段,也没有为 Lucene 5 提供更通用的实现解决方案策略。例如:
Solr uses DocValues and falls back to wrapping with UninvertingReader, if user have not indexed them (with negative startup performance and memory effects). But in general, you should really enable DocValues for fields you want to sort on. (from http://grokbase.com/t/lucene/java-user/152q2jcgzz/lucene-4-x-5-illegalstateexception-while-sorting)
我发现我必须对 UninvertingReader 或 DocValues 做些什么。但是什么?
最佳答案
您需要定义自定义 FieldType
,因为您使用的 DoubleField 构造函数不存储文档值。
例如:
private static final FieldType DOUBLE_FIELD_TYPE_STORED_SORTED = new FieldType();
static {
DOUBLE_FIELD_TYPE_STORED_SORTED.setTokenized(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setOmitNorms(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setIndexOptions(IndexOptions.DOCS);
DOUBLE_FIELD_TYPE_STORED_SORTED
.setNumericType(FieldType.NumericType.DOUBLE);
DOUBLE_FIELD_TYPE_STORED_SORTED.setStored(true);
DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
DOUBLE_FIELD_TYPE_STORED_SORTED.freeze();
}
并使用以下方式添加到您的文档中:
Document doc = new Document();
doc.add(new DoubleField("stars", stars, DOUBLE_FIELD_TYPE_STORED_SORTED));
当你使用
new DoubleField("stars", stars, Stored.YES);
您实际上正在使用此 FieldType
:
public static final FieldType TYPE_STORED = new FieldType();
static {
TYPE_STORED.setTokenized(true);
TYPE_STORED.setOmitNorms(true);
TYPE_STORED.setIndexOptions(IndexOptions.DOCS);
TYPE_STORED.setNumericType(FieldType.NumericType.DOUBLE);
TYPE_STORED.setStored(true);
TYPE_STORED.freeze();
}
没有 DocValues。
关于java - Lucene 5 排序问题(UninvertedReader 和 DocValues),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451111/
我正在使用 Lucene 5.2.1 内置的搜索引擎,但我在搜索的排序更新选项方面遇到了问题。使用排序选项搜索时出现错误: Exception in thread "main" java.lang.I
我是一名优秀的程序员,十分优秀!