1 4 package org.javabb.lucene.search; 5 6 7 import java.io.IOException ; 8 import java.io.StringReader ; 9 10 import org.apache.lucene.analysis.Analyzer; 11 import org.apache.lucene.analysis.TokenStream; 12 import org.apache.lucene.document.Document; 13 import org.apache.lucene.document.Field; 14 import org.apache.lucene.index.IndexReader; 15 import org.apache.lucene.index.IndexWriter; 16 import org.apache.lucene.queryParser.QueryParser; 17 import org.apache.lucene.search.Query; 18 import org.apache.lucene.search.highlight.Formatter; 19 import org.apache.lucene.search.highlight.Highlighter; 20 import org.apache.lucene.search.highlight.QueryScorer; 21 import org.apache.lucene.search.highlight.Scorer; 22 import org.apache.lucene.search.highlight.SimpleFragmenter; 23 import org.apache.lucene.store.Directory; 24 import org.apache.lucene.store.RAMDirectory; 25 26 import org.javabb.lucene.analysis.PortugueseAnalyzer; 27 28 29 37 public class LuceneHighlighter { 38 39 private static final String CONTENTS_FIELD = "contents"; 40 41 private static final Analyzer analyzer = new PortugueseAnalyzer(); 43 44 47 public String highlight( String text, String query, String separator, 48 int fragSize, int numFrags, boolean complete ) { 49 50 String result = ""; 51 try { 52 53 Directory ramDir = new RAMDirectory(); 54 55 addDocument(text, ramDir); 56 57 IndexReader reader = IndexReader.open(ramDir); 58 59 Query queryObj; 60 queryObj = QueryParser.parse(query, CONTENTS_FIELD, analyzer); 61 queryObj = queryObj.rewrite(reader); 62 63 Scorer scorer = new QueryScorer(queryObj); 64 Formatter formatter = new CSSFormatter("highlight"); 65 Highlighter highlighter = new Highlighter(formatter, scorer); 66 67 highlighter.setTextFragmenter(new SimpleFragmenter(fragSize)); 68 69 TokenStream token; 70 token = analyzer 71 .tokenStream(CONTENTS_FIELD, new StringReader (text)); 72 73 if (!complete) { 74 75 result = highlighter.getBestFragments(token, text, numFrags, 76 separator); 77 78 } else { 79 80 result = highlighter.getCompleteTextHighlight(token, text); 81 82 } 83 84 } catch (Exception e) { 85 86 88 } finally { 89 90 result = avoidEmpty(result, text); 91 92 } 93 94 return result; 95 96 } 97 98 103 private void addDocument( String actualText, Directory ramDir ) 104 throws IOException { 105 106 Field field = new Field(CONTENTS_FIELD, actualText, true, true, true); 107 108 Document document = new Document(); 109 110 document.add(field); 111 112 IndexWriter writer = new IndexWriter(ramDir, analyzer, true); 113 114 writer.addDocument(document); 115 writer.optimize(); 116 writer.close(); 117 118 } 119 120 private String avoidEmpty( String string , String text) { 121 122 String result = string; 123 124 if (string == null || "".equals(string.trim())) { 125 126 int length = Math.min(200, text.length()); 127 result = text.substring(0, length); 128 129 } 130 131 return result; 132 } 133 } 134 | Popular Tags |