001
014
015 package com.liferay.portal.kernel.search;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.PortletProvider;
020 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.kernel.util.WebKeys;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.model.Group;
026 import com.liferay.portal.model.Layout;
027 import com.liferay.portal.theme.ThemeDisplay;
028 import com.liferay.portlet.ratings.model.RatingsStats;
029 import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
030
031 import java.util.Date;
032 import java.util.Locale;
033
034 import javax.portlet.PortletURL;
035
036 import javax.servlet.http.HttpServletRequest;
037
038
042 public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
043
044 public Indexer getIndexer() {
045 if (_log.isWarnEnabled()) {
046 _log.warn(getClass() + " does not implement getIndexer()");
047 }
048
049 return null;
050 }
051
052 public abstract String getSearchPath();
053
054 public Summary getSummary(
055 Indexer indexer, Document document, Locale locale, String snippet)
056 throws SearchException {
057
058 return indexer.getSummary(document, snippet, null, null);
059 }
060
061 public abstract String getTitle(String keywords);
062
063 @Override
064 public String search(
065 HttpServletRequest request, long groupId, long userId,
066 String keywords, int startPage, int itemsPerPage, String format)
067 throws SearchException {
068
069 try {
070 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
071 WebKeys.THEME_DISPLAY);
072
073 SearchContext searchContext = SearchContextFactory.getInstance(
074 request);
075
076 searchContext.setAttribute("paginationType", "more");
077
078 if (groupId == 0) {
079 searchContext.setGroupIds(null);
080 }
081 else {
082 searchContext.setGroupIds(new long[] {groupId});
083 }
084
085 int end = startPage * itemsPerPage;
086
087 searchContext.setEnd(end);
088
089 Layout layout = themeDisplay.getLayout();
090
091 Group layoutGroup = layout.getGroup();
092
093 if (!layoutGroup.isStagingGroup() &&
094 !layoutGroup.isControlPanel() &&
095 !layoutGroup.isUserPersonalPanel()) {
096
097 searchContext.setIncludeStagingGroups(false);
098 }
099
100 searchContext.setKeywords(keywords);
101 searchContext.setScopeStrict(false);
102
103 int start = (startPage * itemsPerPage) - itemsPerPage;
104
105 searchContext.setStart(start);
106
107 searchContext.setUserId(userId);
108
109 Indexer indexer = getIndexer();
110
111 Hits results = indexer.search(searchContext);
112
113 String[] queryTerms = results.getQueryTerms();
114
115 int total = results.getLength();
116
117 Object[] values = addSearchResults(
118 queryTerms, keywords, startPage, itemsPerPage, total, start,
119 getTitle(keywords), getSearchPath(), format, themeDisplay);
120
121 com.liferay.portal.kernel.xml.Document doc =
122 (com.liferay.portal.kernel.xml.Document)values[0];
123 Element root = (Element)values[1];
124
125 for (int i = 0; i < results.getDocs().length; i++) {
126 Document result = results.doc(i);
127
128 String snippet = result.get(Field.SNIPPET);
129
130 long resultGroupId = GetterUtil.getLong(
131 result.get(Field.GROUP_ID));
132
133 if (resultGroupId == 0) {
134 resultGroupId = themeDisplay.getScopeGroupId();
135 }
136
137 long resultScopeGroupId = GetterUtil.getLong(
138 result.get(Field.SCOPE_GROUP_ID));
139
140 if (resultScopeGroupId == 0) {
141 resultScopeGroupId = themeDisplay.getScopeGroupId();
142 }
143
144 String className = indexer.getClassName();
145
146 if (Validator.isNull(className)) {
147 className = result.get(Field.ENTRY_CLASS_NAME);
148 }
149
150 String portletId = PortletProviderUtil.getPortletId(
151 className, PortletProvider.Action.VIEW);
152
153 PortletURL portletURL = getPortletURL(
154 request, portletId, resultScopeGroupId);
155
156 Summary summary = getSummary(
157 indexer, result, themeDisplay.getLocale(), snippet);
158
159 String title = summary.getTitle();
160 String url = getURL(
161 themeDisplay, resultScopeGroupId, result, portletURL);
162 Date modifiedDate = result.getDate(Field.MODIFIED_DATE);
163 String content = summary.getContent();
164
165 String[] tags = new String[0];
166
167 Field assetTagNamesField = result.getFields().get(
168 Field.ASSET_TAG_NAMES);
169
170 if (assetTagNamesField != null) {
171 tags = assetTagNamesField.getValues();
172 }
173
174 double ratings = 0.0;
175
176 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
177 long entryClassPK = GetterUtil.getLong(
178 result.get(Field.ENTRY_CLASS_PK));
179
180 if (Validator.isNotNull(entryClassName) && (entryClassPK > 0)) {
181 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
182 entryClassName, entryClassPK);
183
184 ratings = stats.getTotalScore();
185 }
186
187 double score = results.score(i);
188
189 addSearchResult(
190 root, resultGroupId, resultScopeGroupId, entryClassName,
191 entryClassPK, title, url, modifiedDate, content, tags,
192 ratings, score, format);
193 }
194
195 if (_log.isDebugEnabled()) {
196 _log.debug("Return\n" + doc.asXML());
197 }
198
199 return doc.asXML();
200 }
201 catch (Exception e) {
202 throw new SearchException(e);
203 }
204 }
205
206 protected String getURL(
207 ThemeDisplay themeDisplay, long groupId, Document result,
208 PortletURL portletURL)
209 throws Exception {
210
211 return portletURL.toString();
212 }
213
214 private static final Log _log = LogFactoryUtil.getLog(
215 HitsOpenSearchImpl.class);
216
217 }