List of usage examples for org.apache.solr.common SolrDocumentList getNumFound
public long getNumFound()
From source file:org.phenotips.similarity.internal.SolrSimilarPatientsFinder.java
License:Open Source License
/** * Performs a search in the Solr index, returning only the total number of matches found. * * @param query the query prepared with {@link #generateQuery(Patient)} * @return the total number of document matched by the query, {@code 0} if none match *//*from w ww.ja va 2 s. co m*/ private long count(SolrQuery query) { query.setRows(0); SolrDocumentList response = search(query); if (response != null) { return response.getNumFound(); } return 0; }
From source file:org.phenotips.vocabulary.internal.solr.AbstractSolrVocabulary.java
License:Open Source License
/** * Get the number of entries that match a specific Lucene query. * * @param query a valid the Lucene query as string * @return the number of entries matching the query *//*from w ww.j av a 2 s.c o m*/ protected long count(String query) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.Q, query); params.set(CommonParams.START, "0"); params.set(CommonParams.ROWS, "0"); SolrDocumentList results; try { this.logger.debug("Counting terms matching [{}] in [{}]", query, getCoreName()); results = this.externalServicesAccess.getSolrConnection().query(params).getResults(); return results.getNumFound(); } catch (Exception ex) { this.logger.error("Failed to count ontology terms: {}", ex.getMessage(), ex); return -1; } }
From source file:org.roda.core.index.utils.SolrUtils.java
private static <T extends Serializable> IndexResult<T> queryResponseToIndexResult(QueryResponse response, Class<T> responseClass, Facets facets, List<String> liteFields) throws GenericException { final SolrDocumentList docList = response.getResults(); final List<FacetFieldResult> facetResults = processFacetFields(facets, response.getFacetFields()); final long offset = docList.getStart(); final long limit = docList.size(); final long totalCount = docList.getNumFound(); final List<T> docs = new ArrayList<>(); for (SolrDocument doc : docList) { T result = solrDocumentTo(responseClass, doc, liteFields); docs.add(result);/*w ww. jav a 2 s . c o m*/ } return new IndexResult<>(offset, limit, totalCount, docs, facetResults); }
From source file:org.roda.core.RodaCoreFactory.java
private static void runSolrQuery(List<String> args) { String collection = args.get(2); String solrQueryString = args.get(3); try {/*from w ww . j a v a 2 s . c o m*/ QueryResponse executeSolrQuery = SolrUtils.executeSolrQuery(solr, collection, solrQueryString); SolrDocumentList results = executeSolrQuery.getResults(); System.out.println("Size: " + results.getNumFound() + "; Returned: " + results.size()); for (SolrDocument solrDocument : results) { System.out.println(">" + solrDocument); } } catch (SolrServerException | IOException e) { e.printStackTrace(System.err); } }
From source file:org.rsc.liferay.solr.SolrIndexSearcher.java
License:Open Source License
protected Hits subset(SolrQuery solrQuery, Query query, QueryConfig queryConfig, QueryResponse queryResponse, boolean allResults) throws Exception { long startTime = System.currentTimeMillis(); Hits hits = new HitsImpl(); SolrDocumentList solrDocumentList = queryResponse.getResults(); long total = solrDocumentList.getNumFound(); if (allResults && (total > 0)) { solrQuery.setRows((int) total); queryResponse = _solrServer.query(solrQuery); return subset(solrQuery, query, queryConfig, queryResponse, false); }//from www . ja v a2s . c o m List<Document> documents = new ArrayList<Document>(); List<Float> scores = new ArrayList<Float>(); List<String> snippets = new ArrayList<String>(); float maxScore = -1; Set<String> queryTerms = new HashSet<String>(); int subsetTotal = 0; for (SolrDocument solrDocument : solrDocumentList) { Document document = new DocumentImpl(); Collection<String> names = solrDocument.getFieldNames(); for (String name : names) { Collection<Object> fieldValues = solrDocument.getFieldValues(name); Field field = new Field(name, ArrayUtil.toStringArray(fieldValues.toArray(new Object[fieldValues.size()]))); document.add(field); } documents.add(document); String snippet = StringPool.BLANK; if (queryConfig.isHighlightEnabled()) { snippet = getSnippet(solrDocument, queryConfig, queryTerms, queryResponse.getHighlighting(), Field.CONTENT); if (Validator.isNull(snippet)) { snippet = getSnippet(solrDocument, queryConfig, queryTerms, queryResponse.getHighlighting(), Field.TITLE); } if (Validator.isNotNull(snippet)) { snippets.add(snippet); } } if (queryConfig.isScoreEnabled()) { float score = GetterUtil.getFloat(String.valueOf(solrDocument.getFieldValue("score"))); if (score > maxScore) { maxScore = score; } scores.add(score); } else { scores.add(maxScore); } subsetTotal++; } hits.setDocs(documents.toArray(new Document[subsetTotal])); hits.setLength((int) total); hits.setQuery(query); hits.setQueryTerms(queryTerms.toArray(new String[queryTerms.size()])); Float[] scoresArray = scores.toArray(new Float[subsetTotal]); if (queryConfig.isScoreEnabled() && (subsetTotal > 0) && (maxScore > 0)) { for (int i = 0; i < scoresArray.length; i++) { scoresArray[i] = scoresArray[i] / maxScore; } } hits.setScores(scoresArray); float searchTime = (float) (System.currentTimeMillis() - startTime) / Time.SECOND; hits.setSearchTime(searchTime); hits.setSnippets(snippets.toArray(new String[subsetTotal])); hits.setStart(startTime); return hits; }
From source file:org.sakaiproject.nakamura.message.LiteCountServletTest.java
License:Apache License
@Test public void testParams() throws Exception { SlingHttpServletRequest request = mock(SlingHttpServletRequest.class); SlingHttpServletResponse response = mock(SlingHttpServletResponse.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter write = new PrintWriter(baos); when(response.getWriter()).thenReturn(write); // Request stuff RequestParameter groupParam = mock(RequestParameter.class); when(groupParam.getString()).thenReturn("foo"); when(request.getRemoteUser()).thenReturn("admin"); when(request.getRequestParameter("groupedby")).thenReturn(groupParam); // Session & search ResourceResolver rr = mock(ResourceResolver.class); when(request.getResourceResolver()).thenReturn(rr); javax.jcr.Session jcrSession = mock(javax.jcr.Session.class, withSettings().extraInterfaces(SessionAdaptable.class)); when(rr.adaptTo(javax.jcr.Session.class)).thenReturn(jcrSession); Session session = mock(Session.class); when(((SessionAdaptable) jcrSession).getSession()).thenReturn(session); // Node/* w w w . j a va 2s .c om*/ when(messagingService.getFullPathToStore("admin", session)).thenReturn("/path/to/store"); List<NamedList<Object>> groupsResponse = new ArrayList<NamedList<Object>>(); SolrDocumentList documentListA = mock(SolrDocumentList.class); when(documentListA.getNumFound()).thenReturn(2L); NamedList<Object> groupingResultA = new NamedList<Object>(); groupingResultA.add("groupValue", "a"); groupingResultA.add("doclist", documentListA); groupsResponse.add(groupingResultA); SolrDocumentList documentListC = mock(SolrDocumentList.class); when(documentListC.getNumFound()).thenReturn(1L); NamedList<Object> groupingResultC = new NamedList<Object>(); groupingResultC.add("groupValue", "c"); groupingResultC.add("doclist", documentListC); groupsResponse.add(groupingResultC); NamedList<Object> categoryResponse = new NamedList<Object>(); categoryResponse.add("groups", groupsResponse); NamedList<Object> groupedList = new NamedList<Object>(); groupedList.add("foo", categoryResponse); NamedList<Object> solrResponse = new NamedList<Object>(); solrResponse.add("grouped", groupedList); QueryResponse solrQueryResponse = mock(QueryResponse.class); when(solrQueryResponse.getResponse()).thenReturn(solrResponse); SolrSearchResultSet resultSet = mock(SolrSearchResultSet.class, withSettings().extraInterfaces(SolrQueryResponseWrapper.class)); when(searchFactory.getSearchResultSet(isA(SlingHttpServletRequest.class), isA(Query.class), anyBoolean())) .thenReturn(resultSet); when(((SolrQueryResponseWrapper) resultSet).getQueryResponse()).thenReturn(solrQueryResponse); servlet.doGet(request, response); write.flush(); String s = baos.toString("UTF-8"); JSONObject o = new JSONObject(s); JSONArray arr = o.getJSONArray("count"); assertEquals(2, arr.length()); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); String group = obj.getString("group"); if (group.equals("a")) { assertEquals("2", obj.getString("count")); } else if (group.equals("c")) { assertEquals("1", obj.getString("count")); } else { fail("Unexpected group name " + group); } } }
From source file:org.sakaiproject.nakamura.upgrade.servlet.TagMigrator.java
License:Apache License
private Set<String> getUniqueTags() throws SolrServerException { Set<String> allTags = new HashSet<String>(); int start = 0; int processedDocs = 0; while (true) { SolrDocumentList taggedDocuments = getMoreDocuments(start); for (SolrDocument taggedDocument : taggedDocuments) { processedDocs++;//from www . j a v a 2 s.com Collection<Object> tags = taggedDocument.getFieldValues("tag"); if (tags != null) { for (Object o : tags) { if (o instanceof String) { String tag = (String) o; allTags.add(tag); } } } } if (taggedDocuments.getNumFound() > (taggedDocuments.getStart() + ROWS_PER_SEARCH)) { start = start + ROWS_PER_SEARCH; } else { break; } } LOGGER.info("Processed " + processedDocs + " total documents for tags"); LOGGER.info("We have " + allTags.size() + " unique tags represented in Solr indexes: " + allTags); return allTags; }
From source file:org.sakaiproject.nakamura.user.BasicUserInfoServiceTest.java
License:Apache License
@Before public void before() throws ClientPoolException, StorageClientException, AccessDeniedException, ClassNotFoundException, SolrServerException, IOException { System.out.println(System.getProperty("java.version")); repository = (Repository) new BaseMemoryRepository().getRepository(); final Session adminSession = repository.loginAdministrative(); final AuthorizableManager aam = adminSession.getAuthorizableManager(); Map<String, Object> userProps = new HashMap<String, Object>(); userProps.put("firstName", "Ian"); userProps.put("lastName", "Boston"); userProps.put("email", "ieb@gmail.com"); assertTrue(aam.createUser("ieb", "Ian Boston", "password", userProps)); adminSession.logout();/* ww w . jav a 2s .co m*/ session = repository.loginAdministrative("ieb"); basicUserInfoService = new BasicUserInfoServiceImpl(); final SolrServerService sss = Mockito.mock(SolrServerService.class); SolrServer solrServer = Mockito.mock(SolrServer.class); Mockito.when(sss.getServer()).thenReturn(solrServer); QueryResponse response = Mockito.mock(QueryResponse.class); Mockito.when(solrServer.query(Mockito.any(SolrParams.class))).thenReturn(response); SolrDocumentList results = Mockito.mock(SolrDocumentList.class); Mockito.when(response.getResults()).thenReturn(results); Mockito.when(results.getNumFound()).thenReturn(10L); CountProviderImpl countProviderImpl = new CountProviderImpl() { @Override public void modify(Map<String, Object> properties) throws StorageClientException, AccessDeniedException { repository = BasicUserInfoServiceTest.this.repository; solrSearchService = sss; System.err.println("Setting Solr search service " + solrSearchService); super.modify(properties); } }; Map<String, Object> m = ImmutableMap.of(); countProviderImpl.modify(m); basicUserInfoService.countProvider = countProviderImpl; }
From source file:org.sakaiproject.nakamura.user.counts.CountsRefreshJob.java
License:Apache License
/** * update the batch size number of Authorizables whose countLastUpdate is null (never * updated) or whose countLastUpdate is more than the update interval minutes ago * {@inheritDoc}/*w ww .ja v a2 s .c o m*/ * * @see org.apache.sling.commons.scheduler.Job#execute(org.apache.sling.commons.scheduler.JobContext) */ public void execute(JobContext context) { Session adminSession = null; Integer batchSize = (Integer) context.getConfiguration().get(CountsRefreshScheduler.PROP_UPDATE_BATCH_SIZE); try { adminSession = this.sparseRepository.loginAdministrative(); AuthorizableManager authManager = adminSession.getAuthorizableManager(); SolrServer solrServer = solrServerService.getServer(); long nowTicks = System.currentTimeMillis(); long updateIntervalTicks = this.countProvider.getUpdateIntervalMinutes() * 60 * 1000; long updateTicks = nowTicks - updateIntervalTicks; // find all the authorizables have not been updated in the update interval // or who have never been updated StringBuilder querySB = new StringBuilder("+resourceType:authorizable AND -countLastUpdate:[") .append(updateTicks).append(" TO * ]"); String queryString = querySB.toString(); SolrQuery solrQuery = new SolrQuery(queryString).setStart(0).setRows(batchSize); QueryResponse response; try { response = solrServer.query(solrQuery); SolrDocumentList results = response.getResults(); long numResults = results.getNumFound(); if (LOGGER.isDebugEnabled()) LOGGER.debug("with query {}, found {} results", new Object[] { queryString, numResults }); if (numResults > 0) { batchSize = (int) (batchSize < numResults ? batchSize : numResults); LOGGER.info("will update counts on max of {} authorizables", new Object[] { batchSize }); long startTicks = System.currentTimeMillis(); int count = 0; for (SolrDocument solrDocument : results) { String authorizableId = (String) solrDocument.getFieldValue("id"); Authorizable authorizable = authManager.findAuthorizable(authorizableId); if (authorizable != null) { if (authorizable.getId() != null) { this.countProvider.update(authorizable, adminSession); count++; } else { LOGGER.debug( "found authorizable with id {} in Solr index but with NULL id in Sparse, not updating", new Object[] { authorizableId }); } } else { LOGGER.debug( "found authorizable with id {} in Solr index but couldn't find authorizable in Sparse, not updating", new Object[] { authorizableId }); } } long endTicks = System.currentTimeMillis(); LOGGER.info("updated {} authorizables in {} seconds", new Object[] { count, (endTicks - startTicks) / 1000 }); } else { LOGGER.info("All authorizables have up to date counts"); } } catch (SolrServerException e) { LOGGER.warn(e.getMessage(), e); } } catch (ClientPoolException e) { LOGGER.warn(e.getMessage(), e); } catch (StorageClientException e) { LOGGER.warn(e.getMessage(), e); } catch (AccessDeniedException e) { LOGGER.warn(e.getMessage(), e); } finally { if (adminSession != null) try { adminSession.logout(); } catch (ClientPoolException e) { LOGGER.warn(e.getMessage(), e); } } }
From source file:org.schedoscope.metascope.index.SolrQueryExecutor.java
License:Apache License
/** * Perform a query on the metascope solr index. Builds up the query string * from the given parameters and sends request to solr server * /*from w w w .java 2 s. co m*/ * @param params * the parameters which are included in the filter query * @return a SolrQueryResult object containing the result of the query */ public SolrQueryResult query(Map<String, String> params) { SolrQuery query = new SolrQuery(); /* enable facetting */ query.setFacet(true); /* list of all filters; key: name to display, value: entity parameter */ List<SolrQueryParameter> filter = new ArrayList<SolrQueryParameter>(); /* list of partition filter */ List<SolrQueryParameter> partitionFilter = new ArrayList<SolrQueryParameter>(); /* * list of filter which have been set by the user (key = filtername, value = * list of selections) */ Map<String, List<String>> activeFilterValues = new HashMap<String, List<String>>(); /* * determine on which type is searched for (either 'Table' or 'Partition') */ String typeFilterValue = params.get(TYPE); filter.add(new SolrQueryParameter(FILTER_TYPE, TYPE, FilterType.EXCLUSIVE, FacetSort.COUNT)); if (typeFilterValue != null && typeFilterValue.equalsIgnoreCase(TYPE_PARTITION)) { typeFilterValue = TYPE_PARTITION; for (String parameter : fieldEntityService.findDistinctParameters()) { partitionFilter.add(new SolrQueryParameter("Parameter: " + parameter, parameter + "_s", FilterType.AND, FacetSort.INDEX)); } for (SolrQueryParameter pFilter : partitionFilter) { query.addFacetField(pFilter.getName()); query.add("f." + pFilter.getName() + ".facet.sort", "index"); String filterValue = params.get(pFilter.getName()); if (filterValue != null && !filterValue.isEmpty()) { query.addFilterQuery(pFilter.getName() + ":" + "(" + filterValue.replace(",", " AND ") + ")"); } } } else { typeFilterValue = TYPE_TABLE; } query.addFilterQuery("{!tag=" + TYPE + "}" + TYPE + ":" + typeFilterValue); query.addFacetField("{!ex=" + TYPE + "}" + TYPE); /* set solr search query parameter 'q' */ String searchQueryValue = params.get(URLUtil.SEARCH_QUERY_PARAM); if (searchQueryValue == null || searchQueryValue.isEmpty()) { searchQueryValue = "*"; query.setQuery(searchQueryValue); } else { String[] queryTerms = searchQueryValue.trim().split(" "); String queryTerm = ""; for (String term : queryTerms) { if (term.isEmpty()) { continue; } if (!queryTerm.isEmpty()) { queryTerm += " AND "; } queryTerm += "*" + term + "*"; } query.setQuery(queryTerm); query.setHighlight(true); query.setHighlightSimplePre("<b>"); query.setHighlightSimplePost("</b>"); query.setHighlightSnippets(100); query.set("hl.fl", "*"); } /* set the remaining filters */ for (SolrQueryParameter queryFilter : facetFields) { filter.add(queryFilter); String value = params.get(queryFilter.getName()); String filterQuery = ""; String facetField = ""; if (queryFilter.isExcludeFromFacet()) { if (value != null) { String[] multipleFilter = value.split(","); value = "("; for (int i = 0; i < multipleFilter.length; i++) { String filterValue = cleanseValue(multipleFilter[i]).replace(" ", "*"); value += "(" + filterValue + ")"; if (i < multipleFilter.length - 1) { value += " " + getOperator(queryFilter.getFilterType()) + " "; } } value += ")"; } filterQuery = "{!tag=" + queryFilter.getName() + "}" + queryFilter.getName() + ":" + value; facetField = "{!ex=" + queryFilter.getName() + "}" + queryFilter.getName(); } else { if (value != null) { String[] multipleFilter = value.split(","); value = "("; for (int i = 0; i < multipleFilter.length; i++) { String filterValue = cleanseValue(multipleFilter[i]).replace(" ", "*"); value += "(" + filterValue + ")"; if (i < multipleFilter.length - 1) { value += " " + getOperator(queryFilter.getFilterType()) + " "; } } value += ")"; } filterQuery = queryFilter.getName() + ":" + value; facetField = queryFilter.getName(); } if (value != null && !value.isEmpty()) { query.addFilterQuery(filterQuery); } query.addFacetField(facetField); if (queryFilter.getFacetSort().equals(FacetSort.INDEX)) { query.add("f." + queryFilter.getName() + ".facet.sort", "index"); } query.add("f." + queryFilter.getName() + ".facet.limit", "-1"); } /* set facet queries */ Map<String, String> facetQueryMap = new HashMap<String, String>(); long now = System.currentTimeMillis() / 1000; for (SolrFacetQuery solrFacetQuery : facetQueries) { for (SolrHourRange range : solrFacetQuery.getRanges()) { long from = range.getFrom() == Long.MAX_VALUE ? 0 : now - (range.getFrom() * 3600); String facetQueryString = solrFacetQuery.getName() + ":[" + from + " TO " + now + "]"; query.addFacetQuery("{!ex=" + solrFacetQuery.getName() + "}" + facetQueryString); facetQueryMap.put(solrFacetQuery.getName() + range.getName(), facetQueryString); } String value = params.get(solrFacetQuery.getName()); if (value != null) { String fq = "{!tag=" + solrFacetQuery.getName() + "}" + facetQueryMap.get(solrFacetQuery.getName() + value); query.addFilterQuery(fq); } } /* always sort the entities (for a deterministic view) */ query.setSort(ID, ORDER.asc); /* set pagination information */ int page = getPageParameter(params); int elements = getElementsParameter(params); query.setRows(elements); query.setStart((page - 1) * elements); /* execute the query */ QueryResponse queryResponse = null; try { queryResponse = solrClient.query(query); } catch (Exception e) { e.printStackTrace(); } SolrDocumentList list = queryResponse.getResults(); /* get table / view entities from local repository */ List<SolrQueryResultEntity> resultEntities = new LinkedList<SolrQueryResultEntity>(); String resultType = ""; for (SolrDocument solrDocument : list) { String id = (String) solrDocument.get(ID); if (typeFilterValue.equalsIgnoreCase(TYPE_PARTITION)) { if (!searchQueryValue.equals("*")) { resultEntities.add(new SolrQueryResultEntity(viewEntityService.findByUrlPath(id), queryResponse.getHighlighting().get(id))); } else { resultEntities.add(new SolrQueryResultEntity(viewEntityService.findByUrlPath(id))); } resultType = TYPE_PARTITION; } else if (typeFilterValue.equalsIgnoreCase(TYPE_TABLE)) { if (!searchQueryValue.equals("*")) { resultEntities.add(new SolrQueryResultEntity(tableEntityService.findByFqdn(id), queryResponse.getHighlighting().get(id))); } else { resultEntities.add(new SolrQueryResultEntity(tableEntityService.findByFqdn(id))); } } } if (resultType.isEmpty()) { resultType = TYPE_TABLE; } filter.addAll(partitionFilter); /* get the facet values and counts */ Map<String, List<SolrFacet>> facetValues = new HashMap<String, List<SolrFacet>>(); for (SolrQueryParameter f : filter) { if (!f.getName().equals(URLUtil.SEARCH_QUERY_PARAM)) { List<SolrFacet> values = new ArrayList<SolrFacet>(); FacetField facet = queryResponse.getFacetField(f.getName()); for (Count count : facet.getValues()) { values.add(new SolrFacet(count.getName(), count.getCount())); } facetValues.put(f.getName(), values); } } /* remove the type filter */ filter.remove(0); for (SolrFacetQuery solrFacetQuery : facetQueries) { filter.add(new SolrQueryParameter(solrFacetQuery.getDisplayName(), solrFacetQuery.getName(), FilterType.EXCLUSIVE)); List<SolrFacet> values = new ArrayList<SolrFacet>(); for (SolrHourRange range : solrFacetQuery.getRanges()) { long facetQueryCount = getFacetQueryCount(queryResponse, "{!ex=" + solrFacetQuery.getName() + "}" + facetQueryMap.get(solrFacetQuery.getName() + range.getName())); values.add(new SolrFacet(range.getName(), facetQueryCount)); } facetValues.put(solrFacetQuery.getName(), values); } /* get the active filter values which have been selected by the user */ addToActiveFilterValues(activeFilterValues, params, filter); /* build and return the result */ SolrQueryResult result = new SolrQueryResult().withResultEntities(resultEntities).withResultType(resultType) .withFilters(filter).withFacetValues(facetValues).withActiveFilterValues(activeFilterValues) .withSearchQuery(searchQueryValue).withPage(page).withElements(elements) .withTotalPages((int) Math.ceil(((double) list.getNumFound()) / elements)) .withTotalResults(list.getNumFound()); return result; }