/** * Retrieves the SolrDocument associated with the given identifier and uses * the SolrParams to execute the request. * * @param collection the Solr collection to query * @param id the id * @param params additional parameters to add to the query * * @return retrieved SolrDocument, or null if no document is found. * * @throws IOException If there is a low-level I/O error. * @throws SolrServerException if there is an error on the server */ public SolrDocument getById(String collection, String id, SolrParams params) throws SolrServerException, IOException { SolrDocumentList docs = getById(collection, Collections.singletonList(id), params); if (!docs.isEmpty()) { return docs.get(0); } return null; }
/** * Retrieves the SolrDocument associated with the given identifier and uses * the SolrParams to execute the request. * * @param collection the Solr collection to query * @param id the id * @param params additional parameters to add to the query * * @return retrieved SolrDocument, or null if no document is found. * * @throws IOException If there is a low-level I/O error. * @throws SolrServerException if there is an error on the server */ public SolrDocument getById(String collection, String id, SolrParams params) throws SolrServerException, IOException { SolrDocumentList docs = getById(collection, Collections.singletonList(id), params); if (!docs.isEmpty()) { return docs.get(0); } return null; }
@Override public String getVersion() { final SolrQuery query = new SolrQuery(); query.setQuery("version:*"); query.set(CommonParams.ROWS, "1"); try { final QueryResponse response = this.externalServicesAccess.getSolrConnection(this).query(query); final SolrDocumentList termList = response.getResults(); if (!termList.isEmpty()) { final SolrDocument firstDoc = termList.get(0); return firstDoc.getFieldValue(VERSION_FIELD_NAME).toString(); } } catch (SolrServerException | SolrException | IOException ex) { this.logger.warn("Failed to query ontology version: {}", ex.getMessage()); } return null; }
@Override public String getVersion() { QueryResponse response; SolrQuery query = new SolrQuery(); SolrDocumentList termList; SolrDocument firstDoc; query.setQuery("version:*"); query.set(CommonParams.ROWS, "1"); try { response = this.externalServicesAccess.getSolrConnection(this).query(query); termList = response.getResults(); if (!termList.isEmpty()) { firstDoc = termList.get(0); return firstDoc.getFieldValue(VERSION_FIELD_NAME).toString(); } } catch (SolrServerException | SolrException ex) { this.logger.warn("Failed to query vocabulary version: {}", ex.getMessage()); } catch (IOException ex) { this.logger.error("IOException while getting vocabulary version", ex); } return null; } }
@Override public OrgDisambiguatedSolrDocument findById(Long id) { SolrQuery query = new SolrQuery(); query.setQuery(ORG_DISAMBIGUATED_ID + ":" + id).setFields("*"); try { QueryResponse queryResponse = solrServerReadOnly.query(query); if (!queryResponse.getResults().isEmpty()) { OrgDisambiguatedSolrDocument document = queryResponse.getBeans(OrgDisambiguatedSolrDocument.class).get(0); return document; } } catch (SolrServerException se) { String errorMessage = MessageFormat.format("Error when attempting to retrieve org {0}", new Object[] { id }); throw new NonTransientDataAccessResourceException(errorMessage, se); } return null; }
@Override public String getVersion() { QueryResponse response; SolrQuery query = new SolrQuery(); SolrDocumentList termList; SolrDocument firstDoc; query.setQuery("version:*"); query.set("rows", "1"); try { response = this.externalServicesAccess.getServer().query(query); termList = response.getResults(); if (!termList.isEmpty()) { firstDoc = termList.get(0); return firstDoc.getFieldValue(VERSION_FIELD_NAME).toString(); } } catch (SolrServerException | SolrException ex) { this.logger.warn("Failed to query ontology version: {}", ex.getMessage()); } return null; } }
@Override public String getVersion() { QueryResponse response; SolrQuery query = new SolrQuery(); SolrDocumentList termList; SolrDocument firstDoc; query.setQuery("version:*"); query.set("rows", "1"); try { response = this.externalServicesAccess.getSolrConnection(this).query(query); termList = response.getResults(); if (!termList.isEmpty()) { firstDoc = termList.get(0); return firstDoc.getFieldValue(VERSION_FIELD_NAME).toString(); } } catch (SolrServerException | SolrException | IOException ex) { this.logger.warn("Failed to query vocabulary version: {}", ex.getMessage()); } return null; } }
@Override public Optional<Header> getHeader(String mcrId) { SolrQuery query = getBaseQuery(CommonParams.FQ); query.set(CommonParams.Q, "id:" + MCRSolrUtils.escapeSearchValue(mcrId)); query.setRows(1); // do the query SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient(); try { QueryResponse response = solrClient.query(query); SolrDocumentList results = response.getResults(); if (!results.isEmpty()) { return Optional.of(toHeader(results.get(0), getSetResolver(results))); } } catch (Exception exc) { LOGGER.error("Unable to handle solr request", exc); } return Optional.empty(); }
protected VocabularyTerm requestTerm(String queryString, String phraseFields) { QueryResponse response; SolrQuery query = new SolrQuery(); SolrDocumentList termList; VocabularyTerm term; query.setQuery(queryString); query.setRows(1); if (phraseFields != null) { query.set(DisMaxParams.PF, phraseFields); } try { response = this.externalServicesAccess.getSolrConnection(this).query(query); termList = response.getResults(); if (termList != null && !termList.isEmpty()) { term = new SolrVocabularyTerm(termList.get(0), this); return term; } } catch (SolrServerException | SolrException ex) { this.logger.warn("Failed to query vocabulary term: {} ", ex.getMessage()); } catch (IOException ex) { this.logger.error("IOException while getting vocabulary term ", ex); } return null; }
if (queryResponse.getResults().isEmpty()) { return null; } else {
if (queryResponse.getResults().isEmpty()) { return null; } else {
SolrDocumentList results = response.getResults(); if (!results.isEmpty() && response.getHighlighting().get(id) != null) { List<String> fragments = response.getHighlighting().get(id).get(this.content); String[] bestFragments = new String[fragments.size() > 100 ? 100 : fragments.size()];
if (!docs.isEmpty()) { for (SolrDocument doc : docs) { try {
/** * Get the top hit corresponding to the specified query. * * @param fieldValues the map of values to search for, where each key is the name of an indexed field and the value * is the keywords to match for that field * @return the top matching document, {@code null} if there were no matches at all * @see #search(Map) */ public SolrDocument get(final Map<String, String> fieldValues) { String cacheKey = dumpMap(fieldValues); SolrDocument result = this.cache.get(cacheKey); if (result == null) { SolrDocumentList all = search(fieldValues, 1, 0); if (all != null && !all.isEmpty()) { result = all.get(0); this.cache.set(cacheKey, result); } else { this.cache.set(cacheKey, EMPTY_MARKER); } } return (result == EMPTY_MARKER) ? null : result; }
try { SolrDocumentList results = solrServer.query(query).getResults(); if (results.isEmpty()) { throw new ClassifierException(String.format("'%s' is not a registered topic", conceptId));
try { SolrDocumentList results = solrServer.query(query).getResults(); if (results.isEmpty()) { throw new ClassifierException(String.format("'%s' is not a registered topic", conceptId));
@Override public VocabularyTerm getTerm(String id) { if (StringUtils.isBlank(id)) { return null; } VocabularyTerm result = this.externalServicesAccess.getTermCache(this).get(id); if (result == null) { SolrQuery query = new SolrQuery(ID_FIELD_NAME + ':' + ClientUtils.escapeQueryChars(id)); SolrDocumentList allResults = this.search(query); if (allResults != null && !allResults.isEmpty()) { result = new SolrVocabularyTerm(allResults.get(0), this); this.externalServicesAccess.getTermCache(this).set(id, result); } else { this.externalServicesAccess.getTermCache(this).set(id, EMPTY_MARKER); } } return (result == EMPTY_MARKER) ? null : result; }
@Override public OrcidSolrResult findByOrcid(String orcid) { OrcidSolrResult orcidSolrResult = null; SolrQuery query = new SolrQuery(); query.setQuery(ORCID + ":\"" + orcid + "\"").setFields(SCORE, ORCID, PUBLIC_PROFILE); try { QueryResponse queryResponse = solrServerReadOnly.query(query); if (!queryResponse.getResults().isEmpty()) { SolrDocument solrDocument = queryResponse.getResults().get(0); orcidSolrResult = new OrcidSolrResult(); orcidSolrResult.setRelevancyScore((Float) solrDocument.get(SCORE)); orcidSolrResult.setOrcid((String) solrDocument.get(ORCID)); orcidSolrResult.setPublicProfileMessage((String) solrDocument.getFieldValue(PUBLIC_PROFILE)); } } catch (SolrServerException se) { String errorMessage = MessageFormat.format("Error when attempting to retrieve orcid {0}", new Object[] { orcid }); throw new NonTransientDataAccessResourceException(errorMessage, se); } return orcidSolrResult; }
@Override public boolean filter(MCRSet set) { if (!filterEmptySets()) { return false; } SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient(); ModifiableSolrParams p = new ModifiableSolrParams(); String value = set.getSpec(); p.set(CommonParams.Q, MCROAIUtils.getDefaultSetQuery(value, getConfigPrefix())); String restriction = MCROAIUtils.getDefaultRestriction(getConfigPrefix()); if (restriction != null) { p.set(CommonParams.FQ, restriction); } p.set(CommonParams.ROWS, 1); p.set(CommonParams.FL, "id"); try { QueryResponse response = solrClient.query(p); return response.getResults().isEmpty(); } catch (Exception exc) { LOGGER.error("Unable to get results of solr server", exc); return true; } }
@Override public OntologyTerm getTerm(String id) { OntologyTerm result = this.externalServicesAccess.getCache().get(id); if (result == null) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.Q, ID_FIELD_NAME + ':' + ClientUtils.escapeQueryChars(id)); SolrDocumentList allResults = this.search(params); if (allResults != null && !allResults.isEmpty()) { result = new SolrOntologyTerm(allResults.get(0), this); this.externalServicesAccess.getCache().set(id, result); } else { this.externalServicesAccess.getCache().set(id, EMPTY_MARKER); } } return (result == EMPTY_MARKER) ? null : result; }