KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > search > operations > SearchOperation


1 /*
2  * Created on Jul 18, 2003
3  * Authored by: Mindaugas Idzelis (min@idzelis.com)
4  */

5 package org.roller.business.search.operations;
6
7 import java.io.IOException JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.lucene.analysis.standard.StandardAnalyzer;
12 import org.apache.lucene.index.IndexReader;
13 import org.apache.lucene.index.Term;
14 import org.apache.lucene.queryParser.MultiFieldQueryParser;
15 import org.apache.lucene.queryParser.ParseException;
16 import org.apache.lucene.search.BooleanQuery;
17 import org.apache.lucene.search.Hits;
18 import org.apache.lucene.search.IndexSearcher;
19 import org.apache.lucene.search.Query;
20 import org.apache.lucene.search.Sort;
21 import org.apache.lucene.search.SortField;
22 import org.apache.lucene.search.TermQuery;
23 import org.roller.business.IndexManagerImpl;
24 import org.roller.business.search.FieldConstants;
25 import org.roller.business.search.IndexUtil;
26 import org.roller.model.IndexManager;
27
28
29 /**
30  * @author aim4min
31  *
32  * An operation that searches the index.
33  */

34 public class SearchOperation extends ReadFromIndexOperation
35 {
36     //~ Static fields/initializers =============================================
37

38     private static Log mLogger =
39         LogFactory.getFactory().getInstance(SearchOperation.class);
40         
41     private static String JavaDoc[] SEARCH_FIELDS = new String JavaDoc[]{
42         FieldConstants.CONTENT, FieldConstants.TITLE,
43         FieldConstants.C_CONTENT, FieldConstants.CATEGORY
44     };
45     
46     private static Sort SORTER = new Sort( new SortField(
47         FieldConstants.PUBLISHED, SortField.STRING, true) );
48
49     //~ Instance fields ========================================================
50

51     private String JavaDoc term;
52     private String JavaDoc username;
53     private String JavaDoc category;
54     private Hits searchresults;
55     private String JavaDoc parseError;
56
57     //~ Constructors ===========================================================
58

59     /**
60      * Create a new operation that searches the index.
61      */

62     public SearchOperation(IndexManager mgr)
63     {
64         // TODO: finish moving IndexManager to backend, so this cast is not needed
65
super((IndexManagerImpl)mgr);
66     }
67
68     //~ Methods ================================================================
69

70     public void setTerm(String JavaDoc term)
71     {
72         this.term = term;
73     }
74
75     /* (non-Javadoc)
76      * @see java.lang.Runnable#run()
77      */

78     public void doRun()
79     {
80         searchresults = null;
81             
82         IndexSearcher searcher = null;
83
84         try
85         {
86             IndexReader reader = manager.getSharedIndexReader();
87             searcher = new IndexSearcher(reader);
88
89             Query query =
90                 MultiFieldQueryParser.parse(
91                     term, SEARCH_FIELDS, new StandardAnalyzer());
92
93             Term tUsername =
94                 IndexUtil.getTerm(FieldConstants.USERNAME, username);
95
96             if (tUsername != null)
97             {
98                 BooleanQuery bQuery = new BooleanQuery();
99                 bQuery.add(query, true, false);
100                 bQuery.add(new TermQuery(tUsername), true, false);
101                 query = bQuery;
102             }
103             
104             Term tCategory =
105                 IndexUtil.getTerm(FieldConstants.CATEGORY, category);
106
107             if (tCategory != null)
108             {
109                 BooleanQuery bQuery = new BooleanQuery();
110                 bQuery.add(query, true, false);
111                 bQuery.add(new TermQuery(tCategory), true, false);
112                 query = bQuery;
113             }
114             searchresults = searcher.search(query, null/*Filter*/, SORTER);
115         }
116         catch (IOException JavaDoc e)
117         {
118             mLogger.error("Error searching index", e);
119             parseError = e.getMessage();
120         }
121         catch (ParseException e)
122         {
123             // who cares?
124
parseError = e.getMessage();
125         }
126         // don't need to close the reader, since we didn't do any writing!
127
}
128
129     public Hits getResults()
130     {
131         return searchresults;
132     }
133     
134     public int getResultsCount()
135     {
136         if (searchresults == null) return -1;
137         
138         return searchresults.length();
139     }
140     
141     public String JavaDoc getParseError()
142     {
143         return parseError;
144     }
145
146     /**
147      * @param string
148      */

149     public void setUsername(String JavaDoc username)
150     {
151         this.username = username;
152     }
153
154     /**
155      * @param parameter
156      */

157     public void setCategory(String JavaDoc category)
158     {
159         this.category = category;
160     }
161
162 }
163
Popular Tags