KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > util > LuceneHits


1 package com.dotmarketing.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Comparator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.apache.lucene.document.Document;
11
12 import com.liferay.util.Time;
13
14 public class LuceneHits implements Serializable JavaDoc {
15
16     private class HitsComparator implements Comparator JavaDoc<LuceneHit> {
17         
18         private String JavaDoc compField;
19         private boolean desc = false;
20         
21         public HitsComparator (String JavaDoc field) {
22             if (field.endsWith("desc")) {
23                 desc = true;
24                 compField = field.substring(0, field.indexOf("desc")).trim();
25             } else if (field.endsWith("asc")) {
26                 compField = field.substring(0, field.indexOf("asc")).trim();
27             } else
28                 compField = field.trim();
29         }
30
31         /*
32          * this comparator tries to check if the "sortBy" field is
33          * 1) a Long (lucene stores dates as longs)
34          * 2) it sorts by String.compareTo
35          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
36          */

37         public int compare(LuceneHit arg0, LuceneHit arg1) {
38             String JavaDoc f0 = arg0.getDoc().get(compField);
39             String JavaDoc f1 = arg1.getDoc().get(compField);
40             if (f0 != null && f1 != null) {
41
42                 try {
43                     Long JavaDoc long0 = Long.parseLong(f0);
44                     Long JavaDoc long1 = Long.parseLong(f1);
45                     if (desc)
46                         return long1.compareTo(long0);
47                     else
48                         return long0.compareTo(long1);
49
50                 } catch (Exception JavaDoc e) {
51
52                 }
53
54                 if (desc)
55                     return f1.compareTo(f0);
56                 else
57                     return f0.compareTo(f1);
58             }
59
60             if (f0 == null)
61                 return -1;
62             else if (f1 == null) {
63                 return 1;
64             }
65             return 0;
66         }
67
68     }
69     
70     private class LuceneHit {
71         float score = 0;
72         Document doc = null;
73         
74         public LuceneHit (float score, Document doc) {
75             this.score = score;
76             this.doc = doc;
77         }
78
79         public float getScore() {
80             return score;
81         }
82
83         public void setScore(float score) {
84             this.score = score;
85         }
86
87         public Document getDoc() {
88             return doc;
89         }
90
91         public void setDoc(Document doc) {
92             this.doc = doc;
93         }
94     }
95     
96     private static final long serialVersionUID = 1L;
97     
98     public LuceneHits() {
99         _start = System.currentTimeMillis();
100     }
101
102     public void recordHits(org.apache.lucene.search.Hits hits) throws IOException JavaDoc {
103         recordHits (hits, -1, -1, null);
104     }
105
106     public void recordHits(org.apache.lucene.search.Hits hits, int offset, int limit, String JavaDoc sortBy)
107         throws IOException JavaDoc {
108
109         _total = hits.length();
110         
111         int upperIndex = hits.length();
112         if (offset > -1 && limit > 0) {
113             if (offset + limit > hits.length())
114                 upperIndex = hits.length();
115             else
116                 upperIndex = offset + limit;
117         } else {
118             upperIndex = hits.length();
119             offset = 0;
120         }
121         _length = upperIndex - offset;
122
123         if (UtilMethods.isSet(sortBy)) {
124             luceneHits = new ArrayList JavaDoc<LuceneHit>(_length);
125             int j = 0;
126             for (int i = 0; i < _total; i++, j++) {
127                 LuceneHit hit = new LuceneHit (hits.score(i), hits.doc(i));
128                 luceneHits.add(hit);
129             }
130     
131             if (UtilMethods.isSet(sortBy)) {
132                 Collections.sort(luceneHits, new HitsComparator(sortBy));
133             }
134             luceneHits = luceneHits.subList(offset, upperIndex);
135         } else {
136             luceneHits = new ArrayList JavaDoc<LuceneHit>(upperIndex - offset);
137             for (int i = offset; i < upperIndex; i++) {
138                 LuceneHit hit = new LuceneHit (hits.score(i), hits.doc(i));
139                 luceneHits.add(hit);
140             }
141         }
142         
143         _searchTime =
144             (float)(System.currentTimeMillis() - _start) / Time.SECOND;
145         
146         this._offset = offset;
147     }
148
149     public Document doc(int n) {
150         return luceneHits.get(n).getDoc();
151     }
152
153     public int length() {
154         return _length;
155     }
156
157     public float score(int n) {
158         return luceneHits.get(n).getScore();
159     }
160
161     public float searchTime() {
162         return _searchTime;
163     }
164
165     public int getTotal() {
166         return _total;
167     }
168
169     public void setTotal(int _total) {
170         this._total = _total;
171     }
172     
173     public String JavaDoc getLuceneQuery() {
174         return _luceneQuery;
175     }
176
177     public void setLuceneQuery(String JavaDoc query) {
178         _luceneQuery = query;
179     }
180
181     public int getOffset() {
182         return _offset;
183     }
184
185     public void setOffset(int offset) {
186         this._offset = offset;
187     }
188     
189     public String JavaDoc getSortBy() {
190         return _sortBy;
191     }
192
193     public void setSortBy(String JavaDoc sortBy) {
194         this._sortBy = sortBy;
195     }
196     
197     private long _start;
198     private float _searchTime;
199     private List JavaDoc<LuceneHit> luceneHits = new ArrayList JavaDoc<LuceneHit>();
200     private int _length;
201     private int _total;
202     private String JavaDoc _luceneQuery = "";
203     private int _offset = 0;
204     private String JavaDoc _sortBy = "";
205
206
207 }
208
Popular Tags