KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > search > SearchCommand


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.search;
14
15 import java.util.Iterator JavaDoc;
16
17 import org.apache.lucene.analysis.Analyzer;
18 import org.apache.lucene.analysis.standard.StandardAnalyzer;
19 import org.apache.lucene.queryParser.QueryParser;
20 import org.apache.lucene.search.Hits;
21 import org.apache.lucene.search.IndexSearcher;
22 import org.apache.lucene.search.Query;
23 import org.apache.lucene.search.Searcher;
24
25 import com.openedit.OpenEditException;
26 import com.openedit.WebPageRequest;
27 import com.openedit.modules.BaseModule;
28
29
30 /**
31  * DOCUMENT ME!
32  *
33  * @author cburkey To change this generated comment edit the template variable "typecomment":
34  * Window>Preferences>Java>Templates. To enable and disable the creation of type comments
35  * go to Window>Preferences>Java>Code Generation.
36  */

37 public class SearchCommand extends BaseModule
38 {
39     /* (non-Javadoc)
40      * @see com.openedit.action.Command#execute(java.util.Map, java.util.Map)
41      */

42     public void search( WebPageRequest inContext ) throws OpenEditException
43     {
44
45         String JavaDoc q = inContext.getRequestParameter("query");
46
47         String JavaDoc path = getRoot().getAbsolutePath();
48
49         try
50         {
51             Searcher searcher = new IndexSearcher(path + "/search/index");
52
53             Iterator JavaDoc hits = search(q, searcher);
54             inContext.putPageValue("hits", hits);
55             inContext.putPageValue("searcher", searcher);
56
57         }
58         catch (Exception JavaDoc ex)
59         {
60             throw new OpenEditException(ex);
61         }
62     }
63
64     /**
65      * DOCME
66      *
67      * @param inQuery DOCME
68      * @param searcher DOCME
69      *
70      * @return DOCME
71      *
72      * @throws Exception DOCME
73      * @throws OpenEditException DOCME
74      */

75     public Iterator JavaDoc search(String JavaDoc inQuery, Searcher searcher)
76         throws Exception JavaDoc
77     {
78         try
79         {
80             //index");
81
Analyzer analyzer = new StandardAnalyzer();
82
83             if ((inQuery == null) || (inQuery.length() == -1))
84             {
85                 return null;
86             }
87
88             Query query = new QueryParser("contents", analyzer).parse(inQuery);
89
90             //System.out.println("Searching for: " + query.toString("contents"));
91
Hits hits = searcher.search(query);
92
93             //System.out.println(hits.length() + " total matching documents");
94
return new HitIterator(hits);
95
96             /*
97                final int HITS_PER_PAGE = 10;
98                for (int start = 0;
99                        start < hits.length();
100                        start += HITS_PER_PAGE) {
101                        int end = Math.min(hits.length(), start + HITS_PER_PAGE);
102                        for (int i = start; i < end; i++) {
103                                Document doc = hits.doc(i);
104                                String path = doc.get("path");
105                                if (path != null) {
106                                        System.out.println(i + ". " + path);
107                                } else {
108                                        String url = doc.get("url");
109                                        if (url != null) {
110                                                System.out.println(i + ". " + url);
111                                                System.out.println(" - " + doc.get("title"));
112                                        } else {
113                                                System.out.println(
114                                                        i
115                                                                + ". "
116                                                                + "No path nor URL for this document");
117                                        }
118                                }
119                        }
120
121                        if (hits.length() > end) {
122                                System.out.print("more (y/n) ? ");
123                                line = in.readLine();
124                                if (line.length() == 0 || line.charAt(0) == 'n')
125                                        break;
126                        }
127                }
128              */

129
130             //searcher.close();
131
}
132         catch (Exception JavaDoc e)
133         {
134             throw new OpenEditException(e);
135         }
136     }
137 }
138
Popular Tags