Sunday, April 26, 2015

lucene search in liferay on custom portlet


This Topic is all about Indexing and Searching at application level for the portal, so far we've heard about indexing at database level. Indexing at database level is a data structure that improve the speed of data retrieval operation on data base table at the cost of slower writes and less storage space. 

Indices can be created using one or more columns of a database table, providing the facility for random look up of record. But if we need to index at application level then apache lucene is a great feature to use.

Liferay by default support lucene for indexing and searching for data at application level. Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java and it is open source. Any application which is require full-text search, cross-platform Apache Lucene is a great tool to use.

following steps will cover how we will search and index for custom portal data at application level.

Steps 1 : create any custom portlet and create service.xml file as we need to search custom table data through out the portal.


  Step 2 : In your liferay-portlet.xml file put this entry for new indexer class

<indexer-class>com.liferay.search.EmployeeIndexer</indexer-class>

This entry should be in pattern how its display below :


<portlet>
<portlet-name>LucenSearchProject</portlet-name>
<icon>/icon.png</icon>
<indexer-class>com.liferay.search.EmployeeIndexer</indexer-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>LucenSearchProject-portlet</css-class-wrapper>
</portlet>

the above class entry in liferay-portlet.xml is the class which will help in indexing and searching of the Portlet's data. May be while you put this entry in liferay-portlet.xml file it can give error like class not found t specified path. so lets create this class at package com.liferay.search.

Step 3 : Create a class EmployeeIndexer inside package com.liferay.search. This class should extend BaseIndexer class and overwrite some of basic method as shown below. There are total of nine methods – two of them public and seven protected.




Step 3 : After  this our next step is to index our record whenever we insert any employee to our table it should be index so that the next time if we search our employee by its name or its address its should search. This kind of searching from the index is going to drastically improve the performance so for this open your Action Class or Controller class.  In my case i have added this in my controller class below is the code for this : 


below is my JSP for calling this controller



Step 4 :  Now go to your liferay portal page and from add application add liferay search tool portlet to your page. as shown below :

then go click on configuration tab of the search tool portlet you will see a screen like shown below : 


Now at end add your model class Name as i have added for my employee model in the above image .

This is it, all you have to do is now insert any record from you portlet and search it from search tool portlet you should see your record in search result as i have added a employee with name "Jhon Dane" and then search with search tool portlet, result is as shown below : 

Note : Some time it happen like after inserting record also it wont index our record and search from search tool so in that case do indexing manually, for this navigate to control panel and go for this path Control Panel>Apps>App Manager>Manage 

In Manage tab you will see all the portlet available to the portal. Navigate to the page where your portlet is available now in the action tab you should see the option for ReIndex in action tab as shown below, but in 6.2 i didn't find any option for re-indexing from portlet level so another option is go to Control Panel> Configuration >Server Administration >Resource and click execute for Reindex All Search indexes as shown below :

it will re index all the portlet available to the portal ans create document.
So This Way we can achieve the lucene search of  Liferay.


6 comments:

  1. This code works for you?

    ReplyDelete
  2. This code have errors of type.
    For example:
    List totalRecord = new ArrayList();
    "book" actually is "Book".

    You must explain that for the line <%@include file="init.jsp"%> copy&paste init.jsp of "/html/common/" in "/docroot/js" first.

    And there are more.
    All files have errors like Unknown tags or anotations cannot be resolved

    ReplyDelete
  3. Code Is corrected now please refer.

    ReplyDelete
  4. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. It is really very helpful for us and I have gathered some important information from this blog. liferay training in hyderabad

    ReplyDelete

  5. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor-led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ pieces of training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Pratik Shekhar
    MaxMunus
    E-mail: pratik@maxmunus.com
    Ph:(0) +91 9066268701
    http://www.maxmunus.com/

    ReplyDelete