Search in sources :

Example 1 with SearchCriteriaFilterBean

use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.

the class AbstractJpaStorage method applySearchCriteriaToQuery.

/**
 * Applies the criteria found in the {@link SearchCriteriaBean} to the JPA query.
 * @param criteria
 * @param builder
 * @param query
 * @param from
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> void applySearchCriteriaToQuery(SearchCriteriaBean criteria, CriteriaBuilder builder, CriteriaQuery<?> query, Root<T> from, boolean countOnly) {
    List<SearchCriteriaFilterBean> filters = criteria.getFilters();
    if (filters != null && !filters.isEmpty()) {
        List<Predicate> predicates = new ArrayList<>();
        for (SearchCriteriaFilterBean filter : filters) {
            if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
                Path<Object> path = from.get(filter.getName());
                Class<?> pathc = path.getJavaType();
                if (pathc.isAssignableFrom(String.class)) {
                    predicates.add(builder.equal(path, filter.getValue()));
                } else if (pathc.isEnum()) {
                    predicates.add(builder.equal(path, Enum.valueOf((Class) pathc, filter.getValue())));
                }
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
                predicates.add(builder.equal(from.<Boolean>get(filter.getName()), Boolean.valueOf(filter.getValue())));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.gt) {
                predicates.add(builder.greaterThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.gte) {
                predicates.add(builder.greaterThanOrEqualTo(from.<Long>get(filter.getName()), new Long(filter.getValue())));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.lt) {
                predicates.add(builder.lessThan(from.<Long>get(filter.getName()), new Long(filter.getValue())));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.lte) {
                predicates.add(builder.lessThanOrEqualTo(from.<Long>get(filter.getName()), new Long(filter.getValue())));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.neq) {
                predicates.add(builder.notEqual(from.get(filter.getName()), filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
                predicates.add(builder.like(builder.upper(from.<String>get(filter.getName())), filter.getValue().toUpperCase().replace('*', '%')));
            }
        }
        query.where(predicates.toArray(new Predicate[predicates.size()]));
    }
    OrderByBean orderBy = criteria.getOrderBy();
    if (orderBy != null && !countOnly) {
        if (orderBy.isAscending()) {
            query.orderBy(builder.asc(from.get(orderBy.getName())));
        } else {
            query.orderBy(builder.desc(from.get(orderBy.getName())));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean) Predicate(javax.persistence.criteria.Predicate)

Example 2 with SearchCriteriaFilterBean

use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.

the class EsStorage method find.

/**
 * Finds entities using a generic search criteria bean.
 * @param criteria
 * @param type
 * @param unmarshaller
 * @throws StorageException
 */
private <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, String type, IUnmarshaller<T> unmarshaller) throws StorageException {
    try {
        SearchResultsBean<T> rval = new SearchResultsBean<>();
        // Set some default in the case that paging information was not included in the request.
        PagingBean paging = criteria.getPaging();
        if (paging == null) {
            paging = new PagingBean();
            paging.setPage(1);
            paging.setPageSize(20);
        }
        int page = paging.getPage();
        int pageSize = paging.getPageSize();
        int start = (page - 1) * pageSize;
        SearchSourceBuilder builder = new SearchSourceBuilder().size(pageSize).from(start).fetchSource(true);
        // Sort order
        OrderByBean orderBy = criteria.getOrderBy();
        if (orderBy != null) {
            String name = orderBy.getName();
            // Get the index definition so that we can see whether a '.keyword' is available. If so, use it.
            EsIndexProperties esIndex = getEsIndices().get(type);
            if (esIndex.hasProperty(name) && esIndex.getProperty(name).isKeywordMultiField()) {
                name = name + ".keyword";
            }
            if (orderBy.isAscending()) {
                builder.sort(name, SortOrder.ASC);
            } else {
                builder.sort(name, SortOrder.DESC);
            }
        }
        // Now process the filter criteria
        List<SearchCriteriaFilterBean> filters = criteria.getFilters();
        QueryBuilder q = null;
        if (filters != null && !filters.isEmpty()) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            List<QueryBuilder> andFilter = boolQuery.filter();
            int filterCount = 0;
            for (SearchCriteriaFilterBean filter : filters) {
                String propertyName = filter.getName();
                if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
                    andFilter.add(QueryBuilders.termQuery(propertyName, filter.getValue()));
                    filterCount++;
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
                    q = QueryBuilders.wildcardQuery(propertyName, filter.getValue().toLowerCase().replace('%', '*'));
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
                    // $NON-NLS-1$
                    andFilter.add(QueryBuilders.termQuery(propertyName, "true".equals(filter.getValue())));
                    filterCount++;
                }
            // TODO implement the other filter operators here!
            }
            if (filterCount > 0) {
                q = boolQuery;
            }
        }
        builder.query(q);
        String fullIndexName = getFullIndexName(type);
        SearchResponse response = getClient().search(new SearchRequest(fullIndexName).source(builder), RequestOptions.DEFAULT);
        SearchHits thehits = response.getHits();
        rval.setTotalSize((int) thehits.getTotalHits().value);
        for (SearchHit hit : thehits.getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            T bean = unmarshaller.unmarshal(sourceAsMap);
            rval.getBeans().add(bean);
        }
        return rval;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) INDEX_MANAGER_POSTFIX_CONTRACT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CONTRACT) INDEX_MANAGER_POSTFIX_CLIENT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CLIENT) PagingBean(io.apiman.manager.api.beans.search.PagingBean) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResultsBean(io.apiman.manager.api.beans.search.SearchResultsBean) SearchHits(org.elasticsearch.search.SearchHits) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean) SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 3 with SearchCriteriaFilterBean

use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.

the class NotificationRepositoryImpl method searchNotificationsByUser.

@Override
public SearchResultsBean<NotificationEntity> searchNotificationsByUser(String recipientUserId, SearchCriteriaBean searchCriteria) throws StorageException {
    var recipientFilter = new SearchCriteriaFilterBean().setName("recipient").setOperator(SearchCriteriaFilterOperator.eq).setValue(recipientUserId);
    searchCriteria.getFilters().add(recipientFilter);
    return super.find(searchCriteria, List.of(new OrderByBean(true, NotificationEntity_.ID)), NotificationEntity.class, true);
}
Also used : SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean)

Example 4 with SearchCriteriaFilterBean

use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.

the class NotificationRepositoryImpl method getLatestNotificationsByRecipientId.

@Override
public SearchResultsBean<NotificationEntity> getLatestNotificationsByRecipientId(String recipientUserId, @Nullable PagingBean paging) throws StorageException {
    var filter = new SearchCriteriaFilterBean().setName("recipient").setOperator(SearchCriteriaFilterOperator.eq).setValue(recipientUserId);
    var searchCriteria = new SearchCriteriaBean().setFilters(List.of(filter)).setPaging(paging).setOrder("id", false);
    return super.find(searchCriteria, List.of(new OrderByBean(true, NotificationEntity_.ID)), NotificationEntity.class, true);
}
Also used : SearchCriteriaBean(io.apiman.manager.api.beans.search.SearchCriteriaBean) SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean)

Example 5 with SearchCriteriaFilterBean

use of io.apiman.manager.api.beans.search.SearchCriteriaFilterBean in project apiman by apiman.

the class AbstractJpaStorage method applySearchCriteriaToQuery.

/**
 * Applies the criteria found in the {@link SearchCriteriaBean} to the JPA query.
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> CriteriaBuilder<T> applySearchCriteriaToQuery(String rootAlias, SearchCriteriaBean criteria, CriteriaBuilder<T> cb, boolean countOnly) {
    List<SearchCriteriaFilterBean> filters = criteria.getFilters();
    if (filters != null && !filters.isEmpty()) {
        for (SearchCriteriaFilterBean filter : filters) {
            final String name = cb.getPath(filter.getName()).getPath();
            if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
                com.blazebit.persistence.Path path = cb.getPath(filter.getName());
                Class<?> pathKlazz = path.getJavaType();
                if (pathKlazz.isEnum()) {
                    cb = cb.where(name).eq(Enum.valueOf((Class) pathKlazz, filter.getValue()));
                } else {
                    cb = cb.where(name).eq(filter.getValue());
                }
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
                cb = cb.where(name).eq(Boolean.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.gt) {
                cb = cb.where(name).gt(Long.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.gte) {
                cb = cb.where(name).ge(Long.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.lt) {
                cb = cb.where(name).lt(Long.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.lte) {
                cb = cb.where(name).le(Long.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.neq) {
                cb = cb.where(name).notEq(Long.valueOf(filter.getValue()));
            } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
                cb = cb.where(name).like(false).value(filter.getValue().toUpperCase().replace('*', '%')).noEscape();
            }
        }
    }
    OrderByBean orderBy = criteria.getOrderBy();
    if (orderBy != null && !countOnly) {
        if (orderBy.isAscending()) {
            cb = cb.orderByAsc(orderBy.getName());
        } else {
            cb = cb.orderByDesc(orderBy.getName());
        }
    }
    return cb;
}
Also used : SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean)

Aggregations

SearchCriteriaFilterBean (io.apiman.manager.api.beans.search.SearchCriteriaFilterBean)6 OrderByBean (io.apiman.manager.api.beans.search.OrderByBean)5 PagingBean (io.apiman.manager.api.beans.search.PagingBean)2 SearchResultsBean (io.apiman.manager.api.beans.search.SearchResultsBean)2 ArrayList (java.util.ArrayList)2 INDEX_MANAGER_POSTFIX_CLIENT (io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CLIENT)1 INDEX_MANAGER_POSTFIX_CONTRACT (io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CONTRACT)1 EsIndexProperties (io.apiman.common.es.util.builder.index.EsIndexProperties)1 SearchCriteriaBean (io.apiman.manager.api.beans.search.SearchCriteriaBean)1 AvailableApiBean (io.apiman.manager.api.beans.summary.AvailableApiBean)1 StorageException (io.apiman.manager.api.core.exceptions.StorageException)1 IOException (java.io.IOException)1 Predicate (javax.persistence.criteria.Predicate)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1