Wednesday 28 March 2012

Implementing search throught assets/journal articles

Hello,
    Last days I was working about portlet which has to implement some search functionality througth Liferay's web content/journal articles. And i want to share some information related search implementation.

    Main idea of the portlet is to display information in some specific way, and have functionality of local searching and global throug it.
    I had two ways of implementation of such requirement - first way it to develop my own entities with service builder, develop own administration part for it and display those entities. Second way - use web content/journal articles biult-in mechanism. I decided to follow second approach. Main complexity was how to implement search througth Lifery's web content.
   
    For some starting point related Liferay's search it is good to check this article.

    First, let's create some search form:

 <%@ page import="com.liferay.portal.kernel.dao.search.SearchContainer"%><%@
  page import="com.liferay.portal.kernel.search.SearchContext"%><%@
  page import="com.liferay.portal.kernel.search.Hits"%><%@
  page import="com.liferay.portal.kernel.search.Indexer"%><%@
  page import="com.liferay.portal.kernel.search.Document"%><%@
  page import="com.liferay.portal.kernel.search.Field"%><%@
  page import="com.liferay.portal.kernel.search.FacetedSearcher"%><%@
  page import="com.liferay.portal.kernel.search.facet.Facet"%><%@
  page import="com.liferay.portal.kernel.search.facet.AssetEntriesFacet"%><%@
  page import="com.liferay.portal.kernel.search.facet.ScopeFacet"%><%@
  page import="com.liferay.portal.kernel.search.facet.config.FacetConfiguration"%><%@
  page import="com.liferay.portal.kernel.search.facet.config.FacetConfigurationUtil"%><%@
  page import="com.liferay.portal.kernel.search.facet.util.FacetFactoryUtil"%><%@
  page import="javax.portlet.PortletURL"%><%@
  page import="com.liferay.portal.kernel.util.HtmlUtil"%><%@
  page import="com.liferay.portal.kernel.dao.search.ResultRow"%><%@
  page import="com.liferay.portal.kernel.search.SearchContextFactory"%>
 <%
    String searchKeyword = ParamUtil.getString(renderRequest, 'searchKeyword');
 %>
 
        
      

      
        
      Second, create search context and make search:
<%
 List<Document> documents = null;
 SearchContext searchContext = SearchContextFactory.getInstance(request);
 searchContext.setKeywords(searchKeyword);
 searchContext.setEnd(2000);
 searchContext.setStart(0);
   
 String[] entryCalssName = {JournalArticle.class.getName()};
 searchContext.setEntryClassNames(entryCalssName);
 
 Indexer indexer = FacetedSearcher.getInstance();
 
 Hits hits = indexer.search(searchContext);
   
 documents = hits.toList();
%>
        As a result you will have list Document entites, with some fields in it. Now we have to iterate througth them and get AssetEntry and JournalArticle to display.
 <% 
if (documents != null && !documents.isEmpty()) {
 for(Document doc : documents) { 
 
    Long classPK = Long.parseLong(doc.get(Field.ENTRY_CLASS_PK)); 
    
    AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(JournalArticle.class.getName(), classPK);
    
    AssetRendererFactory af = AssetRendererFactoryRegistryUtil
        .getAssetRendererFactoryByClassName(JournalArticle.class.getName());
 
    AssetRenderer assetRenderer = af.getAssetRenderer(assetEntry.getClassPK());
    String renderPath = assetRenderer.render(renderRequest,
                                             renderResponse,
                                             AssetRenderer.TEMPLATE_FULL_CONTENT);
    String renderPortletId = AssetRendererFactoryRegistryUtil
        .getAssetRendererFactoryByClassName(JournalArticle.class.getName()).getPortletId();
 %> 
   
<%=assetEntry.getTitle(locale)%>
<% } } %>
       To see what kind of fields you can retrieve from Document entity check Field class. Tag liferay-util:include will generate full content of Journal article.
   
       In this approach I didn't use SearchContainer and liferay-ui:search-container tag, but they can be really usefull, you can check how they works here.

UPD. If you want to make search through particular entity you can use next code:
Indexer indexer = IndexerRegistryUtil.getIndexer(JournalIndexer.class);
Hits hits = indexer.search(searchContext);
documents = hits.toList();
also for searching through JournalArticle this util can be used:
JournalArticleServiceUtil.search(company.getCompanyId(), themeDisplay.getScopeGroupId(), 0, "", new Double(-1.0), "", "", keyword, "", "", "", null, null, 0, null, false, 0, Integer.MAX_VALUE, null);
BR, Paul

9 comments:

  1. it should be put everything in a single jsp??

    ReplyDelete
  2. Hi,
    Up to you, first one is siple search form, second is search processing and third one is showing results.

    ReplyDelete
  3. Hi I get this error for unknown reason can you help me?

    javax.portlet.PortletException: org.apache.jasper.JasperException: /view.jsp(49,10) equal symbol expected

    String searchKeyword = ParamUtil.getString(renderRequest, "searchKeyword");

    ReplyDelete
    Replies
    1. Hi,
      Try to use '' instead of ""


      String searchKeyword = ParamUtil.getString(renderRequest, 'searchKeyword');

      Delete
  4. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in liferay, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on liferay. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    MaxMunus
    E-mail: nitesh@maxmunus.com
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023
    http://www.maxmunus.com/


    ReplyDelete
  5. you’re getting your information, but good topic. I need to spend some time learning more or understanding more. Thanks for fantastic info I was looking for this information for my mission.

    Dot Net Training in Chennai

    Software Testing Training in Chennai

    ReplyDelete
  6. Your thinking toward the respective issue is awesome also the idea behind the blog is very interesting which would bring a new evolution in respective field. Thanks for sharing.

    MSBI Training in Chennai

    Informatica Training in Chennai

    ReplyDelete
  7. It's really an informative and well described post.

    Title Search Services

    ReplyDelete
  8. I wish to apprentice while you amend your site, how can
    i subscribe for a blog site? The account helped me a acceptable deal.
    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete