Our Work

Read & Push to ElasticSearch Index from Liferay using JEST client

Updated today

Liferay uses ElasticSearch for storing information for its search purpose. We can use ElasticSearch to store the information we need as well by keeping it in another indices. In this article we will see how to push the data to Elasticsearch and also fetch the data from ES Indices using jest client. It explains about the creation and usage of jest client connection.

Below is the small code snippet for your reference

Dependencies :

compileInclude group: 'io.searchbox', name: 'jest', version: '6.3.1'
compileInclude group: 'io.searchbox', name: 'jest-common', version: '6.3.1'
compileInclude group: 'org.elasticsearch', name: 'elasticsearch', version: '5.1.1'
compileInclude group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
compileInclude group: 'org.apache.httpcomponents', name: 'httpcore-nio', version: '4.4.11'
compileInclude group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.4'
compileInclude group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11'
compileInclude group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compileInclude group: 'com.google.guava', name: 'guava', version: '27.0.1-jre'
compileInclude group: 'org.apache.lucene', name: 'lucene-core', version: '6.3.0'
compileInclude group: 'org.apache.lucene', name: 'lucene-queryparser', version: '7.1.0'
compileInclude group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8.1'
compileInclude group: 'org.apache.lucene', name: 'lucene-join', version: '7.1.0'
compileInclude group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.10'
compileInclude group: 'joda-time', name: 'joda-time', version: '2.9.5'
compileInclude group: 'org.apache.lucene', name: 'lucene-queries', version: '5.1.0'

Client Connection :

Required things :

  • ES url
  • Read timeout

Imports :

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;


Snippet :

public JestClient getClient(String url) {
		int readTimeout = 100000;
		JestClientFactory factory = new JestClientFactory();
	factory.setHttpClientConfig(
				new HttpClientConfig.Builder(url).multiThreaded(true).readTimeout(readTimeout).build());
		return factory.getObject();}
}

Push data to Elastic Search Indices :

Required things :

  • Client connection
  • Index name
  • Index type
  • Object to be pushed

Description :

  • Get the client connection using above logic.
  • Create “Index” object by providing data object,index name and index type.
  • Call the execute method of jest client by providing the “Index” object.

Imports :

import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.DocumentResult;

Snippet :

public void pushData(AuditBean auditBean) {
		try {
			JestClient jestClient=createClient.getAuditJestClient();
			Index index = new Index.Builder(auditBean).index(auditIndexName).type(auditIndexType).build();
			DocumentResult documentResult = jestClient.execute(index);
        		} catch (Exception e) {
			logger.error(e);} 
}

Get data from ElasticSearch Indices :

Required things :

  • Client connection
  • Query builder
  • Search source builder
  • Search

Description :

  • Get the client connection
  • Create a query builder.
  • Build the query by adding necessary conditions
  • Create a “search source builder” object  by providing above query and set the result size to it.
  • Create “search” object by providing the search source builder ,index name and index type.
  • Call the execute method of jest client by providing the “search” object to get search result.
  • Get hits from the search result.

Imports :

import io.searchbox.client.JestResult;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import io.searchbox.client.JestClient;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.Search;

Snippet :

JestClient jestClient;

//creating a querybuilder
 BoolQueryBuilder qb = QueryBuilders.boolQuery();

//Building the query 
  qb.must(QueryBuilders.termQuery("orgTreePath", wdId));

//creating search source builder  
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(qb).size(10000);
            searchSourceBuilder.query(qb).from(0);
//creating search
 Search  search = new 
Search.Builder(searchSourceBuilder.toString()).addIndex(xmlActivityBetaIndexName).addType(xmlActivityBetaMappingName).setSearchType(SearchType.QUERY_THEN_FETCH).build(); 

//executing the search
SearchResult searchResult = jestClient.execute(search);
long totalCount = searchResult.getJsonObject().getAsJsonObject("hits").get("total").getAsLong();

Tags

    No tag results found for this post