1
22
23 package com.liferay.portlet.messageboards.util;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.Hits;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.util.Time;
29 import com.liferay.util.lucene.HitsImpl;
30
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36
42 public class ThreadHits extends HitsImpl {
43
44 public ThreadHits() {
45 super();
46 }
47
48 public void recordHits(Hits hits) throws Exception {
49 setSearcher(((HitsImpl)hits).getSearcher());
50
51 Set<Long> threadIds = new HashSet<Long>();
52
53 List<Document> docs = new ArrayList<Document>(hits.getLength());
54 List<Float> scores = new ArrayList<Float>(hits.getLength());
55
56 for (int i = 0; i < hits.getLength(); i++) {
57 Document doc = hits.doc(i);
58
59 Long threadId = GetterUtil.getLong(doc.get("threadId"));
60
61 if (!threadIds.contains(threadId)) {
62 threadIds.add(threadId);
63
64 docs.add(hits.doc(i));
65 scores.add(hits.score(i));
66 }
67 }
68
69 setLength(docs.size());
70 setDocs(docs.toArray(new Document[docs.size()]));
71 setScores(scores.toArray(new Float[scores.size()]));
72
73 setSearchTime(
74 (float)(System.currentTimeMillis() - getStart()) / Time.SECOND);
75 }
76
77 }