我得到的例外:
java.lang.IllegalArgumentException: Cannot create Token Stream from Terms without offsets
我的代码:(创建TokenStream抛出异常)
Highlighter hl = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(titleQuery));
hl.setTextFragmenter(new NullFragmenter());
String text = searcher.doc(scoreDocs[i].doc).get("title");
TokenStream ts = TokenSources.getAnyTokenStream(searcher.getIndexReader(), scoreDocs[i].doc, "title", analyzer);
String frag = hl.getBestFragment(ts, text);
以下是标题字段的编入方式:
FieldType ft = new FieldType();
ft.setIndexed(true);
ft.setStored(true);
ft.setStoreTermVectors(true);
ft.setStoreTermVectorOffsets(true);
ft.setStoreTermVectorPositions(true);
doc.add(new Field("title", title, ft));
这让我感到困惑,因为我显然存储了带有偏移和位置的条款
我试过了ft.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)
,但它似乎没有什么区别。
答案 0 :(得分:0)
您忘了添加setTokenized(true)。如下,它应该工作。
FieldType type = new FieldType();
type.setIndexed(true);
type.setIndexOptions(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
type.setStored(true);
type.setStoreTermVectors(true);
type.setTokenized(true);
type.setStoreTermVectorOffsets(true);
详情请参阅:Highlighting In Lucene