0
回答
Lucene 5 Sort
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

最近闲来无事,在研究Lucene 5,遇到了一个搜索时排序的问题。

我调用search(query, n, sort)方法进行搜索,结果得到了一个异常:

java.lang.IllegalStateException: unexpected docvalues type NONE for field 'name' (expected one of [BINARY, SORTED]). Use UninvertingReader or index with docvalues.
	at org.apache.lucene.index.DocValues.checkField(DocValues.java:208)
	at org.apache.lucene.index.DocValues.getBinary(DocValues.java:247)
	at org.apache.lucene.search.FieldComparator$TermValComparator.getBinaryDocValues(FieldComparator.java:929)
	at org.apache.lucene.search.FieldComparator$TermValComparator.getLeafComparator(FieldComparator.java:948)
	at org.apache.lucene.search.FieldValueHitQueue.getComparators(FieldValueHitQueue.java:183)
	at org.apache.lucene.search.TopFieldCollector$SimpleFieldCollector.getLeafCollector(TopFieldCollector.java:164)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:812)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:535)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:744)
	at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:729)
	at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:671)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:577)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:627)
	at lucene.fourth.SortTest.orderByString(SortTest.java:110)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)



我的代码是这样的:

StandardAnalyzer analyzer = new StandardAnalyzer();
Directory directory = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
Document doc = new Document();
StringField nameField = new StringField("name", "", Store.YES);
String[] names = {"C", "A", "B"};
for (String name : names) {
	nameField.setStringValue(name);
	doc.removeField("name");
	doc.add(nameField);
	indexWriter.addDocument(doc);
}
indexWriter.close();
		
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new WildcardQuery(new Term("name", "*"));
SortField field = new SortField("name", Type.STRING_VAL);
Sort sort = new Sort(field);
TopDocs topDocs = searcher.search(query, 100, sort);




<无标签>
举报
Leech
发帖于1年前 0回/61阅
顶部