Search in sources :

Example 1 with QuerySpecification

use of org.qi4j.api.query.grammar.QuerySpecification in project qi4j-sdk by Qi4j.

the class SolrEntityQueryMixin method findEntities.

@Override
public Iterable<EntityReference> findEntities(Class<?> resultType, @Optional Specification<Composite> whereClause, @Optional OrderBy[] orderBySegments, @Optional Integer firstResult, @Optional Integer maxResults, Map<String, Object> variables) throws EntityFinderException {
    try {
        QuerySpecification expr = (QuerySpecification) whereClause;
        SolrServer server = solr.solrServer();
        NamedList<Object> list = new NamedList<Object>();
        list.add("q", expr.query());
        list.add("rows", maxResults != 0 ? maxResults : 10000);
        list.add("start", firstResult);
        if (orderBySegments != null && orderBySegments.length > 0) {
            for (OrderBy orderBySegment : orderBySegments) {
                String propName = ((Member) orderBySegment.property().accessor()).getName() + "_for_sort";
                String order = orderBySegment.order() == OrderBy.Order.ASCENDING ? "asc" : "desc";
                list.add("sort", propName + " " + order);
            }
        }
        SolrParams solrParams = SolrParams.toSolrParams(list);
        logger.debug("Search:" + list.toString());
        QueryResponse query = server.query(solrParams);
        SolrDocumentList results = query.getResults();
        List<EntityReference> references = new ArrayList<EntityReference>(results.size());
        for (SolrDocument result : results) {
            references.add(EntityReference.parseEntityReference(result.getFirstValue("id").toString()));
        }
        return references;
    } catch (SolrServerException e) {
        throw new EntityFinderException(e);
    }
}
Also used : OrderBy(org.qi4j.api.query.grammar.OrderBy) NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrServer(org.apache.solr.client.solrj.SolrServer) QuerySpecification(org.qi4j.api.query.grammar.QuerySpecification) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) EntityFinderException(org.qi4j.spi.query.EntityFinderException) EntityReference(org.qi4j.api.entity.EntityReference) SolrParams(org.apache.solr.common.params.SolrParams)

Aggregations

ArrayList (java.util.ArrayList)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrParams (org.apache.solr.common.params.SolrParams)1 NamedList (org.apache.solr.common.util.NamedList)1 EntityReference (org.qi4j.api.entity.EntityReference)1 OrderBy (org.qi4j.api.query.grammar.OrderBy)1 QuerySpecification (org.qi4j.api.query.grammar.QuerySpecification)1 EntityFinderException (org.qi4j.spi.query.EntityFinderException)1