KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > search > ChannelSearcher


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: ChannelSearcher.java,v 1.8 2003/08/12 15:18:10 niko_schmuck Exp $
28

29 package de.nava.informa.search;
30
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 import org.apache.lucene.analysis.Analyzer;
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.queryParser.ParseException;
40 import org.apache.lucene.search.Searcher;
41 import org.apache.lucene.search.IndexSearcher;
42 import org.apache.lucene.search.Query;
43 import org.apache.lucene.search.Hits;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 import de.nava.informa.core.ChannelIF;
48 import de.nava.informa.core.ItemIF;
49 import de.nava.informa.core.ChannelGroupIF;
50
51 /**
52  * PUBLIC: Utility class for executing searches against the full-text
53  * index that does allow to directly obtain the found news items.
54  *
55  * @author Niko Schmuck (niko@nava.de)
56  */

57 public class ChannelSearcher {
58
59   private static Log logger = LogFactory.getLog(ChannelSearcher.class);
60
61   /**
62    * The default maximum number of search results that should be
63    * returned by the query.
64    */

65   public static final int DEFAULT_MAX_RESULTS = 25;
66   
67   private String JavaDoc indexDir;
68   private Analyzer analyzer;
69   private Searcher searcher;
70   private int nrOfHits;
71   
72   /**
73    * Constructor which allows to specify the index directory.
74    * For searching the full-text index the lucene
75    * {@link org.apache.lucene.analysis.standard.StandardAnalyzer}
76    * is used.
77    *
78    * @param indexDir - The directory from which the index can be retrieved.
79    */

80   public ChannelSearcher(String JavaDoc indexDir) throws IOException JavaDoc {
81     this.indexDir = indexDir;
82     this.analyzer = new StandardAnalyzer();
83     this.searcher = new IndexSearcher(indexDir);
84     this.nrOfHits = 0;
85   }
86
87   /**
88    * Performs a query on the full-text index using the field
89    * {@link ItemFieldConstants#TITLE_AND_DESC} as the default search
90    * field limited to {@link #DEFAULT_MAX_RESULTS} number of search
91    * results.
92    *
93    * @return A List of ItemResult objects ordered by relevance.
94    */

95   public List JavaDoc search(ChannelGroupIF channels, String JavaDoc queryString)
96     throws QueryParseException, IOException JavaDoc {
97     return search(channels, queryString, DEFAULT_MAX_RESULTS);
98   }
99   
100   /**
101    * Performs a query on the full-text index using the field
102    * {@link ItemFieldConstants#TITLE_AND_DESC} as the default search
103    * field.
104    *
105    * @return A List of ItemResult objects ordered by relevance.
106    */

107   public List JavaDoc search(ChannelGroupIF channels,
108                      String JavaDoc queryString, int maxResults)
109     throws QueryParseException, IOException JavaDoc {
110
111     Query query = null;
112     try {
113       logger.info("Searching for '" + queryString + "'.");
114       query = QueryParser.parse(queryString,
115                                 ItemFieldConstants.TITLE_AND_DESC,
116                                 analyzer);
117     } catch (ParseException pe) {
118       // TODO: wrap into own exception for more independence of lucene
119
throw new QueryParseException(pe);
120     }
121     Hits hits = searcher.search(query);
122     nrOfHits = hits.length();
123     logger.info("Query returned " + nrOfHits + " hits.");
124     List JavaDoc results = new ArrayList JavaDoc();
125     for (int i = 0; i < hits.length() && i < maxResults; i++) {
126       Document doc = hits.doc(i);
127       long channelId = Long.parseLong(doc.get(ItemFieldConstants.CHANNEL_ID));
128       ChannelIF channel = channels.getById(channelId);
129       if (channel == null) {
130         throw new UnretrievableException("channel " + channelId);
131       }
132       // TODO: could this be done in another fashion or using a context?
133
long itemId = Long.parseLong(doc.get(ItemFieldConstants.ITEM_ID));
134       ItemIF item = channel.getItem(itemId);
135       if (item == null) {
136         throw new UnretrievableException("item " + itemId);
137       }
138       results.add(new ItemResult(item, hits.score(i)));
139     }
140     searcher.close();
141     return results;
142   }
143
144   /**
145    * Returns the number of news items found from the previous
146    * full-text search query.
147    *
148    * Note: Use only directly after the search was performed, otherwise
149    * the return value may be wrong.
150    */

151   public int getNrOfHits() {
152     return nrOfHits;
153   }
154   
155   public void setIndexDir(String JavaDoc indexDir) {
156     this.indexDir = indexDir;
157   }
158
159   public String JavaDoc getIndexDir() {
160     return indexDir;
161   }
162   
163 }
164
Popular Tags