KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > lucene > search > SimpleLuceneSearcher


1 /*
2  * Copyright 2004 JavaFree.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package org.javabb.lucene.search;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.queryParser.MultiFieldQueryParser;
25 import org.apache.lucene.queryParser.ParseException;
26 import org.apache.lucene.search.Hits;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.search.Query;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.store.FSDirectory;
31
32 import org.javabb.infra.Monitor;
33 import org.javabb.lucene.index.LuceneIndexerException;
34
35 import org.springframework.core.io.Resource;
36
37
38 /**
39  * @author <a HREF="mailto:jackganzha@dev.java.net">Marcos Silva Pereira</a>
40  *
41  * @version $Id: SimpleLuceneSearcher.java,v 1.4.8.2 2006/04/17 17:47:00 daltoncamargo Exp $
42  */

43 public class SimpleLuceneSearcher implements LuceneSearcher {
44
45     private Directory path;
46     private Analyzer analyzer;
47
48     private static Object JavaDoc monitor = Monitor.MONITOR;
49
50     /**
51      * @param path
52      * @param analyzer
53      */

54     public SimpleLuceneSearcher ( Directory path, Analyzer analyzer ) {
55
56         this.path = path;
57         this.analyzer = analyzer;
58     }
59
60     /**
61      * @param path
62      * @param analyzer
63      * @throws IOException
64      */

65     public SimpleLuceneSearcher ( Resource path, Analyzer analyzer ) throws IOException JavaDoc {
66
67         this(FSDirectory.getDirectory(path.getFile(), false), analyzer);
68     }
69
70     /**
71      * @see org.javabb.lucene.search.LuceneSearcher#search(java.lang.String)
72      */

73     public List JavaDoc search(String JavaDoc query, String JavaDoc[] fields) {
74
75         List JavaDoc result = new ArrayList JavaDoc();
76
77         try {
78
79             synchronized (monitor) {
80
81                 IndexSearcher searcher = new IndexSearcher(path);
82
83                 Query queryObj;
84                 queryObj = MultiFieldQueryParser.parse(query, fields, analyzer);
85
86                 Hits hits = searcher.search(queryObj);
87
88                 for (int i = 0; i < hits.length(); i++) {
89
90                     Document document = hits.doc(i);
91
92                     String JavaDoc idValue = document.getField("postId").stringValue();
93                     long postId = Long.parseLong(idValue);
94
95                     result.add(new Long JavaDoc(postId));
96
97                 }
98
99                 searcher.close();
100
101             }
102
103         } catch (IOException JavaDoc e) {
104
105             String JavaDoc msg = "Can't read the index at path [" + path
106                 + "]. It is really exists?";
107             throw new LuceneIndexerException(msg, e);
108
109         } catch (ParseException e) {
110
111             String JavaDoc msg = "Query syntax is not ok. The query is [" + query + "]";
112             throw new LuceneIndexerException(msg, e);
113
114         }
115
116         return result;
117
118     }
119
120 }
121
Popular Tags