List of usage examples for org.apache.solr.common SolrDocumentList getNumFound
public long getNumFound()
From source file:org.dataconservancy.dcs.access.impl.solr.DcsSolrMapper.java
License:Apache License
protected static SolrDocument lookupEntityDocument(SolrServer server, String id) throws IOException { SolrQuery q = new SolrQuery(QueryUtil.createLiteralQuery(EntityField.ID.solrName(), id)); try {//from www .java2s .c om QueryResponse resp = server.query(q); SolrDocumentList result = resp.getResults(); if (result.getNumFound() == 0) { return null; } else if (result.getNumFound() > 1) { throw new IOException("More than one entity with id: " + id); } else { return result.get(0); } } catch (SolrException e) { throw new IOException(e); } catch (SolrServerException e) { throw new IOException(e); } }
From source file:org.dataconservancy.dcs.index.dcpsolr.SolrService.java
License:Apache License
/** * @param id//from w w w . jav a 2s . c om * @return SolrDocument with the given identifier or null if the * SolrDocument is not in the index. * @throws SolrServerException * @throws IOException */ public SolrDocument lookupSolrDocument(String id) throws IOException, SolrServerException { if (id == null) { return null; } SolrQuery q = new SolrQuery(SolrQueryUtil.createLiteralQuery(EntityField.ID.solrName(), id)); QueryResponse resp = server.query(q); SolrDocumentList result = resp.getResults(); if (result.getNumFound() == 0) { return null; } else if (result.getNumFound() > 1) { throw new IOException("More than one entity with id: " + id); } else { return result.get(0); } }
From source file:org.dataconservancy.dcs.query.dcpsolr.DcsDataModelQueryService.java
License:Apache License
public QueryResult<DcsEntity> query(String query, long offset, int matches, String... params) throws QueryServiceException { if (offset < 0) { throw new IllegalArgumentException("offset < 0"); }//ww w . j a va 2s .c o m if (offset > Integer.MAX_VALUE) { throw new QueryServiceException("offset too large"); } if ((params.length & 1) > 0) { throw new IllegalArgumentException("parameter name without value"); } if (matches <= 0 || matches > MAX_MATCHES) { matches = MAX_MATCHES; } try { QueryResponse resp = solr.search(query, (int) offset, matches, params); SolrDocumentList docs = resp.getResults(); QueryResult<DcsEntity> result = new QueryResult<DcsEntity>(offset, docs.getNumFound(), query, params); for (SolrDocument doc : docs) { DcsEntity entity = solr.asEntity(doc); String context = null; if (resp.getHighlighting() != null) { Map<String, List<String>> snippets = resp.getHighlighting().get(entity.getId()); // Add "field: snippet" to context if (snippets != null) { StringBuilder sb = new StringBuilder(); for (String field : snippets.keySet()) { sb.append(field + ": '" + snippets.get(field) + "' "); } context = sb.toString(); } } result.getMatches().add(new QueryMatch<DcsEntity>(entity, context)); } return result; } catch (SolrException e) { throw new QueryServiceException(e); } catch (SolrServerException e) { throw new QueryServiceException(e); } catch (IOException e) { throw new QueryServiceException(e); } }
From source file:org.dataconservancy.dcs.query.dcpsolr.SeadDataModelQueryService.java
License:Apache License
public QueryResult<DcsEntity> query(String query, long offset, int matches, String... params) throws QueryServiceException { if (offset < 0) { throw new IllegalArgumentException("offset < 0"); }//from w ww . j a v a2 s. com if (offset > Integer.MAX_VALUE) { throw new QueryServiceException("offset too large"); } if ((params.length & 1) > 0) { throw new IllegalArgumentException("parameter name without value"); } if (matches <= 0 || matches > MAX_MATCHES) { matches = MAX_MATCHES; } try { QueryResponse resp = solr.search(query, (int) offset, matches, params); SolrDocumentList docs = resp.getResults(); QueryResult<DcsEntity> result = new QueryResult<DcsEntity>(offset, docs.getNumFound(), query, params); for (SolrDocument doc : docs) { DcsEntity entity = solr.asEntity(doc); String context = null; if (resp.getHighlighting() != null) { Map<String, List<String>> snippets = resp.getHighlighting().get(entity.getId()); // Add "field: snippet" to context if (snippets != null) { StringBuilder sb = new StringBuilder(); for (String field : snippets.keySet()) { sb.append(field + ": '" + snippets.get(field) + "' "); } context = sb.toString(); } } if (resp.getFacetFields() != null) { for (int i = 0; i < resp.getFacetFields().size(); i++) { context = new StringBuilder().append(context).append("FacetCategory[") .append(((FacetField) resp.getFacetFields().get(i)).getName()).toString(); List facets = ((FacetField) resp.getFacetFields().get(i)).getValues(); if (facets != null) { for (int j = 0; j < facets.size(); j++) { context = new StringBuilder().append(context).append("[") .append(((FacetField.Count) facets.get(j)).getName()).append("(") .append(((FacetField.Count) facets.get(j)).getCount()).append(")]") .toString(); } } context = new StringBuilder().append(context).append("]").toString(); } } result.getMatches().add(new QueryMatch<DcsEntity>(entity, context)); } return result; } catch (SolrException e) { throw new QueryServiceException(e); } catch (SolrServerException e) { throw new QueryServiceException(e); } catch (IOException e) { throw new QueryServiceException(e); } }
From source file:org.dspace.app.cris.batch.ScriptAddPMCDataToRP.java
/** * Batch script to aggregate PMC data to RPs. See the technical * documentation for further details.//from w w w.j a v a 2 s. co m * * @throws SearchServiceException */ public static void main(String[] args) throws ParseException, SQLException, SearchServiceException { log.info("#### START AddPMCDataToRP: -----" + new Date() + " ----- ####"); DSpace dspace = new DSpace(); ApplicationService applicationService = dspace.getServiceManager().getServiceByName("applicationService", ApplicationService.class); CrisSearchService searchService = dspace.getServiceManager() .getServiceByName(CrisSearchService.class.getName(), CrisSearchService.class); PMCPersistenceService pmcService = dspace.getServiceManager() .getServiceByName(PMCPersistenceService.class.getName(), PMCPersistenceService.class); List<ResearcherPage> rs = applicationService.getList(ResearcherPage.class); for (ResearcherPage rp : rs) { boolean updated = false; int itemsCited = 0; int citations = 0; SolrQuery query = new SolrQuery(); query.setQuery("dc.identifier.pmid:[* TO *]"); query.addFilterQuery("{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp), "NOT(withdrawn:true)"); query.setFields("dc.identifier.pmid"); query.setRows(Integer.MAX_VALUE); QueryResponse response = searchService.search(query); SolrDocumentList results = response.getResults(); for (SolrDocument doc : results) { Integer pmid = null; try { pmid = Integer.valueOf((String) doc.getFirstValue("dc.identifier.pmid")); } catch (NumberFormatException e) { log.warn("Found invalid pmid: " + doc.getFieldValue("dc.identifier.pmid") + " for rp: " + ResearcherPageUtils.getPersistentIdentifier(rp)); } if (pmid != null) { PMCCitation pmccitation = pmcService.get(PMCCitation.class, pmid); if (pmccitation != null && pmccitation.getNumCitations() > 0) { itemsCited++; citations += pmccitation.getNumCitations(); } } } updated = setValue(applicationService, rp, itemsCitedTP, String.valueOf(itemsCited)); // caution don't use the short-circuit OR operator (i.e || otherwise // only the first found pmcdata value will be recorded!) updated = updated | setValue(applicationService, rp, citationsTP, String.valueOf(citations)); updated = updated | setValue(applicationService, rp, itemsInPubmedTP, String.valueOf(results.getNumFound())); if (StringUtils.isNotEmpty(itemsInPMCTP)) { query = new SolrQuery(); query.setQuery("dc.identifier.pmcid:[* TO *]"); query.addFilterQuery( "{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp), "NOT(withdrawn:true)"); query.setRows(0); response = searchService.search(query); results = response.getResults(); // caution don't use the short-circuit OR operator (i.e || otherwise // only the first found pmcdata value will be recorded!) updated = updated | setValue(applicationService, rp, itemsInPMCTP, String.valueOf(results.getNumFound())); } if (updated) { applicationService.saveOrUpdate(ResearcherPage.class, rp); } } log.info("#### END AddPMCDataToRP: -----" + new Date() + " ----- ####"); }
From source file:org.dspace.app.cris.batch.ScriptCrisSubscribe.java
License:Open Source License
/** * Sends an email to the given e-person with details of new items in the * given dspace object (MUST be a community or a collection), items that * appeared yesterday. No e-mail is sent if there aren't any new items in * any of the dspace objects./* www .j a va 2 s.c om*/ * * @param context * DSpace context object * @param eperson * eperson to send to * @param rpkeys * List of DSpace Objects * @param test * @throws SearchServiceException */ public static void sendEmail(Researcher researcher, Context context, EPerson eperson, List<String> rpkeys, boolean test, List<String> relationFields) throws IOException, MessagingException, SQLException, SearchServiceException { CrisSearchService searchService = researcher.getCrisSearchService(); // Get a resource bundle according to the eperson language preferences Locale supportedLocale = I18nUtil.getEPersonLocale(eperson); StringBuffer emailText = new StringBuffer(); boolean isFirst = true; for (String rpkey : rpkeys) { SolrQuery query = new SolrQuery(); query.setFields("search.resourceid"); query.addFilterQuery("{!field f=search.resourcetype}" + Constants.ITEM, "{!field f=inarchive}true"); for (String tmpRelations : relationFields) { String fq = "{!field f=" + tmpRelations + "}" + rpkey; query.addFilterQuery(fq); } query.setRows(Integer.MAX_VALUE); if (ConfigurationManager.getBooleanProperty("eperson.subscription.onlynew", false)) { // get only the items archived yesterday query.setQuery("dateaccessioned:(NOW/DAY-1DAY)"); } else { // get all item modified yesterday but not published the day // before // and all the item modified today and archived yesterday query.setQuery( "(item.lastmodified:(NOW/DAY-1DAY) AND dateaccessioned:(NOW/DAY-1DAY)) OR ((item.lastmodified:(NOW/DAY) AND dateaccessioned:(NOW/DAY-1DAY)))"); } QueryResponse qResponse = searchService.search(query); SolrDocumentList results = qResponse.getResults(); // Only add to buffer if there are new items if (results.getNumFound() > 0) { if (!isFirst) { emailText.append("\n---------------------------------------\n"); } else { isFirst = false; } emailText.append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.new-items", supportedLocale)) .append(" ").append(rpkey).append(": ").append(results.getNumFound()).append("\n\n"); for (SolrDocument solrDoc : results) { Item item = Item.find(context, (Integer) solrDoc.getFieldValue("search.resourceid")); DCValue[] titles = item.getDC("title", null, Item.ANY); emailText.append(" ") .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.title", supportedLocale)) .append(" "); if (titles.length > 0) { emailText.append(titles[0].value); } else { emailText.append( I18nUtil.getMessage("org.dspace.eperson.Subscribe.untitled", supportedLocale)); } DCValue[] authors = item.getDC("contributor", Item.ANY, Item.ANY); if (authors.length > 0) { emailText.append("\n ").append( I18nUtil.getMessage("org.dspace.eperson.Subscribe.authors", supportedLocale)) .append(" ").append(authors[0].value); for (int k = 1; k < authors.length; k++) { emailText.append("\n ").append(authors[k].value); } } emailText.append("\n ") .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.id", supportedLocale)) .append(" ").append(HandleManager.getCanonicalForm(item.getHandle())).append("\n\n"); context.removeCached(item, item.getID()); } } } // Send an e-mail if there were any new items if (emailText.length() > 0) { if (test) { log.info(LogManager.getHeader(context, "subscription:", "eperson=" + eperson.getEmail())); log.info(LogManager.getHeader(context, "subscription:", "text=" + emailText.toString())); } else { Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscription")); email.addRecipient(eperson.getEmail()); email.addArgument(emailText.toString()); email.send(); log.info(LogManager.getHeader(context, "sent_subscription", "eperson_id=" + eperson.getID())); } } }
From source file:org.dspace.app.cris.batch.ScriptIndexNetwork.java
private static boolean checkAvailableData(String connection, VisualizationGraphSolrService service) throws SearchServiceException { String query = "type:" + connection; SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query);/*from w w w .ja v a 2s.com*/ solrQuery.setStart(0); solrQuery.setRows(0); QueryResponse rsp = service.search(solrQuery); SolrDocumentList docs = rsp.getResults(); if (docs != null) { if (docs.getNumFound() > 0) { return true; } } return false; }
From source file:org.dspace.app.cris.network.AVisualizationGraphModeOne.java
@Override public List<VisualizationGraphNode> load(List<String[]> discardedNode, Integer importedNodes, Boolean otherError) throws Exception { // load all publications SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery("*:*"); solrQuery.setFields("search.resourceid", "search.resourcetype", "author_filter", "dc.title", "handle"); solrQuery.addFilterQuery("search.resourcetype:[2 TO 4]"); int start = 0; int offset = 100; if (NOT_PAGINATION) { solrQuery.setRows(Integer.MAX_VALUE); } else {// www . j a v a2 s . c o m solrQuery.setStart(start); solrQuery.setRows(offset); } QueryResponse rsp = getService().getSearcher().search(solrQuery); SolrDocumentList publications = rsp.getResults(); System.out.println(publications.getNumFound()); List<VisualizationGraphNode> result = null; boolean endwhile = false; while (((publications.getNumFound() + offset) > start) && endwhile == false) { if (start > 0 && !NOT_PAGINATION) { solrQuery.setStart(start); solrQuery.setRows(offset); rsp = getService().getSearcher().search(solrQuery); publications = rsp.getResults(); } start = (start + 1) + offset; // for each publication get authority's authors facets Iterator<SolrDocument> iter = publications.iterator(); int counter = 0; external: while (iter.hasNext()) { counter++; log.debug("" + (start == 0 ? counter : (start + counter)) + " of " + publications.getNumFound()); System.out.println(getConnectionName() + " - " + counter); result = new ArrayList<VisualizationGraphNode>(); Integer pubId = null; try { SolrDocument publication = iter.next(); pubId = (Integer) publication.getFieldValue("search.resourceid"); Object obj = publication.getFieldValue("dc.title"); String handle = (String) publication.getFieldValue("handle"); Object auth = publication.getFieldValue("author_filter"); List<String> values = new ArrayList<String>(); if (obj instanceof ArrayList) { for (String aaa : (List<String>) obj) { values.add(aaa); } } else { String value = (String) obj; values.add(value); } if (auth instanceof ArrayList) { int i = 0; List<String> authArrays = (List<String>) auth; for (String aaa : authArrays) { String[] split = aaa.split("\\|\\|\\|"); String a = aaa; String a_authority = null; String a_dept = null; String a_displayValue = ""; if (split.length > 1) { String[] splitAuthority = split[1].split(splitterAuthority); a_displayValue = splitAuthority[0]; if (splitAuthority.length > 1) { a_authority = splitAuthority[1]; // a_dept = ResearcherPageUtils // .getDepartment(a_authority); a_dept = getDepartmentFromSOLR(a_authority); } } for (int j = i; j < authArrays.size(); j++) { String bbb = authArrays.get(j); String extra = handle; split = bbb.split("\\|\\|\\|"); String b = bbb; String b_authority = null; String b_dept = null; String b_displayValue = ""; if (split.length > 1) { String[] splitAuthority = split[1].split(splitterAuthority); b_displayValue = splitAuthority[0]; if (splitAuthority.length > 1) { b_authority = splitAuthority[1]; // a_dept = ResearcherPageUtils // .getDepartment(a_authority); b_dept = getDepartmentFromSOLR(b_authority); } } if (j == i) { buildRow(result, a, a_authority, a_displayValue, b, b_authority, b_displayValue, values, extra, a_dept, b_dept, ConstantNetwork.ENTITY_PLACEHOLDER_RP); } else { buildRow(result, a, a_authority, a_displayValue, b, b_authority, b_displayValue, values, extra, a_dept, b_dept, ConstantNetwork.ENTITY_RP); } } i++; } } else { continue external; } } catch (Exception e) { log.error("Error try to build object to index with publication ID:" + pubId); log.error(e.getMessage(), e); otherError = true; continue; } importedNodes = indexNode(discardedNode, importedNodes, result); } if (NOT_PAGINATION) { endwhile = true; } log.debug("commit " + getType()); getIndexer().commit(); } return result; }
From source file:org.dspace.app.cris.network.AVisualizationGraphModeThree.java
@Override public List<VisualizationGraphNode> load(List<String[]> discardedNode, Integer importedNodes, Boolean otherError) throws Exception { // load all publications SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(getQuery());/* www .j a v a2 s. c om*/ solrQuery.setFields(getFields()); solrQuery.addFilterQuery("search.resourcetype:" + getResourceType()); int start = 0; int offset = 100; if (NOT_PAGINATION) { solrQuery.setRows(Integer.MAX_VALUE); } else { solrQuery.setStart(start); solrQuery.setRows(offset); } QueryResponse rsp = getService().getSearcher().search(solrQuery); SolrDocumentList publications = rsp.getResults(); System.out.println(publications.getNumFound()); List<VisualizationGraphNode> result = null; boolean endwhile = false; while (((publications.getNumFound() + offset) > start) && endwhile == false) { if (start > 0 && !NOT_PAGINATION) { solrQuery.setStart(start); solrQuery.setRows(offset); rsp = getService().getSearcher().search(solrQuery); publications = rsp.getResults(); } start = (start + 1) + offset; // for each publication get authority's authors facets Iterator<SolrDocument> iter = publications.iterator(); int counter = 0; external: while (iter.hasNext()) { counter++; log.debug("" + (start == 0 ? counter : (start + counter)) + " of " + publications.getNumFound()); System.out.println(getConnectionName() + " - " + counter); result = new ArrayList<VisualizationGraphNode>(); Integer pubId = null; try { SolrDocument publication = iter.next(); pubId = (Integer) publication.getFieldValue("search.resourceid"); Object obj = publication.getFieldValue(getNameFieldValueInFields()); String handle = (String) publication.getFirstValue(getNameFieldExtraInFields()); Object auth = publication.getFieldValue(getNameFieldNodeBuilderInFields()); List<String> values = new ArrayList<String>(); if (obj instanceof ArrayList) { for (String aaa : (List<String>) obj) { values.add(aaa); } } else { String value = (String) obj; values.add(value); } if (auth instanceof ArrayList) { int i = 0; List<String> authArrays = (List<String>) auth; for (String aaa : authArrays) { String[] split = aaa.split("\\|\\|\\|"); String a = aaa; String a_authority = null; String a_dept = null; String a_displayValue = ""; if (split.length > 1) { String[] splitAuthority = split[1].split(splitterAuthority); a_displayValue = splitAuthority[0]; if (split.length > 1) { a_authority = splitAuthority[1]; // a_dept = ResearcherPageUtils // .getDepartment(a_authority); a_dept = getDepartmentFromSOLR(a_authority); } } for (int j = i; j < authArrays.size(); j++) { String bbb = authArrays.get(j); String extra = handle; split = bbb.split("\\|\\|\\|"); String b = bbb; String b_authority = null; String b_dept = null; String b_displayValue = ""; if (split.length > 1) { String[] splitAuthority = split[1].split(splitterAuthority); b_displayValue = splitAuthority[0]; if (split.length > 1) { b_authority = splitAuthority[1]; // a_dept = ResearcherPageUtils // .getDepartment(a_authority); b_dept = getDepartmentFromSOLR(a_authority); } } if (j == i) { buildRow(result, a, a_authority, a_displayValue, b, b_authority, b_displayValue, values, extra, a_dept, b_dept, ConstantNetwork.ENTITY_PLACEHOLDER_RP); } else { buildRow(result, a, a_authority, a_displayValue, b, b_authority, b_displayValue, values, extra, a_dept, b_dept, ConstantNetwork.ENTITY_RP); } } i++; } } else { continue external; } } catch (Exception e) { log.error("Error try to build object to index with publication ID:" + pubId); log.error(e.getMessage(), e); otherError = true; continue; } importedNodes = indexNode(discardedNode, importedNodes, result); } if (NOT_PAGINATION) { endwhile = true; } log.debug("commit " + getType()); getIndexer().commit(); } return result; }
From source file:org.dspace.app.cris.statistics.batch.RebuildDerivateStatisticsMetadata.java
/** * @param args/* w w w .jav a 2 s . c o m*/ * @throws SolrServerException * @throws IOException * @throws SQLException */ public static void main(String[] args) throws SolrServerException, IOException, SQLException { DSpace dspace = new DSpace(); SolrLogger indexer = dspace.getServiceManager().getServiceByName(SolrLogger.class.getName(), SolrLogger.class); SolrDocumentList sdl = indexer.getRawData(Constants.ITEM); System.out.println("Found " + sdl.getNumFound() + " access in the statistics core"); HttpSolrServer solr = indexer.getSolr(); indexer.deleteByType(Constants.ITEM); solr.commit(); System.out.println("Remove old data"); Context context = new Context(); context.turnOffAuthorisationSystem(); int i = 0; for (SolrDocument sd : sdl) { i++; System.out.println("Processed access #" + i + " of " + sdl.getNumFound()); SolrInputDocument sdi = ClientUtils.toSolrInputDocument(sd); Integer id = (Integer) sd.getFieldValue("id"); Integer type = (Integer) sd.getFieldValue("type"); DSpaceObject dso = DSpaceObject.find(context, type, id); // Do any additional indexing, depends on the plugins List<SolrStatsIndexPlugin> solrServiceIndexPlugins = new DSpace().getServiceManager() .getServicesByType(SolrStatsIndexPlugin.class); for (SolrStatsIndexPlugin solrServiceIndexPlugin : solrServiceIndexPlugins) { solrServiceIndexPlugin.additionalIndex(null, dso, sdi); } context.removeCached(dso, id); solr.add(sdi); } solr.commit(); solr.optimize(); sdl = indexer.getRawData(CrisConstants.RP_TYPE_ID); System.out.println("Found " + sdl.getNumFound() + " access in the RP statistics core"); HttpSolrServer rpsolr = indexer.getSolr(); indexer.deleteByType(CrisConstants.RP_TYPE_ID); rpsolr.commit(); System.out.println("Remove old data"); ApplicationService as = dspace.getServiceManager().getServiceByName("applicationService", ApplicationService.class); i = 0; for (SolrDocument sd : sdl) { i++; System.out.println("Processed RP access #" + i + " of " + sdl.getNumFound()); SolrInputDocument sdi = ClientUtils.toSolrInputDocument(sd); Integer id = (Integer) sd.getFieldValue("id"); ResearcherPage rp = as.get(ResearcherPage.class, id); if (rp == null) continue; // Do any additional indexing, depends on the plugins List<SolrStatsIndexPlugin> solrServiceIndexPlugins = new DSpace().getServiceManager() .getServicesByType(SolrStatsIndexPlugin.class); for (SolrStatsIndexPlugin solrServiceIndexPlugin : solrServiceIndexPlugins) { solrServiceIndexPlugin.additionalIndex(null, rp, sdi); } rpsolr.add(sdi); } rpsolr.commit(); rpsolr.optimize(); }