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