Our Work
200+ Enterprises across globally trust KTree for their Web & Mobile application Development needs.See What We Do
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
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'
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
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();}
}
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.DocumentResult;
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);}
}
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;
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();