Lucene 5排序问题(UninvertedReader和DocValues) [英] Lucene 5 Sort problems (UninvertedReader and DocValues)
问题描述
我正在使用Lucene 5.2.1内置的搜索引擎,但我在搜索排序更新选项时遇到问题。使用Sort选项搜索时出错:
I am working on a Search Engine built in Lucene 5.2.1, but I am having trouble with Sort updated option for Search. I get an error while searching with the Sort option:
线程main中的异常java.lang.IllegalStateException:意外的docvalues类型为NONE字段'明星'(预期= NUMERIC)。使用UninvertingReader或索引与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)
在org.a上
pache.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)
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:577)
在SearchEngine.searchBusinessByCategory(SearchEngine.java:145)
at Tests.searchEngineTest( Tests.java:137)
Main.main的
(Main.java:13)
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)
我已编入索引包含双字段星号的文档,我需要使用它来对搜索结果进行排序(具有最高星号值的文档需要在顶部)。
I have indexed a document which contains a double field "stars" that I need to use to sort search results (the document with highest "stars" value needs to be on top).
Document doc = new Document();
doc.add(new DoubleField("stars", stars, Store.YES));
然后我在星星字段上实现了带有排序选项的搜索命令,如下所示:
Then I implemented a Search command with Sort option on that "stars" field as follows:
SortField sortfield = new SortField("stars", SortField.Type.DOUBLE, true);
Sort sort = new Sort(sortfield);
mySearcher.search(query, maxdocs, sort);
我在网上发现了类似的讨论,但他们都谈论SolR(或有时是弹性搜索),但他们不讨论不为Lucene 5提供Lucene片段或更一般的实施解决方案策略。例如:
I have found similar discussions online but they all talk about SolR (or sometimes Elastic Search) but they don't provide neither a Lucene snippet nor a more general implementative solution strategy for Lucene 5. For example:
Solr使用DocValues并回退到使用UninvertingReader包装,如果用户没有为它们编制索引,则为
(具有负启动性能和
内存效果)。但一般来说,您应该为要排序的字段启用DocValues
。
(来自h ttp://grokbase.com/t/lucene/java-user/152q2jcgzz/lucene-4-x-5-illegalstateexception-while-sorting )
我看到我必须对UninvertingReader或DocValues做些什么。但是什么?
I see that I have to do something with UninvertingReader or DocValues. But what?
推荐答案
你需要定义一个自定义 FieldType
,因为您使用的DoubleField构造函数不存储docvalues。
You need to define a custom FieldType
since the DoubleField constructor you are using does not store docvalues.
Ex:
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();
}
并使用以下内容添加到您的文档:
And add to your doc using:
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。
which does not have DocValues.
这篇关于Lucene 5排序问题(UninvertedReader和DocValues)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!