1
14
15 package com.liferay.portal.search;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.search.Document;
20 import com.liferay.portal.kernel.search.DocumentSummary;
21 import com.liferay.portal.kernel.search.Field;
22 import com.liferay.portal.kernel.search.Hits;
23 import com.liferay.portal.kernel.search.Indexer;
24 import com.liferay.portal.kernel.search.SearchException;
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.InstancePool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.kernel.xml.Element;
29 import com.liferay.portal.model.Portlet;
30 import com.liferay.portal.service.PortletLocalServiceUtil;
31 import com.liferay.portal.theme.ThemeDisplay;
32 import com.liferay.portal.util.WebKeys;
33 import com.liferay.portlet.ratings.model.RatingsStats;
34 import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
35
36 import java.util.Date;
37
38 import javax.portlet.PortletURL;
39
40 import javax.servlet.http.HttpServletRequest;
41
42
48 public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
49
50 public abstract Hits getHits(
51 long companyId, String keywords, int start, int end)
52 throws Exception;
53
54 public abstract String getSearchPath();
55
56 public abstract String getTitle(String keywords);
57
58 public String search(
59 HttpServletRequest request, String keywords, int startPage,
60 int itemsPerPage)
61 throws SearchException {
62
63 try {
64 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65 WebKeys.THEME_DISPLAY);
66
67 int start = (startPage * itemsPerPage) - itemsPerPage;
68 int end = startPage * itemsPerPage;
69
70 Hits results = getHits(
71 themeDisplay.getCompanyId(), keywords, start, end);
72
73 int total = results.getLength();
74
75 Object[] values = addSearchResults(
76 keywords, startPage, itemsPerPage, total, start,
77 getTitle(keywords), getSearchPath(), themeDisplay);
78
79 com.liferay.portal.kernel.xml.Document doc =
80 (com.liferay.portal.kernel.xml.Document)values[0];
81 Element root = (Element)values[1];
82
83 for (int i = 0; i < results.getDocs().length; i++) {
84 Document result = results.doc(i);
85
86 String portletId = result.get(Field.PORTLET_ID);
87
88 Portlet portlet = PortletLocalServiceUtil.getPortletById(
89 themeDisplay.getCompanyId(), portletId);
90
91
94 long groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
95
96 PortletURL portletURL = getPortletURL(
97 request, portletId, groupId);
98
99 Indexer indexer = (Indexer)InstancePool.get(
100 portlet.getIndexerClass());
101
102 DocumentSummary docSummary = indexer.getDocumentSummary(
103 result, portletURL);
104
105 String title = docSummary.getTitle();
106 String url = getURL(themeDisplay, groupId, result, portletURL);
107 Date modifedDate = result.getDate(Field.MODIFIED);
108 String content = docSummary.getContent();
109
110 String[] tags = new String[0];
111
112 Field tagsEntriesField = result.getFields().get(
113 Field.TAGS_ENTRIES);
114
115 if (tagsEntriesField != null) {
116 tags = tagsEntriesField.getValues();
117 }
118
119 double ratings = 0.0;
120
121 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
122 long entryClassPK = GetterUtil.getLong(
123 result.get(Field.ENTRY_CLASS_PK));
124
125 if ((Validator.isNotNull(entryClassName)) &&
126 (entryClassPK > 0)) {
127
128 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
129 entryClassName, entryClassPK);
130
131 ratings = stats.getTotalScore();
132 }
133
134 double score = results.score(i);
135
136 addSearchResult(
137 root, title, url, modifedDate, content, tags, ratings,
138 score);
139 }
140
141 if (_log.isDebugEnabled()) {
142 _log.debug("Return\n" + doc.asXML());
143 }
144
145 return doc.asXML();
146 }
147 catch (Exception e) {
148 throw new SearchException(e);
149 }
150 }
151
152 protected String getURL(
153 ThemeDisplay themeDisplay, long groupId, Document result,
154 PortletURL portletURL)
155 throws Exception {
156
157 return portletURL.toString();
158 }
159
160 private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
161
162 }