RE: read elastic search remote connection properties in my custom portlet

Jamie Sammons, modified 2 Years ago. New Member Posts: 5 Join Date: 3/1/23 Recent Posts

​​​​​​​

 

I want to read elastic search remote connection properties in my custom portlet from System Setting -> Search -> Elastic Search Connection. How can I do this?

thumbnail
Russell Bohl, modified 2 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts

I did this in a groovy script and ran it in the scripting console, but you should be able to replace both service trackers with @Reference to those services in your component class.

import com.liferay.portal.search.engine.ConnectionInformation;
import com.liferay.portal.search.engine.NodeInformation;
import com.liferay.portal.search.engine.SearchEngineInformation;
import com.liferay.portal.kernel.module.util.SystemBundleUtil;
import org.osgi.framework.Bundle;
import org.osgi.util.tracker.ServiceTracker;

ServiceTracker<SearchEngineInformation, SearchEngineInformation> st;

st = new ServiceTracker(SystemBundleUtil.getBundleContext(), SearchEngineInformation.class, null);
st.open();

SearchEngineInformation searchEngineInformation = st.waitForService(500);

st.close();

ServiceTracker<NodeInformation, NodeInformation> st2;

st2 = new ServiceTracker(SystemBundleUtil.getBundleContext(), NodeInformation.class, null);
st2.open();

NodeInformation nodeInformation = st2.waitForService(500);

st2.close();

List<ConnectionInformation> connectionInformationList = searchEngineInformation.getConnectionInformationList();

out.println("Vendor: " + searchEngineInformation.getVendorString());
out.println("Client Version: " + searchEngineInformation.getClientVersionString());

for (ConnectionInformation connectionInformation : connectionInformationList) {

    List<NodeInformation> nodeInformationList = connectionInformation.getNodeInformationList();

    out.println("    Cluster Name: " + connectionInformation.getClusterName());
    out.println("    Connection ID: " + connectionInformation.getConnectionId());
    out.println("    Error: " + connectionInformation.getError());
    out.println("    Health: " + connectionInformation.getHealth());
    out.println("    Labels: " + connectionInformation.getLabels());
    out.println("    Nodes:");

    for (NodeInformation nodeInformationItem : nodeInformationList) {
        out.println("        Name: " + nodeInformationItem.getName());
        out.println("        Version: " + nodeInformationItem.getVersion());
    }
}