Today we will see how to use Lucene Search in Liferay. For this i create a simple portlet which can search journal Articles on the basis of title.
So lets start this Step by Step:-
Step 1:-Create a project and portlet
Create a project and then create a portlet in it .Here my project name is lucene-portlet and portlet name is Demo.
view.jsp
Here i use Boolean Query and apply some conditions like:-
1)line 28 :- Search on JournalArticle class
2)line 29 :- Title must match with the Keyword.
3)line 30 :- Fetch only Latest Articles.
Step 2:-Change the jsp
Change your view.jsp as:-
view.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page import="java.util.Locale"%> | |
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> | |
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
<portlet:defineObjects /> | |
<portlet:actionURL var="searchURL" name="searchArticle"/> | |
<aui:form action="${searchURL}" method="POST"> | |
<aui:input name="keyword" label="Search Article"/> | |
<aui:button type="submit" value="Search" /> | |
</aui:form> | |
Explanation:-
Here i create a simple form with only one text field and a submit button. By using the text field we send the keyword for search.
Step 3:-Change the Controller
Now change your Demo.java:-
Demo.java
Explanation:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.util.List; | |
import javax.portlet.ActionRequest; | |
import javax.portlet.ActionResponse; | |
import com.liferay.portal.kernel.search.BooleanQuery; | |
import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil; | |
import com.liferay.portal.kernel.search.Document; | |
import com.liferay.portal.kernel.search.Field; | |
import com.liferay.portal.kernel.search.Hits; | |
import com.liferay.portal.kernel.search.SearchContext; | |
import com.liferay.portal.kernel.search.SearchContextFactory; | |
import com.liferay.portal.kernel.search.SearchEngineUtil; | |
import com.liferay.portal.kernel.search.SearchException; | |
import com.liferay.portal.kernel.util.ParamUtil; | |
import com.liferay.portal.util.PortalUtil; | |
import com.liferay.portlet.journal.model.JournalArticle; | |
import com.liferay.util.bridges.mvc.MVCPortlet; | |
public class Demo extends MVCPortlet { | |
public void searchArticle(final ActionRequest request, final ActionResponse response) { | |
String keyword = ParamUtil.getString(request, "keyword"); | |
SearchContext searchContext = SearchContextFactory.getInstance(PortalUtil.getHttpServletRequest(request)); | |
BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext); | |
searchQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, JournalArticle.class.getName()); | |
searchQuery.addRequiredTerm(Field.TITLE, keyword); | |
searchQuery.addRequiredTerm("head", Boolean.TRUE); | |
try { | |
Hits hits = SearchEngineUtil.search(searchContext, searchQuery); | |
List<Document> docList = hits.toList(); | |
for (Document document : docList) { | |
System.out.println("Article Id=> " + document.get("articleId")); | |
System.out.println("Content =>" + document.get(Field.CONTENT)); | |
} | |
} catch (SearchException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Here i use Boolean Query and apply some conditions like:-
1)line 28 :- Search on JournalArticle class
2)line 29 :- Title must match with the Keyword.
3)line 30 :- Fetch only Latest Articles.
Step 4:-Check the Output
Now deploy the portlet and check the output . When you enter some text in text box it will get the articles that contain the required title.
You can download the source code from here.
Hope this will Help....
Related Post:-
Liferay Portlet URL in JavaScript
You can download the source code from here.
Related Post:-
Liferay Portlet URL in JavaScript
No comments:
Post a Comment