1
22
23 package com.liferay.portal.search;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.DocumentSummary;
27 import com.liferay.portal.kernel.search.Hits;
28 import com.liferay.portal.kernel.search.Indexer;
29 import com.liferay.portal.kernel.search.SearchException;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.InstancePool;
32 import com.liferay.portal.lucene.LuceneFields;
33 import com.liferay.portal.model.Portlet;
34 import com.liferay.portal.service.PortletLocalServiceUtil;
35 import com.liferay.portal.theme.ThemeDisplay;
36 import com.liferay.portal.util.WebKeys;
37
38 import java.util.Date;
39
40 import javax.portlet.PortletURL;
41
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.apache.lucene.document.DateTools;
47
48 import org.dom4j.Element;
49
50
57 public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
58
59 public abstract Hits getHits(long companyId, String keywords)
60 throws Exception;
61
62 public abstract String getSearchPath();
63
64 public abstract String getTitle(String keywords);
65
66 public String search(
67 HttpServletRequest req, String keywords, int startPage,
68 int itemsPerPage)
69 throws SearchException {
70
71 Hits hits = null;
72
73 try {
74 ThemeDisplay themeDisplay =
75 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
76
77 hits = getHits(themeDisplay.getCompanyId(), keywords);
78
79 Object[] values = addSearchResults(
80 keywords, startPage, itemsPerPage, hits, getTitle(keywords),
81 getSearchPath(), themeDisplay);
82
83 Hits results = (Hits)values[0];
84 org.dom4j.Document doc = (org.dom4j.Document)values[1];
85 Element root = (Element)values[2];
86
87 for (int i = 0; i < results.getLength(); i++) {
88 Document result = results.doc(i);
89
90 String portletId = result.get(LuceneFields.PORTLET_ID);
91
92 Portlet portlet = PortletLocalServiceUtil.getPortletById(
93 themeDisplay.getCompanyId(), portletId);
94
95
98 long groupId = GetterUtil.getLong(
99 result.get(LuceneFields.GROUP_ID));
100
101 PortletURL portletURL = getPortletURL(req, portletId, groupId);
102
103 Indexer indexer = (Indexer)InstancePool.get(
104 portlet.getIndexerClass());
105
106 DocumentSummary docSummary = indexer.getDocumentSummary(
107 result, portletURL);
108
109 String title = docSummary.getTitle();
110 String url = getURL(themeDisplay, groupId, result, portletURL);
111 Date modifedDate = DateTools.stringToDate(
112 result.get(LuceneFields.MODIFIED));
113 String content = docSummary.getContent();
114 double score = hits.score(i);
115
116 addSearchResult(root, title, url, modifedDate, content, score);
117 }
118
119 if (_log.isDebugEnabled()) {
120 _log.debug("Return\n" + doc.asXML());
121 }
122
123 return doc.asXML();
124 }
125 catch (Exception e) {
126 throw new SearchException(e);
127 }
128 finally {
129 if (hits != null) {
130 hits.closeSearcher();
131 }
132 }
133 }
134
135 protected String getURL(
136 ThemeDisplay themeDisplay, long groupId, Document result,
137 PortletURL portletURL)
138 throws Exception {
139
140 return portletURL.toString();
141 }
142
143 private static Log _log = LogFactory.getLog(HitsOpenSearchImpl.class);
144
145 }