KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 /* Created on Jul 18, 2003 */
19 package org.apache.roller.business.search.operations;
20
21 import java.io.IOException JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.lucene.analysis.standard.StandardAnalyzer;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.queryParser.MultiFieldQueryParser;
29 import org.apache.lucene.queryParser.ParseException;
30 import org.apache.lucene.search.BooleanQuery;
31 import org.apache.lucene.search.Hits;
32 import org.apache.lucene.search.IndexSearcher;
33 import org.apache.lucene.search.Query;
34 import org.apache.lucene.search.Sort;
35 import org.apache.lucene.search.SortField;
36 import org.apache.lucene.search.TermQuery;
37 import org.apache.roller.business.IndexManagerImpl;
38 import org.apache.roller.business.search.FieldConstants;
39 import org.apache.roller.business.search.IndexUtil;
40 import org.apache.roller.model.IndexManager;
41
42
43 /**
44  * An operation that searches the index.
45  * @author Mindaugas Idzelis (min@idzelis.com)
46  */

47 public class SearchOperation extends ReadFromIndexOperation {
48     //~ Static fields/initializers =============================================
49

50     private static Log mLogger =
51             LogFactory.getFactory().getInstance(SearchOperation.class);
52     
53     private static String JavaDoc[] SEARCH_FIELDS = new String JavaDoc[]{
54         FieldConstants.CONTENT, FieldConstants.TITLE,
55         FieldConstants.C_CONTENT, FieldConstants.CATEGORY
56     };
57     
58     private static Sort SORTER = new Sort( new SortField(
59             FieldConstants.PUBLISHED, SortField.STRING, true) );
60     
61     //~ Instance fields ========================================================
62

63     private String JavaDoc term;
64     private String JavaDoc websiteHandle;
65     private String JavaDoc category;
66     private Hits searchresults;
67     private String JavaDoc parseError;
68     
69     //~ Constructors ===========================================================
70

71     /**
72      * Create a new operation that searches the index.
73      */

74     public SearchOperation(IndexManager mgr) {
75         // TODO: finish moving IndexManager to backend, so this cast is not needed
76
super((IndexManagerImpl)mgr);
77     }
78     
79     //~ Methods ================================================================
80

81     public void setTerm(String JavaDoc term) {
82         this.term = term;
83     }
84     
85     /* (non-Javadoc)
86      * @see java.lang.Runnable#run()
87      */

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

148     public void setWebsiteHandle(String JavaDoc websiteHandle) {
149         this.websiteHandle = websiteHandle;
150     }
151     
152     /**
153      * @param parameter
154      */

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