Example usage for org.apache.solr.client.solrj SolrQuery SolrQuery

List of usage examples for org.apache.solr.client.solrj SolrQuery SolrQuery

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery SolrQuery.

Prototype

public SolrQuery() 

Source Link

Usage

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Retrieves metadata fields for the parent collection pids contained by the supplied facet object.
 * /*from w  w  w . j av  a2 s  .c o m*/
 * @param parentCollectionFacet
 *           Facet object containing parent collection ids to lookup
 * @param accessGroups
 * @return
 */
public List<BriefObjectMetadataBean> getParentCollectionValues(FacetFieldObject parentCollectionFacet) {
    if (parentCollectionFacet == null || parentCollectionFacet.getValues() == null
            || parentCollectionFacet.getValues().size() == 0) {
        return null;
    }

    QueryResponse queryResponse = null;
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder query = new StringBuilder();
    boolean first = true;

    query.append('(');
    for (GenericFacet pidFacet : parentCollectionFacet.getValues()) {
        if (pidFacet.getSearchValue() != null && pidFacet.getSearchValue().length() > 0) {
            if (first) {
                first = false;
            } else {
                query.append(" OR ");
            }
            query.append(solrSettings.getFieldName(SearchFieldKeys.ID.name())).append(':')
                    .append(SolrSettings.sanitize(pidFacet.getSearchValue()));
        }
    }
    query.append(')');

    // If no pids were added to the query, then there's nothing to look up
    if (first) {
        return null;
    }

    try {
        // Add access restrictions to query
        addAccessRestrictions(query);
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.error("No access groups", e);
        return null;
    }

    solrQuery.setQuery(query.toString());

    solrQuery.setFacet(true);
    solrQuery.setFields(solrSettings.getFieldName(SearchFieldKeys.ID.name()),
            solrSettings.getFieldName(SearchFieldKeys.ANCESTOR_PATH.name()),
            solrSettings.getFieldName(SearchFieldKeys.TITLE.name()));

    solrQuery.setRows(parentCollectionFacet.getValues().size());

    try {
        queryResponse = this.executeQuery(solrQuery);
        return queryResponse.getBeans(BriefObjectMetadataBean.class);
    } catch (SolrServerException e) {
        LOG.error("Failed to execute query " + solrQuery.toString(), e);
    }
    return null;
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Retrieves a list of the nearest windowSize neighbors within the nearest parent collection or folder around the
 * item metadata, based on the order field of the item. The first windowSize - 1 neighbors are retrieved to each side
 * of the item, and trimmed so that there are always windowSize - 1 neighbors surrounding the item if possible. If no
 * order field is available, a list of arbitrary windowSize neighbors is returned.
 * /*from  w w w . j  a v  a2  s  .  c  om*/
 * @param metadata
 *           Record which the window pivots around.
 * @param windowSize
 *           max number of items in the window. This includes the pivot, so odd numbers are recommended.
 * @param accessGroups
 *           Access groups of the user making this request.
 * @return
 */
public List<BriefObjectMetadataBean> getNeighboringItems(BriefObjectMetadataBean metadata, int windowSize,
        AccessGroupSet accessGroups) {

    // Get the common access restriction clause (starts with "AND ...")

    StringBuilder accessRestrictionClause = new StringBuilder();

    try {
        addAccessRestrictions(accessRestrictionClause, accessGroups);
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.error(e.getMessage());
        return null;
    }

    // Prepare the common query object, including a filter for resource type and the
    // facet which selects only the item's siblings.

    SolrQuery solrQuery = new SolrQuery();

    solrQuery.setFacet(true);
    solrQuery.addFilterQuery(solrSettings.getFieldName(SearchFieldKeys.RESOURCE_TYPE.name()) + ":File "
            + solrSettings.getFieldName(SearchFieldKeys.RESOURCE_TYPE.name()) + ":Aggregate");

    CutoffFacet ancestorPath = null;

    if (metadata.getResourceType().equals(searchSettings.resourceTypeFile)
            || metadata.getResourceType().equals(searchSettings.resourceTypeAggregate)) {
        ancestorPath = metadata.getAncestorPathFacet();
    } else {
        ancestorPath = metadata.getPath();
    }

    if (ancestorPath != null) {
        // We want only objects at the same level of the hierarchy
        ancestorPath.setCutoff(ancestorPath.getHighestTier() + 1);

        facetFieldUtil.addToSolrQuery(ancestorPath, solrQuery);
    }

    // If this item has no display order, get arbitrary items surrounding it.

    Long pivotOrder = metadata.getDisplayOrder();

    if (pivotOrder == null) {

        LOG.debug("No display order, just querying for " + windowSize + " siblings");

        StringBuilder query = new StringBuilder();

        List<BriefObjectMetadataBean> list = null;

        query.append("*:*");
        query.append(accessRestrictionClause);
        solrQuery.setQuery(query.toString());

        solrQuery.setStart(0);
        solrQuery.setRows(windowSize);

        solrQuery.setSort(solrSettings.getFieldName(SearchFieldKeys.DISPLAY_ORDER.name()),
                SolrQuery.ORDER.desc);

        try {
            QueryResponse queryResponse = this.executeQuery(solrQuery);
            list = queryResponse.getBeans(BriefObjectMetadataBean.class);
        } catch (SolrServerException e) {
            LOG.error("Error retrieving Neighboring items: " + e);
            return null;
        }

        return list;

        // Otherwise, query for items surrounding this item.

    } else {

        LOG.debug("Display order is " + pivotOrder);

        // Find the right and left lists

        StringBuilder query;

        List<BriefObjectMetadataBean> leftList = null;
        List<BriefObjectMetadataBean> rightList = null;

        solrQuery.setStart(0);
        solrQuery.setRows(windowSize - 1);

        // Right list

        query = new StringBuilder();

        query.append(solrSettings.getFieldName(SearchFieldKeys.DISPLAY_ORDER.name())).append(":[")
                .append(pivotOrder + 1).append(" TO *]");
        query.append(accessRestrictionClause);
        solrQuery.setQuery(query.toString());

        solrQuery.setSort(solrSettings.getFieldName(SearchFieldKeys.DISPLAY_ORDER.name()), SolrQuery.ORDER.asc);

        try {
            QueryResponse queryResponse = this.executeQuery(solrQuery);
            rightList = queryResponse.getBeans(BriefObjectMetadataBean.class);
        } catch (SolrServerException e) {
            LOG.error("Error retrieving Neighboring items: " + e);
            return null;
        }

        LOG.debug("Got " + rightList.size() + " items for right list");

        // Left list

        // (Note that display order stuff is reversed.)

        query = new StringBuilder();

        query.append(solrSettings.getFieldName(SearchFieldKeys.DISPLAY_ORDER.name())).append(":[* TO ")
                .append(pivotOrder - 1).append("]");
        query.append(accessRestrictionClause);
        solrQuery.setQuery(query.toString());

        solrQuery.setSort(solrSettings.getFieldName(SearchFieldKeys.DISPLAY_ORDER.name()),
                SolrQuery.ORDER.desc);

        try {
            QueryResponse queryResponse = this.executeQuery(solrQuery);
            leftList = queryResponse.getBeans(BriefObjectMetadataBean.class);
        } catch (SolrServerException e) {
            LOG.error("Error retrieving Neighboring items: " + e);
            return null;
        }

        LOG.debug("Got " + leftList.size() + " items for left list");

        // Trim the lists

        int halfWindow = windowSize / 2;

        // If we have enough in both lists, trim both to be
        // halfWindow long.

        if (leftList.size() >= halfWindow && rightList.size() >= halfWindow) {

            LOG.debug("Trimming both lists");

            leftList.subList(halfWindow, leftList.size()).clear();
            rightList.subList(halfWindow, rightList.size()).clear();

            // If we don't have enough in the left list and we have extra in the right list,
            // try to pick up the slack by trimming fewer items from the right list.

        } else if (leftList.size() < halfWindow && rightList.size() > halfWindow) {

            LOG.debug("Picking up slack from right list");

            // How much extra do we need from the right list?

            int extra = halfWindow - leftList.size();

            // Only "take" the extra (ie, clear less of the right list) if we have it available.

            if (halfWindow + extra < rightList.size())
                rightList.subList(halfWindow + extra, rightList.size()).clear();

        } else if (rightList.size() < halfWindow && leftList.size() > halfWindow) {

            LOG.debug("Picking up slack from left list");

            int extra = halfWindow - rightList.size();

            if (halfWindow + extra < leftList.size())
                leftList.subList(halfWindow + extra, leftList.size()).clear();

        }

        // (Otherwise, we do no trimming, since both lists are smaller or the same size
        // as the window.)

        // Assemble the result.

        Collections.reverse(leftList);
        leftList.add(metadata);
        leftList.addAll(rightList);

        return leftList;

    }

}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

public long getChildrenCount(BriefObjectMetadataBean metadataObject, AccessGroupSet accessGroups) {
    QueryResponse queryResponse = null;/*from   ww  w .  j  a v a  2  s . c  om*/
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder query = new StringBuilder("*:* ");

    try {
        // Add access restrictions to query
        addAccessRestrictions(query, accessGroups);
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.error(e.getMessage());
        return -1;
    }

    solrQuery.setStart(0);
    solrQuery.setRows(0);

    solrQuery.setQuery(query.toString());

    query = new StringBuilder();
    query.append(solrSettings.getFieldName(SearchFieldKeys.ANCESTOR_PATH.name())).append(':')
            .append(SolrSettings.sanitize(metadataObject.getPath().getSearchValue())).append(",*");

    solrQuery.setFacet(true);
    solrQuery.addFilterQuery(query.toString());

    try {
        queryResponse = this.executeQuery(solrQuery);
        return queryResponse.getResults().getNumFound();
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr search result request", e);
    }
    return -1;
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

public void getChildrenCounts(List<BriefObjectMetadata> resultList, AccessGroupSet accessGroups,
        String countName, String queryAddendum, SolrQuery baseQuery) {
    long startTime = System.currentTimeMillis();
    if (resultList == null || resultList.size() == 0)
        return;//from   w  w  w.  j  a v  a 2  s  .  c  o  m

    String ancestorPathField = solrSettings.getFieldName(SearchFieldKeys.ANCESTOR_PATH.name());
    SolrQuery solrQuery;
    if (baseQuery == null) {
        // Create a base query since we didn't receive one
        solrQuery = new SolrQuery();
        StringBuilder query = new StringBuilder("*:*");
        try {
            // Add access restrictions to query
            addAccessRestrictions(query, accessGroups);
        } catch (AccessRestrictionException e) {
            // If the user doesn't have any access groups, they don't have access to anything, return null.
            LOG.error(e.getMessage());
            return;
        }

        solrQuery.setStart(0);
        solrQuery.setRows(0);

        solrQuery.setQuery(query.toString());
    } else {
        // Starting from a base query
        solrQuery = baseQuery.getCopy();
        // Make sure we aren't returning any normal results
        solrQuery.setRows(0);
        // Remove all facet fields so we are only getting ancestor path
        if (solrQuery.getFacetFields() != null) {
            for (String facetField : solrQuery.getFacetFields()) {
                solrQuery.removeFacetField(facetField);
            }
        }
    }

    if (queryAddendum != null) {
        solrQuery.setQuery(solrQuery.getQuery() + " AND " + queryAddendum);
    }

    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(1);
    solrQuery.addFacetField(ancestorPathField);

    Integer countPageSize;
    try {
        countPageSize = new Integer(searchSettings.getProperty("search.facet.countPageSize"));
    } catch (NumberFormatException e) {
        countPageSize = 20;
    }

    solrQuery.add("f." + ancestorPathField + ".facet.limit", Integer.toString(Integer.MAX_VALUE));
    // Sort by value rather than count so that earlier tiers will come first in case the result gets cut off
    solrQuery.setFacetSort("index");

    java.util.Map<Integer, StringBuilder> tierQueryMap = new java.util.HashMap<Integer, StringBuilder>();
    java.util.Map<Integer, List<BriefObjectMetadata>> containerMap = new java.util.HashMap<Integer, List<BriefObjectMetadata>>();

    // Pare the list of ids we are searching for and assigning counts to down to just containers
    for (BriefObjectMetadata metadataObject : resultList) {
        if (metadataObject.getPath() != null && metadataObject.getContentModel() != null
                && metadataObject.getContentModel().contains(ContentModelHelper.Model.CONTAINER.toString())) {
            CutoffFacetNode highestTier = metadataObject.getPath().getHighestTierNode();
            StringBuilder tierQuery = tierQueryMap.get(highestTier.getTier());
            List<BriefObjectMetadata> containerObjects = containerMap.get(highestTier.getTier());
            if (tierQuery == null) {
                tierQuery = new StringBuilder();
                tierQueryMap.put(highestTier.getTier(), tierQuery);

                containerObjects = new ArrayList<BriefObjectMetadata>();
                containerMap.put(highestTier.getTier(), containerObjects);
            }

            if (tierQuery.length() == 0) {
                tierQuery.append(ancestorPathField).append(":(");
            } else {
                tierQuery.append(" OR ");
            }

            tierQuery.append(SolrSettings.sanitize(highestTier.getSearchValue())).append(",*");
            containerObjects.add(metadataObject);

            // If there are a lot of results, then do a partial lookup
            if (containerObjects.size() >= countPageSize) {
                tierQuery.append(")");
                this.executeChildrenCounts(tierQuery, containerObjects, solrQuery, countName,
                        highestTier.getTier());
                LOG.info("Partial query done at " + System.currentTimeMillis() + " ("
                        + (System.currentTimeMillis() - startTime) + ")");
                containerMap.remove(highestTier.getTier());
                tierQueryMap.remove(highestTier.getTier());
            }
        }
    }

    Iterator<java.util.Map.Entry<Integer, StringBuilder>> queryIt = tierQueryMap.entrySet().iterator();
    while (queryIt.hasNext()) {
        java.util.Map.Entry<Integer, StringBuilder> tierQueryEntry = queryIt.next();
        tierQueryEntry.getValue().append(')');
        this.executeChildrenCounts(tierQueryEntry.getValue(), containerMap.get(tierQueryEntry.getKey()),
                solrQuery, countName, tierQueryEntry.getKey());
    }
    LOG.info("Child count query done at " + System.currentTimeMillis() + " ("
            + (System.currentTimeMillis() - startTime) + ")");
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Checks if an item is accessible given the specified access restrictions
 * /* ww w. j  a va2s  .  c om*/
 * @param idRequest
 * @param accessType
 * @return
 */
public boolean isAccessible(SimpleIdRequest idRequest) {
    QueryResponse queryResponse = null;
    SolrQuery solrQuery = new SolrQuery();
    StringBuilder query = new StringBuilder();

    PID pid = new PID(idRequest.getId());
    String id = pid.getPid();
    String[] idParts = id.split("/");
    String datastream = null;
    if (idParts.length > 1) {
        id = idParts[0];
        datastream = idParts[1];
        solrQuery.addField(solrSettings.getFieldName(SearchFieldKeys.ROLE_GROUP.name()));
    }

    query.append(solrSettings.getFieldName(SearchFieldKeys.ID.name())).append(':')
            .append(SolrSettings.sanitize(id));

    try {
        // Add access restrictions to query
        addAccessRestrictions(query, idRequest.getAccessGroups());
    } catch (AccessRestrictionException e) {
        // If the user doesn't have any access groups, they don't have access to anything, return null.
        LOG.error(e.getMessage());
        return false;
    }

    solrQuery.setQuery(query.toString());
    if (datastream == null)
        solrQuery.setRows(0);
    else
        solrQuery.setRows(1);

    solrQuery.addField(solrSettings.getFieldName(SearchFieldKeys.ID.name()));

    LOG.debug("getObjectById query: " + solrQuery.toString());
    try {
        queryResponse = this.executeQuery(solrQuery);
        if (queryResponse.getResults().getNumFound() == 0)
            return false;
        if (datastream == null)
            return true;

        List<BriefObjectMetadataBean> results = queryResponse.getBeans(BriefObjectMetadataBean.class);
        BriefObjectMetadataBean metadata = results.get(0);

        return AccessUtil.permitDatastreamAccess(idRequest.getAccessGroups(), datastream, metadata);
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }

    return false;
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Determines if the user has adminRole permissions on any items
 * //  w w w.j  av a 2s  . c o m
 * @param accessGroups
 * @return
 */
public boolean hasAdminViewPermission(AccessGroupSet accessGroups) {
    if (accessGroups.contains(AccessGroupConstants.ADMIN_GROUP)) {
        return true;
    }
    StringBuilder query = new StringBuilder();
    String joinedGroups = accessGroups.joinAccessGroups(" OR ", null, true);
    query.append("adminGroup:(").append(joinedGroups).append(')');

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(query.toString());
    solrQuery.setRows(0);

    try {
        QueryResponse queryResponse = this.executeQuery(solrQuery);
        return queryResponse.getResults().getNumFound() > 0;
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }
    return false;
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

public boolean hasRole(AccessGroupSet accessGroups, UserRole userRole) {
    StringBuilder query = new StringBuilder();
    String joinedGroups = accessGroups.joinAccessGroups(" OR ", userRole.toString() + "|", true);
    query.append("roleGroup:(").append(joinedGroups).append(')');

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(query.toString());
    solrQuery.setRows(0);/*from  ww w .j a  va 2  s  . c o  m*/

    try {
        QueryResponse queryResponse = this.executeQuery(solrQuery);
        return queryResponse.getResults().getNumFound() > 0;
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }
    return false;
}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Get the number of departments represented in the collection
 * //from w ww.ja v a 2  s  .co m
 * @return the count, or -1 if there was an error retrieving the count
 */

public int getDepartmentsCount() {

    SolrQuery query;
    QueryResponse response;

    query = new SolrQuery();
    query.setQuery("*:*");
    query.setRows(0);
    query.addFacetField("department");
    query.setFacetLimit(-1);

    try {
        response = this.executeQuery(query);
        return response.getFacetField("department").getValueCount();
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }

    return -1;

}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Get the total number of collections//w  w w . ja v  a 2s .  com
 * 
 * @return the count, or -1 if there was an error retrieving the count
 */

public long getCollectionsCount() {

    SolrQuery query;
    QueryResponse response;

    query = new SolrQuery();
    query.setQuery("resourceType:Collection");
    query.setRows(0);
    query.setFacetLimit(-1);

    try {
        response = this.executeQuery(query);
        return response.getResults().getNumFound();
    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }

    return -1;

}

From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java

License:Apache License

/**
 * Get the number of objects present in the collection for various formats
 * // w  w w. j  a v  a 2s  .c  o  m
 * @return a map from format name to count
 */

public Map<String, Long> getFormatCounts() {

    SolrQuery query;
    QueryResponse response;

    query = new SolrQuery();
    query.setQuery("*:*");
    query.setRows(0);
    query.addFacetField("contentType");
    query.setFacetLimit(-1);

    HashMap<String, Long> counts = new HashMap<String, Long>();

    try {
        response = this.executeQuery(query);
        FacetField facetField = response.getFacetField("contentType");

        for (Count count : facetField.getValues()) {

            if (count.getName().startsWith("^text"))
                counts.put("text", count.getCount());
            else if (count.getName().startsWith("^image"))
                counts.put("image", count.getCount());
            else if (count.getName().startsWith("^dataset"))
                counts.put("dataset", count.getCount());
            else if (count.getName().startsWith("^audio"))
                counts.put("audio", count.getCount());
            else if (count.getName().startsWith("^video"))
                counts.put("video", count.getCount());

        }

    } catch (SolrServerException e) {
        LOG.error("Error retrieving Solr object request: " + e);
    }

    return counts;

}