List of usage examples for org.apache.solr.common.params GroupParams GROUP
String GROUP
To view the source code for org.apache.solr.common.params GroupParams GROUP.
Click Source Link
From source file:at.pagu.soldockr.core.QueryParser.java
License:Apache License
private void appendGroupByFields(SolrQuery solrQuery, List<Field> fields) { if (CollectionUtils.isEmpty(fields)) { return;/* w ww .j ava2s . com*/ } if (fields.size() > 1) { // there is a bug in solj which prevents multiple grouping // although available via HTTP call throw new ApiUsageException( "Cannot group on more than one field with current SolrJ API. Group on single field insead"); } solrQuery.set(GroupParams.GROUP, true); solrQuery.setParam(GroupParams.GROUP_MAIN, true); for (Field field : fields) { solrQuery.add(GroupParams.GROUP_FIELD, field.getName()); } }
From source file:at.pagu.soldockr.core.QueryParserTest.java
License:Apache License
private void assertGroupingNotPresent(SolrQuery solrQuery) { Assert.assertNull(solrQuery.get(GroupParams.GROUP)); Assert.assertNull(solrQuery.get(GroupParams.GROUP_FIELD)); Assert.assertNull(solrQuery.get(GroupParams.GROUP_MAIN)); }
From source file:at.pagu.soldockr.core.QueryParserTest.java
License:Apache License
private void assertGroupingPresent(SolrQuery solrQuery, String expected) { Assert.assertNotNull(solrQuery.get(GroupParams.GROUP)); Assert.assertNotNull(solrQuery.get(GroupParams.GROUP_FIELD)); Assert.assertNotNull(solrQuery.get(GroupParams.GROUP_MAIN)); Assert.assertEquals(expected, solrQuery.get(GroupParams.GROUP_FIELD)); }
From source file:com.frank.search.solr.core.DefaultQueryParser.java
License:Apache License
private void processGroupOptions(SolrQuery solrQuery, Query query) { GroupOptions groupOptions = query.getGroupOptions(); if (groupOptions == null || (CollectionUtils.isEmpty(groupOptions.getGroupByFields()) && CollectionUtils.isEmpty(groupOptions.getGroupByFunctions()) && CollectionUtils.isEmpty(groupOptions.getGroupByQueries()))) { return;/*w w w . ja v a 2 s . c o m*/ } solrQuery.set(GroupParams.GROUP, true); solrQuery.set(GroupParams.GROUP_MAIN, groupOptions.isGroupMain()); solrQuery.set(GroupParams.GROUP_FORMAT, "grouped"); if (!CollectionUtils.isEmpty(groupOptions.getGroupByFields())) { for (Field field : groupOptions.getGroupByFields()) { solrQuery.add(GroupParams.GROUP_FIELD, field.getName()); } } if (!CollectionUtils.isEmpty(groupOptions.getGroupByFunctions())) { for (Function function : groupOptions.getGroupByFunctions()) { String functionFragment = createFunctionFragment(function, 0); setObjectNameOnGroupQuery(query, function, functionFragment); solrQuery.add(GroupParams.GROUP_FUNC, functionFragment); } } if (!CollectionUtils.isEmpty(groupOptions.getGroupByQueries())) { for (Query groupQuery : groupOptions.getGroupByQueries()) { String queryFragment = getQueryString(groupQuery); setObjectNameOnGroupQuery(query, groupQuery, queryFragment); solrQuery.add(GroupParams.GROUP_QUERY, queryFragment); } } if (groupOptions.getSort() != null) { for (Order order : groupOptions.getSort()) { solrQuery.add(GroupParams.GROUP_SORT, order.getProperty().trim() + " " + (order.isAscending() ? ORDER.asc : ORDER.desc)); } } if (groupOptions.getCachePercent() > 0) { solrQuery.add(GroupParams.GROUP_CACHE_PERCENTAGE, String.valueOf(groupOptions.getCachePercent())); } if (groupOptions.getLimit() != null && groupOptions.getLimit() >= 0) { solrQuery.set(GroupParams.GROUP_LIMIT, groupOptions.getLimit()); } if (groupOptions.getOffset() != null && groupOptions.getOffset() >= 0) { solrQuery.set(GroupParams.GROUP_OFFSET, groupOptions.getOffset()); } solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, groupOptions.isTotalCount()); solrQuery.set(GroupParams.GROUP_FACET, groupOptions.isGroupFacets()); solrQuery.set(GroupParams.GROUP_TRUNCATE, groupOptions.isTruncateFacets()); }
From source file:com.yaotrue.learn.solr.SolrjTest.java
License:Apache License
@Test public void testGroup() throws SolrServerException, IOException { SolrQuery solrQuery = new SolrQuery("*:*"); solrQuery.set(GroupParams.GROUP, true); solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true); solrQuery.set(GroupParams.GROUP_LIMIT, 12); solrQuery.set(GroupParams.GROUP_FORMAT, "grouped"); solrQuery.set(GroupParams.GROUP_FACET, true); solrQuery.set(GroupParams.GROUP_FIELD, "style"); QueryResponse queryResponse = solrClient.query(solrQuery); GroupResponse groupResponse = queryResponse.getGroupResponse(); if (null != groupResponse) { List<GroupCommand> groupCommandList = groupResponse.getValues(); for (GroupCommand groupCommand : groupCommandList) { int matches = groupCommand.getMatches(); String name = groupCommand.getName(); Integer ngroups = groupCommand.getNGroups(); List<Group> groupList = groupCommand.getValues(); for (Group group : groupList) { SolrDocumentList solrDocumentList = group.getResult(); ListIterator<SolrDocument> listIterator = solrDocumentList.listIterator(); while (listIterator.hasNext()) { SolrDocument solrDocument = listIterator.next(); System.out.println(solrDocument.get("code")); }/*from w ww .j a va 2 s . c o m*/ } } } }
From source file:edu.unc.lib.dl.search.solr.service.SolrSearchService.java
License:Apache License
/** * Constructs a SolrQuery object from the search state specified within a SearchRequest object. The request may * optionally request to retrieve facet results in addition to search results. * //from w w w. j av a2s . com * @param searchRequest * @param isRetrieveFacetsRequest * @return */ protected SolrQuery generateSearch(SearchRequest searchRequest) { SearchState searchState = (SearchState) searchRequest.getSearchState(); SolrQuery solrQuery = new SolrQuery(); StringBuilder termQuery = new StringBuilder(); // Generate search term query string addSearchFields(searchState, termQuery); // Add range Fields to the query addRangeFields(searchState, termQuery); // No query terms given, make it an everything query StringBuilder query = new StringBuilder(); if (termQuery.length() == 0) { query.append("*:* "); } else { query.append('(').append(termQuery).append(')'); } // Add access restrictions to query try { addAccessRestrictions(query, searchRequest.getAccessGroups()); } catch (AccessRestrictionException e) { // If the user doesn't have any access groups, they don't have access to anything, return null. LOG.debug("User had no access groups", e); return null; } // Add query solrQuery.setQuery(query.toString()); if (searchState.getResultFields() != null) { for (String field : searchState.getResultFields()) { String solrFieldName = solrSettings.getFieldName(field); if (solrFieldName != null) solrQuery.addField(solrFieldName); } } if (searchState.getRollup() != null && searchState.getRollup()) { solrQuery.set(GroupParams.GROUP, true); if (searchState.getRollupField() == null) solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(SearchFieldKeys.ROLLUP_ID.name())); else solrQuery.set(GroupParams.GROUP_FIELD, solrSettings.getFieldName(searchState.getRollupField())); solrQuery.set(GroupParams.GROUP_TOTAL_COUNT, true); if (searchState.getFacetsToRetrieve() != null && searchState.getFacetsToRetrieve().size() > 0) { solrQuery.set(GroupParams.GROUP_FACET, true); } } // Add sort parameters List<SearchSettings.SortField> sortFields = searchSettings.sortTypes.get(searchState.getSortType()); if (sortFields != null) { for (int i = 0; i < sortFields.size(); i++) { SearchSettings.SortField sortField = sortFields.get(i); SolrQuery.ORDER sortOrder = SolrQuery.ORDER.valueOf(sortField.getSortOrder()); if (!searchState.getSortNormalOrder()) sortOrder = sortOrder.reverse(); solrQuery.addSort(solrSettings.getFieldName(sortField.getFieldName()), sortOrder); } } // Set requested resource types String resourceTypeFilter = this.getResourceTypeFilter(searchState.getResourceTypes()); if (resourceTypeFilter != null) { solrQuery.addFilterQuery(resourceTypeFilter); } // Turn on faceting if (searchRequest.isRetrieveFacets()) { solrQuery.setFacet(true); solrQuery.setFacetMinCount(1); if (searchState.getBaseFacetLimit() != null) solrQuery.setFacetLimit(searchState.getBaseFacetLimit()); if (searchState.getFacetsToRetrieve() != null) { // Add facet fields for (String facetName : searchState.getFacetsToRetrieve()) { String facetField = solrSettings.getFieldName(facetName); if (facetField != null) solrQuery.addFacetField(solrSettings.getFieldName(facetName)); } } } // Override the base facet limit if overrides are given. if (searchState.getFacetLimits() != null) { for (Entry<String, Integer> facetLimit : searchState.getFacetLimits().entrySet()) { solrQuery.add("f." + solrSettings.getFieldName(facetLimit.getKey()) + ".facet.limit", facetLimit.getValue().toString()); } } // Add facet limits Map<String, Object> facets = searchState.getFacets(); if (facets != null) { Iterator<Entry<String, Object>> facetIt = facets.entrySet().iterator(); while (facetIt.hasNext()) { Entry<String, Object> facetEntry = facetIt.next(); if (facetEntry.getValue() instanceof String) { LOG.debug("Adding facet " + facetEntry.getKey() + " as a String"); // Add Normal facets solrQuery.addFilterQuery(solrSettings.getFieldName(facetEntry.getKey()) + ":\"" + SolrSettings.sanitize((String) facetEntry.getValue()) + "\""); } else { LOG.debug("Adding facet " + facetEntry.getKey() + " as a " + facetEntry.getValue().getClass().getName()); facetFieldUtil.addToSolrQuery(facetEntry.getValue(), solrQuery); } } } // Scope hierarchical facet results to the highest tier selected within the facet tree if (searchRequest.isRetrieveFacets() && searchRequest.isApplyCutoffs() && searchState.getFacetsToRetrieve() != null) { Set<String> facetsQueried = searchState.getFacets().keySet(); // Apply closing cutoff to all cutoff facets that are being retrieved but not being queried for for (String fieldKey : searchState.getFacetsToRetrieve()) { if (!facetsQueried.contains(fieldKey)) { facetFieldUtil.addDefaultFacetPivot(fieldKey, solrQuery); } } // Add individual facet field sorts if they are present. if (searchState.getFacetSorts() != null) { for (Entry<String, String> facetSort : searchState.getFacetSorts().entrySet()) { solrQuery.add("f." + solrSettings.getFieldName(facetSort.getKey()) + ".facet.sort", facetSort.getValue()); } } } // Set Navigation options if (searchState.getStartRow() != null) solrQuery.setStart(searchState.getStartRow()); if (searchState.getRowsPerPage() != null) solrQuery.setRows(searchState.getRowsPerPage()); return solrQuery; }
From source file:org.alfresco.solr.component.spellcheck.AlfrescoSpellCheckCollator.java
License:Open Source License
public List<AlfrescoSpellCheckCollation> collate(SpellingResult result, String originalQuery, ResponseBuilder ultimateResponse) { List<AlfrescoSpellCheckCollation> collations = new ArrayList<>(); QueryComponent queryComponent = null; if (ultimateResponse.components != null) { for (SearchComponent sc : ultimateResponse.components) { if (sc instanceof QueryComponent) { queryComponent = (QueryComponent) sc; break; }/*from w ww .j av a 2 s. com*/ } } boolean verifyCandidateWithQuery = true; int maxTries = maxCollationTries; int maxNumberToIterate = maxTries; if (maxTries < 1) { maxTries = 1; maxNumberToIterate = maxCollations; verifyCandidateWithQuery = false; } if (queryComponent == null && verifyCandidateWithQuery) { LOG.info( "Could not find an instance of QueryComponent. Disabling collation verification against the index."); maxTries = 1; verifyCandidateWithQuery = false; } docCollectionLimit = docCollectionLimit > 0 ? docCollectionLimit : 0; int maxDocId = -1; if (verifyCandidateWithQuery && docCollectionLimit > 0) { IndexReader reader = ultimateResponse.req.getSearcher().getIndexReader(); maxDocId = reader.maxDoc(); } JSONObject alfrescoJSON = (JSONObject) ultimateResponse.req.getContext().get(AbstractQParser.ALFRESCO_JSON); String originalAftsQuery = alfrescoJSON != null ? alfrescoJSON.getString("query") : ultimateResponse.getQueryString(); int tryNo = 0; int collNo = 0; PossibilityIterator possibilityIter = new PossibilityIterator(result.getSuggestions(), maxNumberToIterate, maxCollationEvaluations, suggestionsMayOverlap); while (tryNo < maxTries && collNo < maxCollations && possibilityIter.hasNext()) { PossibilityIterator.RankedSpellPossibility possibility = possibilityIter.next(); String collationQueryStr = getCollation(originalQuery, possibility.corrections); int hits = 0; String aftsQuery = null; if (verifyCandidateWithQuery) { tryNo++; SolrQueryRequest req = ultimateResponse.req; SolrParams origParams = req.getParams(); ModifiableSolrParams params = new ModifiableSolrParams(origParams); Iterator<String> origParamIterator = origParams.getParameterNamesIterator(); int pl = SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE.length(); while (origParamIterator.hasNext()) { String origParamName = origParamIterator.next(); if (origParamName.startsWith(SpellingParams.SPELLCHECK_COLLATE_PARAM_OVERRIDE) && origParamName.length() > pl) { String[] val = origParams.getParams(origParamName); if (val.length == 1 && val[0].length() == 0) { params.set(origParamName.substring(pl), (String[]) null); } else { params.set(origParamName.substring(pl), val); } } } // we don't set the 'q' param, as we'll pass the query via JSON. // params.set(CommonParams.Q, collationQueryStr); params.remove(CommonParams.START); params.set(CommonParams.ROWS, "" + docCollectionLimit); // we don't want any stored fields params.set(CommonParams.FL, "id"); // we'll sort by doc id to ensure no scoring is done. params.set(CommonParams.SORT, "_docid_ asc"); // If a dismax query, don't add unnecessary clauses for scoring params.remove(DisMaxParams.TIE); params.remove(DisMaxParams.PF); params.remove(DisMaxParams.PF2); params.remove(DisMaxParams.PF3); params.remove(DisMaxParams.BQ); params.remove(DisMaxParams.BF); // Collate testing does not support Grouping (see SOLR-2577) params.remove(GroupParams.GROUP); boolean useQStr = true; if (alfrescoJSON != null) { try { aftsQuery = originalAftsQuery.replaceAll(Pattern.quote(originalQuery), Matcher.quoteReplacement(collationQueryStr)); alfrescoJSON.put("query", aftsQuery); req.getContext().put(AbstractQParser.ALFRESCO_JSON, alfrescoJSON); useQStr = false; } catch (JSONException e) { LOG.warn("Exception trying to get/set the query from/to ALFRESCO_JSON.]" + e); } } else { aftsQuery = collationQueryStr; } req.setParams(params); // creating a request here... make sure to close it! ResponseBuilder checkResponse = new ResponseBuilder(req, new SolrQueryResponse(), Arrays.<SearchComponent>asList(queryComponent)); checkResponse.setQparser(ultimateResponse.getQparser()); checkResponse.setFilters(ultimateResponse.getFilters()); checkResponse.components = Arrays.<SearchComponent>asList(queryComponent); if (useQStr) { checkResponse.setQueryString(collationQueryStr); } try { queryComponent.prepare(checkResponse); if (docCollectionLimit > 0) { int f = checkResponse.getFieldFlags(); checkResponse.setFieldFlags(f |= SolrIndexSearcher.TERMINATE_EARLY); } queryComponent.process(checkResponse); hits = (Integer) checkResponse.rsp.getToLog().get("hits"); } catch (EarlyTerminatingCollectorException etce) { assert (docCollectionLimit > 0); assert 0 < etce.getNumberScanned(); assert 0 < etce.getNumberCollected(); if (etce.getNumberScanned() == maxDocId) { hits = etce.getNumberCollected(); } else { hits = (int) (((float) (maxDocId * etce.getNumberCollected())) / (float) etce.getNumberScanned()); } } catch (Exception e) { LOG.warn( "Exception trying to re-query to check if a spell check possibility would return any hits." + e); } finally { checkResponse.req.close(); } } if (hits > 0 || !verifyCandidateWithQuery) { collNo++; AlfrescoSpellCheckCollation collation = new AlfrescoSpellCheckCollation(); collation.setCollationQuery(aftsQuery); collation.setCollationQueryString(collationQueryStr); collation.setHits(hits); collation.setInternalRank( suggestionsMayOverlap ? ((possibility.rank * 1000) + possibility.index) : possibility.rank); NamedList<String> misspellingsAndCorrections = new NamedList<>(); for (SpellCheckCorrection corr : possibility.corrections) { misspellingsAndCorrections.add(corr.getOriginal().toString(), corr.getCorrection()); } collation.setMisspellingsAndCorrections(misspellingsAndCorrections); collations.add(collation); } if (LOG.isDebugEnabled()) { LOG.debug("Collation: " + aftsQuery + (verifyCandidateWithQuery ? (" will return " + hits + " hits.") : "")); } } return collations; }
From source file:org.phenotips.variantstore.db.solr.SolrController.java
License:Open Source License
@Override public List<String> getTopGenesForIndividual(String id, Integer k) { logger.debug(String.format("getTopGenesForIndividual(%s, %d)", id, k)); final List<String> list = new LinkedList<>(); String queryString = String.format("%s:%s", VariantsSchema.CALLSET_IDS, id); SolrQuery q = new SolrQuery().setQuery(queryString).setRows(k) .setSort(VariantsSchema.getCallsetsFieldName(id, VariantsSchema.EXOMISER_GENE_COMBINED_SCORE), SolrQuery.ORDER.desc) .setParam(GroupParams.GROUP, true).setParam(GroupParams.GROUP_FIELD, VariantsSchema.GENE); QueryResponse resp;//from www . j a va 2 s .c o m try { resp = server.query(q); } catch (SolrServerException | IOException e) { logger.error("Solr Exception", e); return list; } for (GroupCommand command : resp.getGroupResponse().getValues()) { for (Group group : command.getValues()) { list.add(group.getGroupValue()); } } return list; }
From source file:org.sakaiproject.nakamura.files.search.AbstractContentSearchQueryHandler.java
License:Apache License
/** * {@inheritDoc}//from w ww . ja va2 s. com * @see org.sakaiproject.nakamura.api.search.solr.DomainObjectSearchQueryHandler#refineQuery(java.util.Map, org.sakaiproject.nakamura.api.search.solr.Query) */ @Override public void refineQuery(Map<String, String> parametersMap, Query query) { query.getOptions().put(CommonParams.FL, "path"); /* * If there is a query string 'q' specified, then we will also need to search on * widget data contents. Because of this, to avoid duplicate content (e.g, multiple * widgets of a pooled content item match on the content), we need to group by the * widget content "returnpath". See also the #getResourceTypeClause(Map) method to * see how the widget data resourceType is dynamically included in the query. */ if (hasGeneralQuery(parametersMap)) { query.getOptions().put(GroupParams.GROUP, Boolean.TRUE); query.getOptions().put(GroupParams.GROUP_FIELD, "returnpath"); // record the number "groups" matched to give accurate total of elements // returned. Here, one GROUP is one actual result, instead of all the // identical elements aggregated in those groups. query.getOptions().put(GroupParams.GROUP_TOTAL_COUNT, Boolean.TRUE); } }
From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java
License:Apache License
private void assertGroupFormatPresent(SolrQuery solrQuery, boolean groupTotalCount) { Assert.assertEquals("true", solrQuery.get(GroupParams.GROUP)); Assert.assertEquals("false", solrQuery.get(GroupParams.GROUP_MAIN)); Assert.assertEquals("grouped", solrQuery.get(GroupParams.GROUP_FORMAT)); Assert.assertEquals(String.valueOf(groupTotalCount), solrQuery.get(GroupParams.GROUP_TOTAL_COUNT)); }