use of org.folio.rest.persist.facets.FacetManager in project raml-module-builder by folio-org.
the class PostgresClient method buildFacetQuery.
/**
* function uses freemarker templating, the template will be loaded the first time
* should take about 70-80 milli - after that the template gets cached and will be sub milli
* @param tableName
* @param where
* @param facets
* @param query
* @return
* @throws Exception
*/
private String buildFacetQuery(String tableName, ParsedQuery parsedQuery, List<FacetField> facets, boolean countRequested, String query) throws Exception {
long start = System.nanoTime();
FacetManager fm = new FacetManager(convertToPsqlStandard(tenantId) + "." + tableName);
if (parsedQuery.getWhereClause() != null) {
fm.setWhere(" where " + parsedQuery.getWhereClause());
}
fm.setSupportFacets(facets);
fm.setIdField(idField);
fm.setLimitClause(parsedQuery.getLimitClause());
fm.setOffsetClause(parsedQuery.getOffsetClause());
fm.setMainQuery(parsedQuery.getQueryWithoutLimOff());
fm.setSchema(convertToPsqlStandard(tenantId));
fm.setCountQuery(org.apache.commons.lang.StringEscapeUtils.escapeSql(parsedQuery.getCountFuncQuery()));
long end = System.nanoTime();
log.debug("timer: buildFacetQuery (ns) " + (end - start));
return fm.generateFacetQuery();
}
use of org.folio.rest.persist.facets.FacetManager in project raml-module-builder by folio-org.
the class PostgresClient method buildFacetManager.
private FacetManager buildFacetManager(CQLWrapper wrapper, QueryHelper queryHelper, String mainQuery, List<FacetField> facets) {
FacetManager fm = new FacetManager(schemaName + DOT + queryHelper.table);
if (wrapper.getWhereClause().isEmpty()) {
fm.setWhere(" " + wrapper.getWhereClause());
}
fm.setSupportFacets(facets);
fm.setIdField(ID_FIELD);
fm.setLimitClause(wrapper.getLimit().toString());
fm.setOffsetClause(wrapper.getOffset().toString());
fm.setMainQuery(mainQuery);
fm.setSchema(schemaName);
fm.setCountQuery(queryHelper.countQuery);
return fm;
}
use of org.folio.rest.persist.facets.FacetManager in project raml-module-builder by folio-org.
the class PostgresClient method buildQueryHelper.
QueryHelper buildQueryHelper(String table, String fieldName, CQLWrapper wrapper, boolean returnIdField, List<FacetField> facets, String distinctOn) throws IOException, TemplateException {
if (wrapper == null) {
wrapper = new CQLWrapper();
}
String addIdField = "";
if (returnIdField) {
addIdField = COMMA + ID_FIELD;
}
if (!"null".equals(fieldName) && fieldName.contains("*")) {
// if we are requesting all fields (*) , then dont add the id field to the select
// this will return two id columns which will create ambiguity in facet queries
addIdField = "";
}
QueryHelper queryHelper = new QueryHelper(table);
String countOn = "*";
String distinctOnClause = "";
if (distinctOn != null && !distinctOn.isEmpty()) {
distinctOnClause = String.format("DISTINCT ON(%s) ", distinctOn);
countOn = String.format("DISTINCT(%s)", distinctOn);
}
queryHelper.selectQuery = SELECT + distinctOnClause + fieldName + addIdField + FROM + schemaName + DOT + table + SPACE + wrapper.toString();
queryHelper.countQuery = SELECT + "COUNT(" + countOn + ")" + FROM + schemaName + DOT + table + SPACE + wrapper.getWhereClause();
if (facets != null && !facets.isEmpty()) {
String mainQuery = SELECT + distinctOnClause + fieldName + addIdField + FROM + schemaName + DOT + table + SPACE + wrapper.getWithoutLimOff();
FacetManager facetManager = buildFacetManager(wrapper, queryHelper, mainQuery, facets);
// this method call invokes freemarker templating
queryHelper.selectQuery = facetManager.generateFacetQuery();
}
int offset = wrapper.getOffset().get();
if (offset != -1) {
queryHelper.offset = offset;
}
int limit = wrapper.getLimit().get();
queryHelper.limit = limit != -1 ? limit : Integer.MAX_VALUE;
// with where, but without order by, offset, limit
String query = SELECT + distinctOnClause + fieldName + addIdField + FROM + schemaName + DOT + table + SPACE + wrapper.getWhereClause();
if (limit == 0) {
// calculate exact total count without returning records
queryHelper.countQuery = SELECT + "count(*) FROM (" + query + ") x";
} else if (!wrapper.getWhereClause().isEmpty()) {
// only do estimation when filter is in use (such as CQL).
queryHelper.countQuery = SELECT + schemaName + DOT + "count_estimate('" + query.replace("'", "''") + "')";
}
return queryHelper;
}
Aggregations