@Override @SuppressWarnings("unchecked") public Map<String, SolrJSONFacet> resolveJSONFacetResponse(QueryResponse response) { Map<String, SolrJSONFacet> jsonFacetMap = new HashMap<>(); NamedList facetResponse = (NamedList) response.getResponse().get("facets"); if (facetResponse != null) { Iterator<Map.Entry<String, Object>> facetIterator = facetResponse.iterator(); while (facetIterator.hasNext()) { Map.Entry<String, Object> entry = facetIterator.next(); if (NamedList.class.isAssignableFrom(entry.getValue().getClass())) { jsonFacetMap.put(entry.getKey(), resolveJSONFacet((NamedList) entry.getValue())); } } } return jsonFacetMap; }
private String getFieldNameOfUniqueKey() { final SolrQuery solrQuery = new SolrQuery(); try { solrQuery.setRequestHandler("/schema/uniquekey"); final QueryRequest req = new QueryRequest(solrQuery); if (isBasicAuthEnabled()) { req.setBasicAuthCredentials(getUsername(), getPassword()); } return(req.process(getSolrClient()).getResponse().get("uniqueKey").toString()); } catch (SolrServerException | IOException e) { getLogger().error("Solr query to retrieve uniqueKey-field failed due to {}", new Object[]{solrQuery.toString(), e}, e); throw new ProcessException(e); } }
private static TermFacetResult<String> getTypeFacetResults(QueryResponse response) { final TermFacetResult typeFacetResults = new TermFacetResult(); //term facets if (Objects.nonNull(response.getResponse())) { final SimpleOrderedMap jsonFacetResult = (SimpleOrderedMap) response.getResponse().get("facets"); if (Objects.nonNull(jsonFacetResult)) { for (int i = 0; i < jsonFacetResult.size(); i++) { if (jsonFacetResult.getName(i).equals(Fieldname.TYPE)) { final ArrayList<SimpleOrderedMap> termFacet = ((ArrayList<SimpleOrderedMap>) ((SimpleOrderedMap) jsonFacetResult.get(jsonFacetResult.getName(i))).get("buckets")); termFacet.stream().forEach(f -> typeFacetResults .addFacetValue(new FacetValue<>((String) f.get("val"), ((Integer)f.get("count")).longValue()))); } } } } return typeFacetResults; }
protected void logReplicationDetails(Replica replica, StringBuilder builder) throws IOException { try (HttpSolrClient client = new HttpSolrClient.Builder(replica.getCoreUrl()).build()) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/replication"); params.set(ReplicationHandler.COMMAND, ReplicationHandler.CMD_DETAILS); try { QueryResponse response = client.query(params); builder.append(String.format(Locale.ROOT, "%s: %s%s", replica.getName(), response.getResponse(), System.lineSeparator())); } catch (SolrServerException e) { log.warn("Unable to ger replication details for replica {}", replica.getName(), e); } } }
@Override public Reader findByOrcidAsReader(String orcid) { SolrQuery query = new SolrQuery(); query.setQuery(ORCID + ":\"" + orcid + "\"").setFields(SCORE, ORCID, PUBLIC_PROFILE); query.add("wt", "orcidProfile"); try { QueryResponse queryResponse = solrServerForStreaming.query(query); InputStream inputStream = (InputStream) queryResponse.getResponse().get("stream"); return new InputStreamReader(inputStream, "UTF-8"); } catch (SolrServerException | SolrException e) { String errorMessage = MessageFormat.format("Error when attempting to retrieve stream for orcid {0}", new Object[] { orcid }); throw new NonTransientDataAccessResourceException(errorMessage, e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * @param query the query in the main index * @param from the field to join from (in the joined index) * @param to the field to join to (in the main index) * @param fromIndex the index to join (if null, join is in main index * @param joinQuery * @param consumer */ public void join(SolrQuery query, String from, String to, String fromIndex, SolrQuery joinQuery, Consumer<SolrDocument> consumer) { if (query == null) throw new NullPointerException("Search query is null"); if (from == null) throw new NullPointerException("from is null"); if (to == null) throw new NullPointerException("to is null"); if (fromIndex == null) throw new NullPointerException("fromIndex is null"); query.setRows(this._row); query.setStart(this._start); // add the filter query String otherIndex = fromIndex == null ? "" : " fromIndex=" + fromIndex; query.addFilterQuery("{!join from=" + from + " to=" + to + otherIndex + "}" + joinQuery.get("q")); LOGGER.info("query {}", query); QueryResponse response = this._solr.query(query); if (response != null) { LOGGER.info("response size {}", response.getResponse().size()); // standard query SolrDocumentList list = response.getResults(); if (list != null) { list.forEach(consumer); } } }
private static HashMap<FieldDescriptor, TermFacetResult<?>> getTermFacetResults(QueryResponse response, DocumentFactory factory, DocumentFactory childFactory, Map<String,Facet> facetsQuery, String searchContext) { final HashMap<FieldDescriptor, TermFacetResult<?>> facets = new HashMap<>(); if (Objects.nonNull(response.getResponse())) { final SimpleOrderedMap jsonFacetResult = (SimpleOrderedMap) response.getResponse().get("facets"); if (Objects.nonNull(jsonFacetResult)) { for (int i = 0; i < jsonFacetResult.size(); i++) {
public NamedList<Double> call() throws Exception { ModifiableSolrParams params = new ModifiableSolrParams(); HttpSolrClient solrClient = cache.getHttpSolrClient(baseUrl); params.add(DISTRIB, "false"); params.add("fq","{!significantTerms}"); for(String key : paramsMap.keySet()) { params.add(key, paramsMap.get(key)); } params.add("minDocFreq", Float.toString(minDocFreq)); params.add("maxDocFreq", Float.toString(maxDocFreq)); params.add("minTermLength", Integer.toString(minTermLength)); params.add("field", field); params.add("numTerms", String.valueOf(numTerms*5)); QueryRequest request= new QueryRequest(params); QueryResponse response = request.process(solrClient); NamedList res = response.getResponse(); return res; } }
public NamedList<Double> call() throws Exception { ModifiableSolrParams params = new ModifiableSolrParams(); HttpSolrClient solrClient = cache.getHttpSolrClient(baseUrl); params.add(DISTRIB, "false"); params.add("fq","{!igain}"); for(String key : paramsMap.keySet()) { params.add(key, paramsMap.get(key)); } params.add("outcome", outcome); params.add("positiveLabel", Integer.toString(positiveLabel)); params.add("field", field); params.add("numTerms", String.valueOf(numTerms)); QueryRequest request= new QueryRequest(params); QueryResponse response = request.process(solrClient); NamedList res = response.getResponse(); return res; } }
/** * @param query the {@link SolrQuery} * @param consumer how to handle the results * @param groupingField the name of the field to group with * @param groupLimit the number of results per group */ public void group(SolrQuery query, Consumer<GroupCommand> consumer, String groupingField, int groupLimit) { if (query == null) throw new NullPointerException("Search query is null"); if (groupingField == null) throw new NullPointerException("Grouping field is null"); LOGGER.info("query {}", query.toQueryString()); query.setRows(this._row); query.setStart(this._start); // grouping query.setParam("group", true); //the number of groups that matches the the query query.setParam("group.ngroups", true); // the grouping field query.setParam("group.field", groupingField); // number of group limit query.setParam("group.limit", String.valueOf(groupLimit)); QueryResponse response = this._solr.query(query); if (response != null) { LOGGER.info("response size {}", response.getResponse().size()); GroupResponse groupResponse = response.getGroupResponse(); groupResponse.getValues().forEach(consumer); } }
protected void pingSolr() { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/admin/cores"); params.set("action", "STATUS"); QueryResponse response = basicQuery(params); if(response != null && response.getStatus() == 0) { NamedList<Object> results = response.getResponse(); @SuppressWarnings("unchecked") NamedList<Object> report = (NamedList<Object>)results.get("status"); Iterator<Map.Entry<String, Object>> coreIterator = report.iterator(); List<String> cores = new ArrayList<String>(report.size()); while(coreIterator.hasNext()) { Map.Entry<String, Object> core = coreIterator.next(); cores.add(core.getKey()); } registerCores(cores); setSolrActive(true); } else { setSolrActive(false); } }
/** * Returns a Document representing the specified document ID (combination of resource and context), or * null when no such Document exists yet. * * @throws SolrServerException */ @Override protected SearchDocument getDocument(String id) throws IOException { SolrDocument doc; try { doc = (SolrDocument)client.query(new SolrQuery().setRequestHandler("/get").set( SearchFields.ID_FIELD_NAME, id)).getResponse().get("doc"); } catch (SolrServerException e) { throw new IOException(e); } return (doc != null) ? new SolrSearchDocument(doc) : null; }
protected long getIndexVersion(Replica replica) throws IOException { try (HttpSolrClient client = new HttpSolrClient.Builder(replica.getCoreUrl()).build()) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/replication"); params.set(ReplicationHandler.COMMAND, ReplicationHandler.CMD_SHOW_COMMITS); try { QueryResponse response = client.query(params); @SuppressWarnings("unchecked") List<NamedList<Object>> commits = (List<NamedList<Object>>)response.getResponse().get(ReplicationHandler.CMD_SHOW_COMMITS); Collections.max(commits, (a,b)->((Long)a.get("indexVersion")).compareTo((Long)b.get("indexVersion"))); return (long) Collections.max(commits, (a,b)->((Long)a.get("indexVersion")).compareTo((Long)b.get("indexVersion"))).get("indexVersion"); } catch (SolrServerException e) { log.warn("Exception getting version from {}, will return an invalid version to retry.", replica.getName(), e); return -1; } } }
protected void pingSolr() { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("qt", "/admin/cores"); params.set("action", "STATUS"); QueryResponse response = basicQuery(params); if(response != null && response.getStatus() == 0) { NamedList<Object> results = response.getResponse(); @SuppressWarnings("unchecked") NamedList<Object> report = (NamedList<Object>)results.get("status"); Iterator<Map.Entry<String, Object>> coreIterator = report.iterator(); List<String> cores = new ArrayList<String>(report.size()); while(coreIterator.hasNext()) { Map.Entry<String, Object> core = coreIterator.next(); cores.add(core.getKey()); } registerCores(cores); setSolrActive(true); } else { setSolrActive(false); } }
/** * Returns a Document representing the specified document ID (combination of * resource and context), or null when no such Document exists yet. * * @throws SolrServerException */ @Override protected SearchDocument getDocument(String id) throws IOException { SolrDocument doc; try { doc = (SolrDocument)client.query( new SolrQuery().setRequestHandler("/get").set(SearchFields.ID_FIELD_NAME, id)).getResponse().get( "doc"); } catch (SolrServerException e) { throw new IOException(e); } return (doc != null) ? new SolrSearchDocument(doc) : null; }
NamedList res = response.getResponse();
public static GetResult buildRealTimeGetResult(QueryResponse response, RealTimeGet query, DocumentFactory factory) { final String DOC = "doc"; long nResults = 0; List<Document> docResults = new ArrayList<>(); final SolrDocumentList results = response.getResults(); if(results != null && results.size() >0){ docResults = buildResultList(results, null, factory, null); nResults = docResults.size(); } else { final SolrDocument solrDoc = (SolrDocument)response.getResponse().get(DOC); if(solrDoc != null) { final SolrDocumentList solrDocuments = new SolrDocumentList(); solrDocuments.add(solrDoc); docResults = buildResultList(solrDocuments, null, factory, null); nResults = 1; } } return new GetResult(nResults,docResults,query,factory,response.getQTime()).setElapsedTime(response.getElapsedTime()); }
@Override public T get(K key, String[] fields) throws GoraException { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(CommonParams.QT, "/get"); params.set(CommonParams.FL, toDelimitedString(fields, ",")); params.set("id", key.toString()); try { QueryResponse rsp = server.query(params); Object o = rsp.getResponse().get("doc"); if (o == null) { return null; } return newInstance((SolrDocument) o, fields); } catch (Exception e) { throw new GoraException(e); } }
private String getFieldNameOfUniqueKey() { final SolrQuery solrQuery = new SolrQuery(); try { solrQuery.setRequestHandler("/schema/uniquekey"); final QueryRequest req = new QueryRequest(solrQuery); if (isBasicAuthEnabled()) { req.setBasicAuthCredentials(getUsername(), getPassword()); } return(req.process(getSolrClient()).getResponse().get("uniqueKey").toString()); } catch (SolrServerException | IOException e) { getLogger().error("Solr query to retrieve uniqueKey-field failed due to {}", new Object[]{solrQuery.toString(), e}, e); throw new ProcessException(e); } }
@Override public String getDatabaseProductVersion() throws SQLException { // Returns the version for the first live node in the Solr cluster. SolrQuery sysQuery = new SolrQuery(); sysQuery.setRequestHandler("/admin/info/system"); CloudSolrClient cloudSolrClient = this.connection.getClient(); Set<String> liveNodes = cloudSolrClient.getZkStateReader().getClusterState().getLiveNodes(); SolrClient solrClient = null; for (String node : liveNodes) { try { String nodeURL = cloudSolrClient.getZkStateReader().getBaseUrlForNodeName(node); solrClient = new Builder(nodeURL).build(); QueryResponse rsp = solrClient.query(sysQuery); return String.valueOf(((SimpleOrderedMap) rsp.getResponse().get("lucene")).get("solr-spec-version")); } catch (SolrServerException | IOException ignore) { return ""; } finally { if (solrClient != null) { try { solrClient.close(); } catch (IOException ignore) { // Don't worry about failing to close the Solr client } } } } // If no version found just return empty string return ""; }