List of usage examples for org.apache.solr.client.solrj SolrQuery setQuery
public SolrQuery setQuery(String query)
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 ava 2 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 w ww . ja v a 2 s . c o m*/ 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 . co 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 * //from w w w . ja v a 2 s . c o m * @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 .ja va 2s. com*/ * @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 w w w . j av a 2 s. com*/ 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 w w . j a 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/*from ww w . j av a 2 s.co m*/ * * @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 * /*from www.j av a2 s. c om*/ * @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; }
From source file:edu.usu.sdl.openstorefront.service.SearchServiceImpl.java
License:Apache License
@Override public List<ComponentSearchView> getSearchItems(SearchQuery query, FilterQueryParams filter) { // use for advanced search with And - Or combinations on separate fields String queryOperator = " " + SolrAndOr.OR + " "; String myQueryString;//from ww w.j ava 2s .c om // If incoming query string is blank, default to solar *:* for the full query if (StringUtils.isNotBlank(query.getQuery())) { StringBuilder queryData = new StringBuilder(); Field fields[] = SolrComponentModel.class.getDeclaredFields(); for (Field field : fields) { org.apache.solr.client.solrj.beans.Field fieldAnnotation = field .getAnnotation(org.apache.solr.client.solrj.beans.Field.class); if (fieldAnnotation != null && field.getType() == String.class) { String name = field.getName(); if (StringUtils.isNotBlank(fieldAnnotation.value()) && org.apache.solr.client.solrj.beans.Field.DEFAULT .equals(fieldAnnotation.value()) == false) { name = fieldAnnotation.value(); } queryData.append(SolrEquals.EQUAL.getSolrOperator()).append(name) .append(SolrManager.SOLR_QUERY_SEPERATOR).append(query.getQuery()) .append(queryOperator); } } myQueryString = queryData.toString(); if (myQueryString.endsWith(queryOperator)) { queryData.delete((myQueryString.length() - (queryOperator.length())), myQueryString.length()); myQueryString = queryData.toString(); } } else { myQueryString = SolrManager.SOLR_ALL_QUERY; } log.log(Level.FINER, myQueryString); // execute the searchComponent method and bring back from solr a list array List<SolrComponentModel> resultsList = new ArrayList<>(); try { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(myQueryString); // fields to be returned back from solr solrQuery.setFields(SolrComponentModel.ID_FIELD, SolrComponentModel.ISCOMPONENT_FIELD); solrQuery.setStart(filter.getOffset()); solrQuery.setRows(filter.getMax()); Field sortField = ReflectionUtil.getField(new SolrComponentModel(), filter.getSortField()); if (sortField != null) { String sortFieldText = filter.getSortField(); org.apache.solr.client.solrj.beans.Field fieldAnnotation = sortField .getAnnotation(org.apache.solr.client.solrj.beans.Field.class); if (fieldAnnotation != null) { sortFieldText = fieldAnnotation.value(); } SolrQuery.ORDER order = SolrQuery.ORDER.desc; if (OpenStorefrontConstant.SORT_ASCENDING.equalsIgnoreCase(filter.getSortOrder())) { order = SolrQuery.ORDER.asc; } solrQuery.addSort(sortFieldText, order); } solrQuery.setIncludeScore(true); QueryResponse response = SolrManager.getServer().query(solrQuery); SolrDocumentList results = response.getResults(); DocumentObjectBinder binder = new DocumentObjectBinder(); resultsList = binder.getBeans(SolrComponentModel.class, results); } catch (SolrServerException ex) { throw new OpenStorefrontRuntimeException("Search Failed", "Contact System Admin. Seach server maybe Unavailable", ex); } catch (Exception ex) { log.log(Level.WARNING, "Solr query failed unexpectly; likely bad input.", ex); } //Pulling the full object on the return List<ComponentSearchView> views = new ArrayList<>(); List<String> componentIds = new ArrayList<>(); for (SolrComponentModel result : resultsList) { if (result.getIsComponent()) { componentIds.add(result.getId()); } } //remove bad indexes, if any List<ComponentSearchView> componentSearchViews = getComponentService().getSearchComponentList(componentIds); Set<String> goodComponentIdSet = new HashSet<>(); for (ComponentSearchView view : componentSearchViews) { goodComponentIdSet.add(view.getComponentId()); } for (String componentId : componentIds) { if (goodComponentIdSet.contains(componentId) == false) { log.log(Level.FINE, MessageFormat.format("Removing bad index: {0}", componentId)); deleteById(componentId); } } views.addAll(componentSearchViews); List<ComponentSearchView> articleViews = getAttributeService().getArticlesSearchView(); Map<String, ComponentSearchView> allViews = new HashMap<>(); for (ComponentSearchView componentSearchView : articleViews) { AttributeCodePk attributeCodePk = new AttributeCodePk(); attributeCodePk.setAttributeType(componentSearchView.getArticleAttributeType()); attributeCodePk.setAttributeCode(componentSearchView.getArticleAttributeCode()); allViews.put(attributeCodePk.toKey(), componentSearchView); } for (SolrComponentModel result : resultsList) { if (result.getIsComponent() == false) { ComponentSearchView view = allViews.get(result.getId()); if (view != null) { views.add(view); } else { log.log(Level.FINE, MessageFormat.format("Removing bad index: {0}", result.getId())); deleteById(result.getId()); } } } //TODO: Get the score and sort by score return views; }