File Source: SearchOperation.java
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.weblogger.business.search.operations;
20
21 import java.io.IOException;
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.weblogger.business.search.IndexManagerImpl;
38 import org.apache.roller.weblogger.business.search.FieldConstants;
39 import org.apache.roller.weblogger.business.search.IndexUtil;
40 import org.apache.roller.weblogger.business.search.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
/*
P/P * Method: org.apache.roller.weblogger.business.search.operations.SearchOperation__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getFactory(...)@50 != null
*
* Postconditions:
* SEARCH_FIELDS == &new String[](SearchOperation__static_init#1)
* SORTER == &new Sort(SearchOperation__static_init#2)
* init'ed(mLogger)
* new Sort(SearchOperation__static_init#2) num objects == 1
* new String[](SearchOperation__static_init#1) num objects == 1
* SEARCH_FIELDS.length == 4
* SEARCH_FIELDS[0] == &"content"
* SEARCH_FIELDS[1] == &"title"
* SEARCH_FIELDS[2] == &"comment"
* SEARCH_FIELDS[3] == &"cat"
*/
50 private static Log mLogger =
51 LogFactory.getFactory().getInstance(SearchOperation.class);
52
53 private static String[] SEARCH_FIELDS = new String[]{
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 term;
64 private String websiteHandle;
65 private String category;
66 private Hits searchresults;
67 private String 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
/*
P/P * Method: void org.apache.roller.weblogger.business.search.operations.SearchOperation(IndexManager)
*
* Postconditions:
* this.manager == mgr
* init'ed(this.manager)
*/
76 super((IndexManagerImpl)mgr);
77 }
78
79 //~ Methods ================================================================
80
81 public void setTerm(String term) {
/*
P/P * Method: void setTerm(String)
*
* Postconditions:
* this.term == term
* init'ed(this.term)
*/
82 this.term = term;
83 }
84
85 /* (non-Javadoc)
86 * @see java.lang.Runnable#run()
87 */
88 public void doRun() {
/*
P/P * Method: void doRun()
*
* Preconditions:
* (soft) init'ed(SEARCH_FIELDS)
* (soft) init'ed(SORTER)
* (soft) mLogger != null
* (soft) init'ed(this.manager.reader)
* (soft) org/apache/roller/weblogger/business/search/IndexManagerImpl.mLogger != null
* (soft) init'ed(this.category)
* (soft) this.manager != null
* (soft) init'ed(this.manager.fRAMindex)
* (soft) init'ed(this.manager.indexDir)
* (soft) init'ed(this.manager.useRAMIndex)
* ...
*
* Postconditions:
* init'ed(this.manager.reader)
* possibly_updated(this.parseError)
* init'ed(this.searchresults)
*/
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 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() {
/*
P/P * Method: Hits getResults()
*
* Preconditions:
* init'ed(this.searchresults)
*
* Postconditions:
* return_value == this.searchresults
* init'ed(return_value)
*/
132 return searchresults;
133 }
134
135 public int getResultsCount() {
/*
P/P * Method: int getResultsCount()
*
* Preconditions:
* init'ed(this.searchresults)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.searchresults: Inverse{null}, Addr_Set{null}
*/
136 if (searchresults == null) return -1;
137
138 return searchresults.length();
139 }
140
141 public String getParseError() {
/*
P/P * Method: String getParseError()
*
* Preconditions:
* init'ed(this.parseError)
*
* Postconditions:
* return_value == this.parseError
* init'ed(return_value)
*/
142 return parseError;
143 }
144
145 /**
146 * @param string
147 */
148 public void setWebsiteHandle(String websiteHandle) {
/*
P/P * Method: void setWebsiteHandle(String)
*
* Postconditions:
* this.websiteHandle == websiteHandle
* init'ed(this.websiteHandle)
*/
149 this.websiteHandle = websiteHandle;
150 }
151
152 /**
153 * @param parameter
154 */
155 public void setCategory(String category) {
/*
P/P * Method: void setCategory(String)
*
* Postconditions:
* this.category == category
* init'ed(this.category)
*/
156 this.category = category;
157 }
158
159 }
SofCheck Inspector Build Version : 2.18479
| SearchOperation.java |
2009-Jan-02 14:25:34 |
| SearchOperation.class |
2009-Sep-04 03:12:31 |