1 package prefuse.data.search; 2 3 import java.io.IOException ; 4 import java.util.logging.Logger ; 5 6 import org.apache.lucene.document.Document; 7 import org.apache.lucene.document.Field; 8 import org.apache.lucene.queryParser.ParseException; 9 import org.apache.lucene.search.Hits; 10 11 import prefuse.data.Tuple; 12 import prefuse.util.StringLib; 13 import prefuse.util.collections.IntObjectHashMap; 14 15 30 public class KeywordSearchTupleSet extends SearchTupleSet { 31 32 private static final Logger s_logger 33 = Logger.getLogger(KeywordSearchTupleSet.class.getName()); 34 35 protected IntObjectHashMap m_map = new IntObjectHashMap(); 36 protected String m_query = ""; 37 38 protected LuceneSearcher m_lucene = null; 39 protected boolean m_storeTermVectors = false; 40 41 protected int m_id = 1; 42 43 46 public KeywordSearchTupleSet() { 47 m_lucene = new LuceneSearcher(); 48 } 49 50 54 public KeywordSearchTupleSet(LuceneSearcher searcher) { 55 m_lucene = searcher; 56 } 57 58 62 public String getQuery() { 63 return m_query; 64 } 65 66 72 public void search(String query) { 73 if ( query == null ) 74 query = ""; 75 76 if ( query.equals(m_query) ) 77 return; 79 Tuple[] rem = clearInternal(); 80 m_query = query; 81 82 query.trim(); 83 if ( query.length() == 0 ) { 84 this.fireTupleEvent(null, DELETE); 85 return; 86 } 87 88 m_lucene.setReadMode(true); 89 try { 90 Hits hits = m_lucene.search(query); 91 for ( int i=0; i < hits.length(); i++ ) { 92 Tuple t = getMatchingTuple(hits.doc(i)); 93 addInternal(t); 94 } 95 Tuple[] add = getTupleCount() > 0 ? toArray() : null; 96 fireTupleEvent(add, rem); 97 } catch (ParseException e) { 98 s_logger.warning("Lucene query parse exception.\n"+ 99 StringLib.getStackTrace(e)); 100 } catch (IOException e) { 101 s_logger.warning("Lucene IO exception.\n"+ 102 StringLib.getStackTrace(e)); 103 } 104 105 } 106 107 112 protected Tuple getMatchingTuple(Document d) { 113 int id = Integer.parseInt(d.get(LuceneSearcher.ID)); 114 return (Tuple)m_map.get(id); 115 } 116 117 120 public void index(Tuple t, String field) { 121 m_lucene.setReadMode(false); 122 String s; 123 if ( (s=t.getString(field)) == null ) return; 124 125 int id = m_id++; 126 m_lucene.addDocument(getDocument(id, s)); 127 m_map.put(id, t); 128 } 129 130 134 public boolean isUnindexSupported() { 135 return false; 136 } 137 138 143 public void unindex(Tuple t, String attrName) { 144 throw new UnsupportedOperationException (); 145 } 146 147 153 protected Document getDocument(int id, String text) { 154 Document d = new Document(); 155 d.add(Field.Text(LuceneSearcher.FIELD, text, m_storeTermVectors)); 156 d.add(Field.Keyword(LuceneSearcher.ID, String.valueOf(id))); 157 return d; 158 } 159 160 164 public LuceneSearcher getLuceneSearcher() { 165 return m_lucene; 166 } 167 168 172 public IntObjectHashMap getTupleMap() { 173 return (IntObjectHashMap)m_map.clone(); 174 } 175 176 180 public void clear() { 181 m_lucene = new LuceneSearcher(); 182 super.clear(); 183 } 184 185 } | Popular Tags |