KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > blog > SearchAction


1 /*
2  * $Id: SearchAction.java,v 1.3 2005/01/15 10:40:35 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.blog;
27
28 import java.io.File JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.lucene.analysis.standard.StandardAnalyzer;
37 import org.apache.lucene.document.Document;
38 import org.apache.lucene.queryParser.QueryParser;
39 import org.apache.lucene.search.BooleanQuery;
40 import org.apache.lucene.search.Hits;
41 import org.apache.lucene.search.IndexSearcher;
42 import org.apache.lucene.search.Query;
43 import org.apache.lucene.store.Directory;
44 import org.apache.lucene.store.FSDirectory;
45
46 import com.j2biz.blogunity.exception.BlogunityException;
47 import com.j2biz.blogunity.pojo.Blog;
48 import com.j2biz.blogunity.util.LuceneSearchResult;
49 import com.j2biz.blogunity.web.ActionResultFactory;
50 import com.j2biz.blogunity.web.IActionResult;
51 import com.j2biz.blogunity.web.actions.AbstractAction;
52
53 public class SearchAction extends AbstractAction {
54
55     private static final IActionResult SEARCH_FORWARD = ActionResultFactory
56             .buildForward("/searchView.vm");
57
58     private Blog blog;
59
60     public SearchAction(Blog blog) {
61         this.blog = blog;
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
68      * javax.servlet.http.HttpServletResponse)
69      */

70     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71             throws BlogunityException {
72
73         String JavaDoc query = request.getParameter("query");
74
75         if (StringUtils.isEmpty(query)) {
76             request.setAttribute("hits", "0");
77             request.setAttribute("results", Collections.EMPTY_LIST);
78             return SEARCH_FORWARD;
79         }
80
81         File JavaDoc indexDir = blog.getIndexesDirectory();
82
83         try {
84             Directory fsDir = FSDirectory.getDirectory(indexDir, false);
85             IndexSearcher is = new IndexSearcher(fsDir);
86
87             StandardAnalyzer analyzer = new StandardAnalyzer();
88             Query q1 = QueryParser.parse(query, "body", analyzer);
89             Query q2 = QueryParser.parse(query, "excerpt", analyzer);
90             Query q3 = QueryParser.parse(query, "title", analyzer);
91             Query q4 = QueryParser.parse(query, "author", analyzer);
92
93             BooleanQuery q = new BooleanQuery();
94             q.add(q1, false, false);
95             q.add(q2, false, false);
96             q.add(q3, false, false);
97             q.add(q4, false, false);
98
99             Hits hits = is.search(q);
100
101             request.setAttribute("hits", "" + hits.length());
102             ArrayList JavaDoc results = new ArrayList JavaDoc();
103
104             for (int i = 0; i < hits.length(); i++) {
105                 Document doc = hits.doc(i);
106                 LuceneSearchResult result = new LuceneSearchResult(doc.get("title"), doc
107                         .get("permalink"), doc.get("author"), doc.get("createTime"));
108                 results.add(result);
109             }
110
111             request.setAttribute("results", results);
112
113         } catch (Throwable JavaDoc t) {
114             request.setAttribute("hits", "0");
115             request.setAttribute("results", Collections.EMPTY_LIST);
116         }
117
118         request.setAttribute("query", query);
119
120         return SEARCH_FORWARD;
121
122     }
123
124 }
Popular Tags