1
22
23 package com.liferay.util.lucene;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.Hits;
27 import com.liferay.util.Time;
28
29 import java.io.IOException;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import org.apache.lucene.search.Searcher;
35
36
42 public class HitsImpl implements Hits {
43
44 public HitsImpl() {
45 _start = System.currentTimeMillis();
46 }
47
48 public long getStart() {
49 return _start;
50 }
51
52 public void setStart(long start) {
53 _start = start;
54 }
55
56 public float getSearchTime() {
57 return _searchTime;
58 }
59
60 public void setSearchTime(float time) {
61 _searchTime = time;
62 }
63
64 public Document[] getDocs() {
65 return _docs;
66 }
67
68 public void setDocs(Document[] docs) {
69 _docs = docs;
70 }
71
72 public int getLength() {
73 return _length;
74 }
75
76 public void setLength(int length) {
77 _length = length;
78 }
79
80 public float[] getScores() {
81 return _scores;
82 }
83
84 public void setScores(float[] scores) {
85 _scores = scores;
86 }
87
88 public void setScores(Float[] scores) {
89 float[] primScores = new float[scores.length];
90
91 for (int i = 0; i < scores.length; i++) {
92 primScores[i] = scores[i].floatValue();
93 }
94
95 setScores(primScores);
96 }
97
98 public Searcher getSearcher() {
99 return _searcher;
100 }
101
102 public void setSearcher(Searcher searcher) {
103 _searcher = searcher;
104 }
105
106 public void closeSearcher() {
107 try {
108 if (_searcher != null){
109 _searcher.close();
110
111 _searcher = null;
112 }
113 }
114 catch (Exception e) {
115 }
116 }
117
118 public Document doc(int n) {
119 try {
120 if ((_docs[n] == null) && (_hits != null)) {
121 _docs[n] = new DocumentImpl(_hits.doc(n));
122 }
123 }
124 catch(IOException ioe) {
125 }
126
127 return _docs[n];
128 }
129
130 public float score(int n) {
131 try {
132 if ((_scores[n] == 0) && (_hits != null)) {
133 _scores[n] = _hits.score(n);
134 }
135 }
136 catch (IOException ioe) {
137 }
138
139 return _scores[n];
140 }
141
142 public Hits subset(int begin, int end) {
143 Hits subset = new HitsImpl();
144
145 if ((begin > - 1) && (begin <= end)) {
146 subset.setStart(getStart());
147
148 Document[] subsetDocs = new DocumentImpl[getLength()];
149 float[] subsetScores = new float[getLength()];
150
151 int j = 0;
152
153 for (int i = begin; (i < end) && (i < getLength()); i++, j++) {
154 subsetDocs[j] = doc(i);
155 subsetScores[j] = score(i);
156 }
157
158 subset.setLength(j);
159
160 subset.setDocs(subsetDocs);
161 subset.setScores(subsetScores);
162
163 _searchTime =
164 (float)(System.currentTimeMillis() - _start) / Time.SECOND;
165
166 subset.setSearchTime(getSearchTime());
167 }
168
169 return subset;
170 }
171
172 public List<Document> toList() {
173 List<Document> subset = new ArrayList<Document>(_length);
174
175 for (int i = 0; i < _length; i++) {
176 subset.add(doc(i));
177 }
178
179 return subset;
180 }
181
182 public void recordHits(
183 org.apache.lucene.search.Hits hits, Searcher searcher)
184 throws IOException {
185
186 _hits = hits;
187 _length = hits.length();
188 _docs = new DocumentImpl[_length];
189 _scores = new float[_length];
190 _searcher = searcher;
191 }
192
193 private org.apache.lucene.search.Hits _hits;
194 private long _start;
195 private float _searchTime;
196 private Document[] _docs = new DocumentImpl[0];
197 private int _length;
198 private float[] _scores = new float[0];
199 private Searcher _searcher;
200
201 }