| ThreadHits.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portlet.messageboards.util;
21
22 import com.liferay.portal.kernel.search.Document;
23 import com.liferay.portal.kernel.search.Hits;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.Time;
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 /**
33 * <a href="ThreadHits.java.html"><b><i>View Source</i></b></a>
34 *
35 * @author Brian Wing Shun Chan
36 *
37 */
38 public class ThreadHits {
39
40 public void recordHits(Hits hits, int start, int end) throws Exception {
41 Set<Long> threadIds = new HashSet<Long>();
42
43 List<Document> docs = new ArrayList<Document>();
44 List<Float> scores = new ArrayList<Float>();
45
46 for (int i = 0; i < hits.getLength(); i++) {
47 Document doc = hits.doc(i);
48
49 Long threadId = GetterUtil.getLong(doc.get("threadId"));
50
51 if (!threadIds.contains(threadId)) {
52 threadIds.add(threadId);
53
54 docs.add(hits.doc(i));
55 scores.add(hits.score(i));
56 }
57 }
58
59 int length = docs.size();
60
61 hits.setLength(length);
62
63 if (end > length) {
64 end = length;
65 }
66
67 docs = docs.subList(start, end);
68
69 hits.setDocs(docs.toArray(new Document[docs.size()]));
70 hits.setScores(scores.toArray(new Float[docs.size()]));
71
72 hits.setSearchTime(
73 (float)(System.currentTimeMillis() - hits.getStart()) /
74 Time.SECOND);
75 }
76
77 }