KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > indexing > Searcher


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.indexing;
6
7 import java.util.List JavaDoc;
8 import org.apache.lucene.analysis.Analyzer;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.search.*;
11 /**
12  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
13  * @since Sep 13, 2004
14  * @version $Id: Searcher.java,v 1.9 2004/10/30 02:29:14 tuan08 Exp $
15  */

16 public class Searcher {
17   private org.apache.lucene.search.Searcher searcher_ ;
18   private Query modules_ ;
19   private Analyzer analyzer_ ;
20   private Hits lastSearchResult_ ;
21   
22   public Searcher(IndexSearcher searcher,
23                   Analyzer analyzer) throws Exception JavaDoc {
24     searcher_ = searcher;
25     analyzer_ = analyzer ;
26   }
27   
28   public void setQueryModules(Query query) {
29     modules_ = query ;
30   }
31   
32   public Hits getLastSearchResult() { return lastSearchResult_ ; }
33   
34   public HitPageList search(SearchInput input) throws Exception JavaDoc {
35     BooleanQuery query = new BooleanQuery() ;
36     if(modules_ != null) {
37       query.add(modules_, true, false) ;
38     }
39     
40     Query subQuery = input.getQuery(analyzer_) ;
41     query.add(subQuery, true, false) ;
42     lastSearchResult_ = searcher_.search(query) ;
43     lastSearchResult_ = searcher_.search(query) ;
44     return new HitPageList(this) ;
45   }
46   
47   public HitPageList search(List JavaDoc inputs) throws Exception JavaDoc {
48     BooleanQuery query = new BooleanQuery() ;
49     if(modules_ != null) {
50       query.add(modules_, true, false) ;
51     }
52     for(int i = 0; i < inputs.size(); i++) {
53       SearchInput input = (SearchInput) inputs.get(i) ;
54       if(input.hasTerm()) {
55         Query subQuery = input.getQuery(analyzer_) ;
56         query.add(subQuery, true, false) ;
57       }
58     }
59     lastSearchResult_ = searcher_.search(query) ;
60     return new HitPageList(this) ;
61   }
62   
63   public HitPageList search(SearchInput input, List JavaDoc modules) throws Exception JavaDoc {
64     BooleanQuery query = new BooleanQuery() ;
65     if(modules != null && modules.size() > 0) {
66       Query modulesQuery = createModuleQuery(modules);
67       query.add( modulesQuery, true, false) ;
68     }
69     
70     Query subQuery = input.getQuery(analyzer_) ;
71     query.add(subQuery, true, false) ;
72     lastSearchResult_ = searcher_.search(query) ;
73     return new HitPageList(this) ;
74   }
75   
76   
77   public HitPageList search(List JavaDoc inputs, List JavaDoc modules) throws Exception JavaDoc {
78     BooleanQuery query = new BooleanQuery() ;
79     if(modules != null && modules.size() > 0) {
80       Query modulesQuery = createModuleQuery(modules);
81       query.add( modulesQuery, true, false) ;
82     }
83     
84     for(int i = 0; i < inputs.size(); i++) {
85       SearchInput input = (SearchInput) inputs.get(i) ;
86       if(input.hasTerm()) {
87         Query subQuery = input.getQuery(analyzer_) ;
88         query.add(subQuery, true, false) ;
89       }
90     }
91     lastSearchResult_ = searcher_.search(query) ;
92     return new HitPageList(this) ;
93   }
94   
95   private Query createModuleQuery(List JavaDoc modules) {
96     BooleanQuery modulesQuery = new BooleanQuery() ;
97     for(int i = 0 ; i < modules.size(); i++) {
98       String JavaDoc module = (String JavaDoc) modules.get(i) ;
99       Query moduleQuery = new TermQuery(new Term(IndexingService.MODULE_FIELD, module)) ;
100       modulesQuery.add(moduleQuery, false, false) ;
101     }
102     return modulesQuery;
103   }
104   
105   protected void finalize() {
106     try {
107       searcher_.close() ;
108     } catch (Exception JavaDoc ex) {
109       ex.printStackTrace() ;
110     }
111   }
112 }
Popular Tags