com.fluidbpm.program.api.util.elasticsearch.ABaseESUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-api Show documentation
Show all versions of fluid-api Show documentation
Used for the
* Custom Program Step,
* Custom Actions,
* Scheduled Actions and
* Fluid API
in the Fluid BPM and Content Management system.
/*
* Koekiebox CONFIDENTIAL
*
* [2012] - [2017] Koekiebox (Pty) Ltd
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of Koekiebox and its suppliers, if any. The intellectual and
* technical concepts contained herein are proprietary to Koekiebox
* and its suppliers and may be covered by South African and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material is strictly
* forbidden unless prior written permission is obtained from Koekiebox.
*/
package com.fluidbpm.program.api.util.elasticsearch;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.json.JSONObject;
import com.fluidbpm.program.api.util.ABaseUtil;
import com.fluidbpm.program.api.util.cache.CacheUtil;
import com.fluidbpm.program.api.util.elasticsearch.exception.FluidElasticSearchException;
import com.fluidbpm.program.api.util.sql.ABaseSQLUtil;
import com.fluidbpm.program.api.util.sql.impl.SQLFormFieldUtil;
import com.fluidbpm.program.api.vo.ABaseFluidJSONObject;
import com.fluidbpm.program.api.vo.Field;
import com.fluidbpm.program.api.vo.Form;
import com.fluidbpm.program.api.vo.TableField;
/**
* ElasticSearch base Utility class.
*
* @author jasonbruwer on 2016/08/19.
* @since 1.3
* @see ABaseUtil
* @see ABaseSQLUtil
*/
public abstract class ABaseESUtil extends ABaseSQLUtil {
protected Client client;
protected SQLFormFieldUtil fieldUtil = null;
protected static final int MAX_NUMBER_OF_TABLE_RECORDS = 10000;
//public static final String[] NO_FIELDS_MAPPER = {"_none_"};
public static final String[] NO_FIELDS_MAPPER = {"_id","_type"};
/**
* The index type.
*/
public static final class Index {
public static final String DOCUMENT = "document";
public static final String FOLDER = "folder";
public static final String TABLE_RECORD = "table_record";
/**
* Checks to see whether {@code indexParam} is a valid
* index type.
*
* @param indexParam The index to check.
* @return {@code true} for valid index and {@code false} for invalid index.
*/
public static boolean isIndexValid(String indexParam)
{
if(DOCUMENT.equals(indexParam))
{
return true;
}
else if(FOLDER.equals(indexParam))
{
return true;
}
else if(TABLE_RECORD.equals(indexParam))
{
return true;
}
return false;
}
}
/**
* Initialise with the ElasticSearch client.
*
* @param connectionParam SQL Connection to use.
* @param esClientParam The ES Client.
* @param cacheUtilParam The Cache Util for better performance.
*/
public ABaseESUtil(
Connection connectionParam,
Client esClientParam,
CacheUtil cacheUtilParam) {
super(connectionParam, cacheUtilParam);
this.client = esClientParam;
this.fieldUtil = new SQLFormFieldUtil(connectionParam, cacheUtilParam);
}
/**
* Initialise with the ElasticSearch client.
*
* @param esClientParam The ES Client.
*/
public ABaseESUtil(Client esClientParam) {
super(null);
this.client = esClientParam;
}
/*******************************************************/
/**
* Performs a search against the Elasticsearch instance with the {@code qbParam}.
*
* @param qbParam The @{code QueryBuilder} to search with.
* @param indexParam The Elasticsearch Index to use {@code (document, folder or table_field)}.
* @param numberOfResultsParam The number of results to return.
* @param formTypesParam The Id's for the Form Definitions to be returned.
* @return The {@code ElasticTypeAndId} {@code List}.
*
* @see Index
* @see ElasticTypeAndId
*/
public final List searchAndConvertHitsToIdsOnly(
QueryBuilder qbParam,
String indexParam,
int numberOfResultsParam,
Long ... formTypesParam) {
SearchHits searchHits = this.searchWithHits(
qbParam,
indexParam,
true,
numberOfResultsParam,
formTypesParam);
List returnVal = null;
long totalHits;
if (searchHits != null && (totalHits = searchHits.getTotalHits()) > 0) {
returnVal = new ArrayList();
if((searchHits.getHits().length != totalHits) &&
(searchHits.getHits().length != numberOfResultsParam))
{
throw new FluidElasticSearchException(
"The Hits and fetch count has mismatch. Total hits is '"+totalHits
+"' while hits is '"+
searchHits.getHits().length+"'.");
}
long iterationMax = totalHits;
if(numberOfResultsParam > 0 &&
totalHits > numberOfResultsParam)
{
iterationMax = numberOfResultsParam;
}
//Iterate...
for(int index = 0;index < iterationMax;index++)
{
SearchHit searchHit = searchHits.getAt(index);
String idAsString;
if((idAsString = searchHit.getId()) == null)
{
continue;
}
returnVal.add(new ElasticTypeAndId(
this.toLongSafe(idAsString),
searchHit.getType()));
}
}
return returnVal;
}
/**
* Performs a search against the Elasticsearch instance with the {@code qbParam}.
*
* @param qbParam The @{code QueryBuilder} to search with.
* @param indexParam The Elasticsearch Index to use {@code (document, folder or table_field)}.
* @param withNoFieldsParam Should fields be IGNORED returned.
* @param numberOfResultsParam The number of results to return.
* @param formTypesParam The Id's for the Form Definitions to be returned.
*
* @return The Elasticsearch {@code SearchHits}.
*/
public final SearchHits searchWithHits(
QueryBuilder qbParam,
String indexParam,
boolean withNoFieldsParam,
int numberOfResultsParam,
Long ... formTypesParam) {
if(this.client == null)
{
throw new ElasticsearchException("Elasticsearch client is not set.");
}
SearchRequestBuilder searchRequestBuilder = this.client.prepareSearch(
//Indexes...
indexParam)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(qbParam)
.setFrom(0)
.setExplain(false);
//No Fields...
if (withNoFieldsParam) {
searchRequestBuilder = searchRequestBuilder.storedFields(NO_FIELDS_MAPPER);
}
//The requested number of results...
if(numberOfResultsParam > 0)
{
searchRequestBuilder = searchRequestBuilder.setSize(
numberOfResultsParam);
}
if(formTypesParam == null)
{
formTypesParam = new Long[]{};
}
//If Types is set...
if(formTypesParam != null && formTypesParam.length > 0)
{
String[] formTypesAsString = new String[formTypesParam.length];
for(int index = 0;index < formTypesParam.length;index++)
{
Long formTypeId = formTypesParam[index];
if(formTypeId == null)
{
continue;
}
formTypesAsString[index] = formTypeId.toString();
}
searchRequestBuilder = searchRequestBuilder.setTypes(formTypesAsString);
}
//Perform the actual search...
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
if (searchResponse == null) {
return null;
}
return searchResponse.getHits();
}
/**
* Performs a search against the Elasticsearch instance with the {@code qbParam}.
*
* @param qbParam The @{code QueryBuilder} to search with.
* @param indexParam The Elasticsearch Index to use {@code (document, folder or table_field)}.
* @param withNoFieldsParam Should fields be IGNORED returned.
* @param numberOfResultsParam The number of results to return.
* @param formTypesParam The Id's for the Form Definitions to be returned.
*
* @return {@code true} if there were any {@code SearchHits} from the lookup.
*/
public final boolean searchContainHits(
QueryBuilder qbParam,
String indexParam,
boolean withNoFieldsParam,
int numberOfResultsParam,
Long ... formTypesParam) {
SearchHits searchHits = this.searchWithHits(
qbParam,
indexParam,
withNoFieldsParam,
numberOfResultsParam,
formTypesParam);
return (searchHits != null && searchHits.getTotalHits() > 0);
}
/**
* Retrieves the {@code Form}'s via the provided {@code formIdsParam}.
*
* @param indexParam The ElasticSearch index to be used for lookup.
* @param formIdsParam {@code List} of Identifiers for the Forms to return.
* @param includeFieldDataParam Whether to populate the return {@code Form} table fields.
* @param maxNumberOfResultsParam The maximum number of results to return.
*
* @return {@code List