Showing posts with label liferay search. Show all posts
Showing posts with label liferay search. Show all posts

Tuesday, July 27, 2021

Configure Remote Elasticsearch Cluster in Liferay Cluster

Liferay 7.x is using Elasticsearch and it’s embedded in the bundle. When we start Liferay then embedded elastic instance also will start.



Development environments, this embedded search is ok but for production environments we must need external Elasticsearch cluster.

 


If we are building Liferay cluster then all instances should connect with same Elasticsearch cluster. So, we must have some external/remote Elasticsearch.

 


This Article Demonstrate configure the remote Elasticsearch in Liferay.

 


From Liferay DXP 7.3 and CE 7.3 GA4+ Elasticsearch connector is included in the bundle and previous versions are required Elasticsearch Connector which is available in Liferay Market Place.

 


This Demonstration is using Liferay 7.4 GA2, So Elastic connector already included in the latest Liferay portal bundle.

 


Software Stack



 

Elasticsearch-7.13.3

Liferay-ce-portal-7.4.1-ga2

 

 

 


 

Following are the steps to configure the External/Remote Elasticsearch for Liferay.

 


  • Install Elastic Cluster
  • Install Liferay Cluster.
  • Configure Elasticsearch in Liferay

 


 

Install Elastic Cluster

 


Follow the below article to install Elasticsearch Cluster

 


http://www.liferaysavvy.com/2021/07/install-elastic-search-cluster.html

 



Install Liferay Cluster

 


Follow the below article to install Liferay Cluster

 


http://www.liferaysavvy.com/2021/07/liferay-portal-apache-webserver.html



 

Configure Elasticsearch in Liferay

 


There is two ways we can configure Elasticsearch in Liferay.

 


  • From Control Panel
  • Using OSGi config file

 


From Control Panel



Login to Liferay Portal as Administrator. Access Liferay Cluster or Any one the node in the cluster.


 

Access direct Liferay node using Host and Port


Example:


 

http://localhost:8090/

 

 


Global Menu --> Control Panel --> Configuration --> System Settings

 




 



 

 

 

 

System Setting --> Click on Search


 



 

 

Provide ElasticSearch7 Configuration as follows.



Step:1

 


Click on Elasticsearch Connections and Provide Connection Id and Cluster Host names. Click + Button to add multiple Elasticsearch hosts in the cluster.

 


 

http://localhost:9200

 

http://localhost:9201

 

http://localhost:9202

 

 

 


 







 

Save the configuration Elasticsearch connection Id will be created. There are many configuration properties available and based on requirement provide required properties.

 

 



 


 

Step: 2



Click on Elasticsearch7 Settings and Enable Production mode and Select Elasticsearch Connection ID that we already created in the previous step. Save the configuration.

 

 




 


Default search index is liferay-* so we can see in the Elasticsearch that the indexes will be created.


Use below URL to confirm that indexes are created.



 

http://localhost:9200/_cat/indices?v

 

 






Elasticsearch connector configuration is stored in Database So these settings available for all Liferay Nodes in the cluster.

 


Just restart all Liferay Nodes in the cluster so that all Liferay Nodes will be connecting to Remote Elasticsearch cluster and indexes will be created in the Elasticsearch cluster. We can also use Reindex control panel feature to index all search values in Elasticsearch cluster.

 


 

Using OSGi config file

 



We can also use OSGi configuration file option to connect Liferay cluster with Remote Elasticsearch.


Create following configuration files and place in “osgi/config” directory in each Liferay instance.

 

 

Create “com.liferay.portal.search.elasticsearch7.configuration.ElasticsearchConnectionConfiguration.config” file and add following properties

 


 

 

active="true"

authenticationEnabled="false"

connectionId="RemoteElasticSearchCluster"

httpSSLEnabled="false"

networkHostAddresses=[ \

  "http://localhost:9200", \

  "http://localhost:9201", \

  "http://localhost:9202", \

  ]

proxyHost=""

proxyPort="0"

proxyUserName=""

truststorePath="/path/to/localhost.p12"

truststoreType="pkcs12"

username="elastic"

 

 

 

 






 


Create “com.liferay.portal.search.elasticsearch7.configuration.ElasticsearchConfiguration.config” file and add following properties.


 

additionalConfigurations=""

additionalIndexConfigurations=""

additionalTypeMappings=""

authenticationEnabled="false"

bootstrapMlockAll="false"

clusterName="LiferayElasticsearchCluster"

discoveryZenPingUnicastHostsPort="9300-9400"

embeddedHttpPort="9201"

httpCORSAllowOrigin="/https?:\\/\\/localhost(:[0-9]+)?/"

httpCORSConfigurations=""

httpCORSEnabled="true"

httpSSLEnabled="false"

indexNamePrefix="liferay-"

indexNumberOfReplicas=""

indexNumberOfShards=""

logExceptionsOnly="true"

networkBindHost=""

networkHost=""

networkHostAddresses=[ \

  "", \

  ]

networkPublishHost=""

nodeName=""

operationMode="REMOTE"

overrideTypeMappings=""

productionModeEnabled="true"

proxyHost=""

proxyPort="0"

proxyUserName=""

remoteClusterConnectionId="RemoteElasticSearchCluster"

restClientLoggerLevel="ERROR"

sidecarDebug="false"

sidecarDebugSettings="-agentlib:jdwp\=transport\=dt_socket,address\=8001,server\=y,suspend\=y,quiet\=y"

sidecarHeartbeatInterval="10000"

sidecarHome="elasticsearch7"

sidecarHttpPort=""

sidecarJVMOptions=[ \

  "-Xms1g", \

  "-Xmx1g", \

  "-XX:+AlwaysPreTouch", \

  ]

sidecarShutdownTimeout="10000"

trackTotalHits="true"

transportTcpPort=""

truststorePath="/path/to/localhost.p12"

truststoreType="pkcs12"

username="elastic"

 

 

 


 



 

 


Liferay Node1


 



 

Liferay Node2

 




 

 

 

Once place the configuration files in all Liferay Nodes osgi/config location, Restart all Liferay instances.

 

 

 

 

Author

 

 

Friday, May 23, 2014

Liferay Google Search Portlet

Introduction:

Some time we want to use Google search in our applications. Google have given different API to integrate their products into other websites.

Similarly we have Google Custom Search API from which we can use Google search functionality in our websites.

Google have given different API for custom search like REST API and Java Script API.

In the following article we will use Java Script API to implement search in our applications.

Steps:
  1. Load Google Search Java Script Source and its CSS
  2. Initialize Google Search Control and Pass Required Options
  3. Draw search Result

Load Google Search Java Script Source and its CSS

Google Search Java Script API have implemented in java script and all required functions available in java script source file so we need load that java script source in out jsp/html page. Similarly required CSS also we need load in the page

The following is code snippet to load Google Search Java Script and CSS


<script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0"></script>
<link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet"/>


Note:

In portlet example I just copy all css and place into portlet main.css file instead of loading from source.

Initialize Google Search Control and Pass Required Options

Now we already loaded all Google search Java script ,now we need to initialize the Google Search Controller and we need to define that what are the search areas you want search like blogs, videos, images, web and book.

We are writing java script so we need write all java scrip with in <script/> tag.

Initialize Search controller


var searchControl = new GSearchControl();


Add search areas or searcher to controller


searchControl.addSearcher(new google.search.WebSearch());
searchControl.addSearcher(new google.search.VideoSearch());
searchControl.addSearcher(new google.search.BlogSearch());


Draw search Result

We already initialize the search controller and its searcher, now we need to draw or display search result in our page.

We need choose specific HTML element where we will draw search result.

The following is code snippet to draw result


searchControl.draw(document.getElementById("<portlet:namespace/>searchcontrol");

 <div id="<portlet:namespace/>searchcontrol">Loading...</div>


When we draw result we have option to decide Draw Models

Example:

Draw Mode Tabbed


var drawOptions = new google.search.DrawOptions();
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);    searchControl.draw(document.getElementById("<portlet:namespace/>searchcontrol"),
drawOptions);


Draw Mode Linear


var drawOptions = new google.search.DrawOptions();            
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_LINEAR); searchControl.draw(document.getElementById("<portlet:namespace/>searchcontrol"),
drawOptions);


The final Code in JSP page


<script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0"></script>
<link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript">
    function <portlet:namespace/>OnLoad() {
      var searchControl = new GSearchControl();
      searchControl.addSearcher(new google.search.WebSearch());
      searchControl.addSearcher(new google.search.VideoSearch());
      searchControl.addSearcher(new google.search.BlogSearch());
      searchControl.addSearcher(new google.search.NewsSearch());
      searchControl.addSearcher(new google.search.ImageSearch());
      searchControl.addSearcher(new google.search.BookSearch());
      var localSearch = new google.search.LocalSearch();
      localSearch.setCenterPoint("India");
      searchControl.addSearcher(localSearch);
      var drawOptions = new google.search.DrawOptions();
      drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
      //drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_LINEAR);
      searchControl.draw(document.getElementById("<portlet:namespace/>searchcontrol"),drawOptions);
      searchControl.execute("Liferay Savvy");
      }
    GSearch.setOnLoadCallback(<portlet:namespace/>OnLoad);
    </script>
    <div id="<portlet:namespace/>searchcontrol">Loading...</div>


When we use above search it will search in all websites. If we want restrict the search which is specific to particular web site then we can restrict the search.

Search controller restriction on website


var siteSearch = new google.search.WebSearch();
siteSearch.setUserDefinedLabel("liferaysavvy.com");
    siteSearch.setSiteRestriction("liferaysavvy.com");
    searchControl.addSearcher(siteSearch);


Note:

In the above we will get all css styles which given by Google if we want modify use <style/> tag in JPS page simply find style class name and override in jsp page.

Add a custom search engine to our Website

This is another way to add Google search to our website. Here we will create custom search engine to require websites i.e. where you want search then it will generate embedded java script and html code then we can use the code snippet in our web site.

Steps: 1

We need to have Google account

Step: 2

Access custom search console by following URL


Create new Search engine


Give your website URL name where Google will search and give result.



Step: 3

Once you fill the data then click on create button then your search engine will be created.
Now we have button called Get Code when click on that we will get code snippet so that we can 
use in websites or JSP page.


The following is code snippet


Now we can use above code in any HTML page or JPS page of our application.

Adding More Websites to Search Engine

When we create search engine we have added only one web site URL so it will search only one website and give the result.

If we want add more search sites then edit the search engine and add more web sites URLs
Access Same above URL to get Control panel


Go Edit Search Engine and Select Existed Search Engine to Modify



In the sites Search Section we have Add button click on Add and Give another Web site URL to search.



Add another Website URL for Search Engine




Once we have done click on save button then another site will be added to our Custom Search Engine. Now the result will be searched from multiple websites.

Using Google Search Portlet in Theme

We have developed the portlet so we need drag and drop the portlet in page. Then we can use the search portlet in any portal page.

Some time we don’t want use page to display portlet when we click link then portlet should load dynamically and there we need to search the items. Generally we will use these scenarios in themes.

Scenario:

Assume When I click some icon or link then I need get Portlet and there I need enter data.

We can also open portlet in any page without and drag and drop in page that is called dynamic portlet loading.

Idea is we need to create Maximized window state Portlet Render URL so that when we click the URL then we can see the portlet in Maximized window state.

Using Search Portlet in the Theme

When we want use in theme we need to create some link in portal_normal.vm file and there we will create Portlet render URL and use the link in anchor tag.

The following is code in portal_normal.vm file


<div class="google-search">
#set ($googleSearchlocPortletId = "googlesearch_WAR_ASWGoogleSearchportlet")
#set ($googleSearchportletPreferencesFactoryUtil = $portal.getClass().forName("com.liferay.portlet.PortletPreferencesFactoryUtil"))
 #set ($googleSearchportletSetup = $googleSearchportletPreferencesFactoryUtil.getLayoutPortletSetup($layout, $googleSearchlocPortletId))
#if ($googleSearchportletSetup.getValue("portletSetupShowBorders", "") != "false")
#set ($temp = $googleSearchportletSetup.setValue("portletSetupShowBorders", "false"))
#set ($temp = $googleSearchportletSetup.store())
#end
#set ($googleSearchPortletURL = $portletURLFactory.create($request, $googleSearchlocPortletId, $layout.plid, "RENDER_PHASE"))
#set ($googleSearchVOID1 = $googleSearchPortletURL.setWindowState("MAXIMIZED"))
#set ($googleSearchVOID1 = $googleSearchPortletURL.setPortletMode("view"))
<a href="${googleSearchPortletURL.toString()}"><img src="$images_folder/common/icon_google_search.jpg" style="height:33px;"/></a>
</div>


Using in other Portlet JSP page.

If you want use in other portlet JSP page from there you want access search portlet

The following is code need to add in other portlet jsp page.


<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<h1>Google Search Examples</h1>
<liferay-portlet:renderURL portletName="googlesearchportlet_WAR_GoogleSearchportlet"  var="dynamicalyLoadURL" plid="<%=themeDisplay.getPlid()%>"  varImpl="openPortletURL" windowState="<%=LiferayWindowState.MAXIMIZED.toString()%>">
<portlet:param name="mvcPath" value="/html/jsps/google.jsp"/>
</liferay-portlet:renderURL>
<br/>
<div>
<a href="<%=dynamicalyLoadURL.toString() %>"><img src="<%=renderRequest.getContextPath()%>/images/icon_google_search.jpg" style="height:33px;"/></a>
</div>



Note:

We need to make sure that the portlet liferay-portlet.xml file following tags should be present.


<add-default-resource>true</add-default-resource>
<system>true</system>


In portal-ext.properties file add portlet ID in list of portlet.add.default.resource.check.whitelist


portlet.add.default.resource.check.whitelist=3,56_INSTANCE_0000,58,82,86,87,
88,103,113,145,164,166,170,177, googlesearchportlet_WAR_GoogleSearchportlet


Google Search Icon and Search Portlet in Theme


Note:

Portlet will be loaded dynamically in the page with maximized window state.

Google Search Portlet in portal Page

Screen: 1


Screen: 2


Screen: 3



Download Portlet

Download the portlet and use in your portal I have given provide source and war file.


Note:

In the given portlet I have used custom search code with respect to my Google account so you can change accordingly.

Reference Links




Author

Recent Posts

Recent Posts Widget

Popular Posts