KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > search > KeywordSearchTupleSet


1 package prefuse.data.search;
2
3 import java.io.IOException JavaDoc;
4 import java.util.logging.Logger JavaDoc;
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 /**
16  * <p>
17  * SearchTupleSet implementation that performs text searches on indexed Tuple
18  * data using the Lucene search engine.
19  * <a HREF="http://lucene.apache.org/">Lucene</a> is an open source search
20  * engine supporting full text indexing and keyword search. Please refer to
21  * the Lucene web page for more information. Note that for this class to be
22  * used by prefuse applications, the Lucene classes must be included on the
23  * application classpath.
24  * </p>
25  *
26  * @version 1.0
27  * @author <a HREF="http://jheer.org">jeffrey heer</a>
28  * @see prefuse.data.query.SearchQueryBinding
29  */

30 public class KeywordSearchTupleSet extends SearchTupleSet {
31     
32     private static final Logger JavaDoc s_logger
33         = Logger.getLogger(KeywordSearchTupleSet.class.getName());
34     
35     protected IntObjectHashMap m_map = new IntObjectHashMap();
36     protected String JavaDoc m_query = "";
37     
38     protected LuceneSearcher m_lucene = null;
39     protected boolean m_storeTermVectors = false;
40     
41     protected int m_id = 1;
42     
43     /**
44      * Creates a new KeywordSearchFocusSet using an in-memory search index.
45      */

46     public KeywordSearchTupleSet() {
47         m_lucene = new LuceneSearcher();
48     }
49     
50     /**
51      * Creates a new TextSearchFocusSet with the given LuceneSearcher.
52      * @param searcher the {@link LuceneSearcher} to use.
53      */

54     public KeywordSearchTupleSet(LuceneSearcher searcher) {
55         m_lucene = searcher;
56     }
57     
58     /**
59      * Returns the current search query, if any.
60      * @return the currently active search query
61      */

62     public String JavaDoc getQuery() {
63         return m_query;
64     }
65     
66     /**
67      * Searches the indexed Tuple fields for matching keywords, using
68      * the Lucene search engine. Matching Tuples are available as the
69      * members of this TupleSet.
70      * @param query the query string to search for
71      */

72     public void search(String JavaDoc query) {
73         if ( query == null )
74             query = "";
75         
76         if ( query.equals(m_query) )
77             return; // no change
78

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 JavaDoc e) {
101             s_logger.warning("Lucene IO exception.\n"+
102                     StringLib.getStackTrace(e));
103         }
104         
105     }
106     
107     /**
108      * Return the Tuple matching the given Lucene Document, if any.
109      * @param d the Document to lookup.
110      * @return the matching Tuple, or null if none.
111      */

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     /**
118      * @see prefuse.data.search.SearchTupleSet#index(prefuse.data.Tuple, java.lang.String)
119      */

120     public void index(Tuple t, String JavaDoc field) {
121         m_lucene.setReadMode(false);
122         String JavaDoc 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     /**
131      * Returns false, as unindexing values is not currently supported.
132      * @see prefuse.data.search.SearchTupleSet#isUnindexSupported()
133      */

134     public boolean isUnindexSupported() {
135         return false;
136     }
137     
138     /**
139      * This method throws an exception, as unidexing is not supported.
140      * @see prefuse.data.search.SearchTupleSet#unindex(prefuse.data.Tuple, java.lang.String)
141      * @throws UnsupportedOperationException
142      */

143     public void unindex(Tuple t, String JavaDoc attrName) {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146     
147     /**
148      * Create a Lucene Document instance with the given document ID and text.
149      * @param id the document ID
150      * @param text the text the Document should contain
151      * @return a new Lucene Document instance
152      */

153     protected Document getDocument(int id, String JavaDoc 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     /**
161      * Get the {@link LuceneSearcher} instance used by this class.
162      * @return returns the backing lucene searcher.
163      */

164     public LuceneSearcher getLuceneSearcher() {
165         return m_lucene;
166     }
167     
168     /**
169      * Returns a copy of the mapping from Lucene document IDs to prefuse Tuple instances.
170      * @return a copy of the map from lucene doc IDs to prefuse Tuples.
171      */

172     public IntObjectHashMap getTupleMap() {
173         return (IntObjectHashMap)m_map.clone();
174     }
175     
176     /**
177      * Removes all search hits and clears out the index.
178      * @see prefuse.data.tuple.TupleSet#clear()
179      */

180     public void clear() {
181         m_lucene = new LuceneSearcher();
182         super.clear();
183     }
184     
185 } // end of class KeywordSearchTupleSet
186
Popular Tags