CrescentHighlighter { public String getBestFragment(CrescentCollectionField field, String value, Query query, Analyzer analyzer) throws CrescentInvalidRequestException { String fragment = ""; logger.debug("fieldName : {}", field.getName()); try { logger.debug("query for highlighter : {}" , query); QueryScorer scorer = new QueryScorer(query); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.setTextFragmenter(new SimpleFragmenter(50)); TokenStream stream = analyzer.tokenStream(field.getName(), new StringReader(value)); fragment = highlighter.getBestFragments(stream, value, 1, "..."); if(fragment == null || "".equals(fragment)) { if(value.length() > 100) { fragment = value.substring(0,100); } else { fragment = value; } } return fragment; } catch (IOException e) { logger.error("error in crescent highlighter", e); return value; } catch (InvalidTokenOffsetsException e) { logger.error("error in crescent highlighter", e); return value; } }  String getBestFragment(CrescentCollectionField field, String value, Query query, Analyzer analyzer);  }
@Test public void fastVectorTest() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.setKeyword("입니다"); searchRequest.setCollectionName("sample"); searchRequest.setSearchField("title,dscr"); SearcherManager searcherManager = crescentSearcherManager.getSearcherManager("sample"); IndexSearcher indexSearcher = searcherManager.acquire(); IndexReader indexReader = indexSearcher.getIndexReader(); TopDocs topDocs = indexSearcher.search(new TermQuery(new Term("dscr", "입니다")), 3); System.out.println("ddd : " + indexReader.document(0)); System.out.println(topDocs.totalHits); FastVectorHighlighter highlighter = new FastVectorHighlighter(); System.out.println("field query : " + highlighter.getFieldQuery(new TermQuery(new Term("dscr", "입니다")))); String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("dscr", "텍스트"))), indexReader, topDocs.scoreDocs[0].doc, "dscr", 30); System.out.println(fragment); }
@Test public void testVectorHighlighter() throws Exception { Directory dir = new RAMDirectory(); IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_44, new WhitespaceAnalyzer(Version.LUCENE_44))); Document doc = new Document(); FieldType fieldType = new FieldType(); fieldType.setIndexed(true); fieldType.setStored(true); fieldType.setTokenized(true); fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); fieldType.setStoreTermVectors(true); Field f1 = new Field("_id", "1", fieldType); Field f2 = new Field("content", "the big 삼성전자연구원. dog", fieldType); doc.add(f1); doc.add(f2); indexWriter.addDocument(doc); indexWriter.commit(); indexWriter.close(); DirectoryReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1); System.out.println(topDocs.totalHits); FastVectorHighlighter highlighter = new FastVectorHighlighter(); String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "big"))), reader, topDocs.scoreDocs[0].doc, "content", 200); System.out.println(fragment); }