001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /* Created on Jul 18, 2003 */
019: package org.apache.roller.business.search.operations;
020:
021: import java.io.IOException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.lucene.analysis.standard.StandardAnalyzer;
026: import org.apache.lucene.index.IndexReader;
027: import org.apache.lucene.index.Term;
028: import org.apache.lucene.queryParser.MultiFieldQueryParser;
029: import org.apache.lucene.queryParser.ParseException;
030: import org.apache.lucene.search.BooleanQuery;
031: import org.apache.lucene.search.Hits;
032: import org.apache.lucene.search.IndexSearcher;
033: import org.apache.lucene.search.Query;
034: import org.apache.lucene.search.Sort;
035: import org.apache.lucene.search.SortField;
036: import org.apache.lucene.search.TermQuery;
037: import org.apache.roller.business.search.IndexManagerImpl;
038: import org.apache.roller.business.search.FieldConstants;
039: import org.apache.roller.business.search.IndexUtil;
040: import org.apache.roller.business.search.IndexManager;
041:
042: /**
043: * An operation that searches the index.
044: * @author Mindaugas Idzelis (min@idzelis.com)
045: */
046: public class SearchOperation extends ReadFromIndexOperation {
047: //~ Static fields/initializers =============================================
048:
049: private static Log mLogger = LogFactory.getFactory().getInstance(
050: SearchOperation.class);
051:
052: private static String[] SEARCH_FIELDS = new String[] {
053: FieldConstants.CONTENT, FieldConstants.TITLE,
054: FieldConstants.C_CONTENT, FieldConstants.CATEGORY };
055:
056: private static Sort SORTER = new Sort(new SortField(
057: FieldConstants.PUBLISHED, SortField.STRING, true));
058:
059: //~ Instance fields ========================================================
060:
061: private String term;
062: private String websiteHandle;
063: private String category;
064: private Hits searchresults;
065: private String parseError;
066:
067: //~ Constructors ===========================================================
068:
069: /**
070: * Create a new operation that searches the index.
071: */
072: public SearchOperation(IndexManager mgr) {
073: // TODO: finish moving IndexManager to backend, so this cast is not needed
074: super ((IndexManagerImpl) mgr);
075: }
076:
077: //~ Methods ================================================================
078:
079: public void setTerm(String term) {
080: this .term = term;
081: }
082:
083: /* (non-Javadoc)
084: * @see java.lang.Runnable#run()
085: */
086: public void doRun() {
087: searchresults = null;
088:
089: IndexSearcher searcher = null;
090:
091: try {
092: IndexReader reader = manager.getSharedIndexReader();
093: searcher = new IndexSearcher(reader);
094:
095: Query query = MultiFieldQueryParser.parse(term,
096: SEARCH_FIELDS, new StandardAnalyzer());
097:
098: Term tUsername = IndexUtil.getTerm(
099: FieldConstants.WEBSITE_HANDLE, websiteHandle);
100:
101: if (tUsername != null) {
102: BooleanQuery bQuery = new BooleanQuery();
103: bQuery.add(query, true, false);
104: bQuery.add(new TermQuery(tUsername), true, false);
105: query = bQuery;
106: }
107:
108: Term tCategory = IndexUtil.getTerm(FieldConstants.CATEGORY,
109: category);
110:
111: if (tCategory != null) {
112: BooleanQuery bQuery = new BooleanQuery();
113: bQuery.add(query, true, false);
114: bQuery.add(new TermQuery(tCategory), true, false);
115: query = bQuery;
116: }
117: searchresults = searcher.search(query, null/*Filter*/,
118: SORTER);
119: } catch (IOException e) {
120: mLogger.error("Error searching index", e);
121: parseError = e.getMessage();
122: } catch (ParseException e) {
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: return searchresults;
131: }
132:
133: public int getResultsCount() {
134: if (searchresults == null)
135: return -1;
136:
137: return searchresults.length();
138: }
139:
140: public String getParseError() {
141: return parseError;
142: }
143:
144: /**
145: * @param string
146: */
147: public void setWebsiteHandle(String websiteHandle) {
148: this .websiteHandle = websiteHandle;
149: }
150:
151: /**
152: * @param parameter
153: */
154: public void setCategory(String category) {
155: this.category = category;
156: }
157:
158: }
|