• Liferay Post Processor Indexer Hook

    Posted on December 4, 2014 by Hamidul Islam in Liferay.

    Indexer Hook


    aimThe Indexer Hook implements a post processing system on the top of the existing indexer to allow plugin hook developers to modify their search, index, and query capabilities. If we look into Liferay source code we will see lots of entities and their corresponding indexer classes. If we want to modify any one of the indexers  then we can implement Indexer Hook. This article explains a basic use case scenario and how to implement Liferay Indexer Hook. 


    Use Case: 

    1.Go to Liferay Control Panel

    2. Go to the User Section 

    3. Search any user by Email, Screen Name, First Name or Last Name etc. For example put the search key "test@liferay.com". You will be able to see the search result. 

    Keyword-Search-User

    4. Now think little different. Consider that below are the users in your user list. You should be able to search user by the search key "Analyst" (see the Job Title Column). But you will not be able to search any user by Job Title. Is not it surprising?

    all-users

     Why we can not search user by Job Title :

    When we create new user from control panel some fields like First Name, Last Name are indexed and some feel like Job Title are not indexed.This is the default behaviour. Whenever you search user in the control panel it first search the key in index file. Unfortunately all the items under Job Title column are not indexed. So you don't get any result when you search user by Job Title. That's the actual fact behind the scene. 

    Then how can we index Job Title:

    This is the main focus of the article. Post Processor Indexer Hook is the rescue. The main purpose of the Indexer Hook is to execute our custom code which will be invoked after the default Indexer class has done its work. That means that by implementing this type of hook we can index necessary fields according to our need. In this case we want to index Job Title when a new user is created or any user is updated. Follow the steps to create Indexer Hook .

    Step 1: Create liferay plugin project of type Hook. To create liferay plugin project of type hook follow the post 

    http://proliferay.com/basics-liferay-hook-development/

    Step 2: Open liferay-hook.xml and add the below content. 

    <?xml version="1.0"?>
    <!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd">
    
    <hook>
     <indexer-post-processor>
     <indexer-class-name>com.liferay.portal.model.User</indexer-class-name>
     <indexer-post-processor-impl>com.proliferay.demo.MyCustomPostIndexer</indexer-post-processor-impl>
     </indexer-post-processor>
    </hook>
    

    Note: We are going to create our custom Indexer MyCustomPostIndexer for the entity User

    Step 3: Create a class MyCustomPostIndexer in the same package as shown in the Step 2. The content of the class should be 

    package com.proliferay.demo;
    
    import com.liferay.portal.kernel.search.BaseIndexerPostProcessor;
    import com.liferay.portal.kernel.search.Document;
    import com.liferay.portal.kernel.search.Field;
    import com.liferay.portal.model.User;
    
    /**
     *
     * @author Hamidul Islam
     *
     */
    public class MyCustomPostIndexer extends BaseIndexerPostProcessor {
    
    	public void postProcessDocument(Document document, Object object)
    			throws Exception {
    		User userEntity = (User) object;
    		String indexerUserTitle = "";
    		try {
    			indexerUserTitle = userEntity.getJobTitle();
    		} catch (Exception e) {
    		}
    		if (indexerUserTitle.length() > 0)
    			document.addText(Field.TITLE, indexerUserTitle);
    	}
    }
    
    

    Note: MyCustomPostIndexer  must extends BaseIndexerPostProcessor class. 

    Step 4: Deploy the hook.

    Step 5: From the control panel either create new user or update existing user. Ensure that you have added Job Title

    Step 6: Now search user with the keyword Job Title that you have done in Step 5. You should see the search result if you have done everything properly.

    Note: This Hook wont reflect the existing users. Only if you create new user or if you update any existing user.  

    Download Source Code

    Post Tagged with , ,

3 Responses so far.

  1. naresh reddy says:

    Hi Hamidul,

    Thanks for such helpful information

    Previously I don't know the concepts of indexer but now i got something about it

    In controller code what is meaning of following line of code

    document.addText(Field.TITLE, indexerUserTitle);

    Can you please explain it.

    Thanks

  2. Hamidul Islam says:

    Hi Naresh,
    //document.addText(Field.TITLE, indexerUserTitle);
    Whenever you save user details from the control panel job title is assigned to indexerUserTitle. This means we are indexing Job Title.

    Regards
    Admin
    http://www.proliferay.com

  3. Saifulla says:

    Thanks......

    It is very helpful me.........

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close