List of usage examples for org.apache.solr.client.solrj.response QueryResponse getHeader
public NamedList<Object> getHeader()
From source file:com.nridge.ds.solr.SolrResponseBuilder.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" })
private void populateHeader(QueryResponse aQueryResponse, long anOffset, long aLimit) {
Logger appLogger = mAppMgr.getLogger(this, "populateHeader");
appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);
Relationship headerRelationship = mDocument.getFirstRelationship(Solr.RESPONSE_HEADER);
if (headerRelationship != null) {
DataBag headerBag = headerRelationship.getBag();
headerBag.resetValues();/* w w w . j av a 2s . co m*/
headerBag.setValueByName("page_size", aLimit);
headerBag.setValueByName("offset_start", anOffset);
headerBag.setValueByName("query_time", aQueryResponse.getQTime());
String propertyName = getCfgPropertyPrefix() + ".request_handler";
String requestHandler = mAppMgr.getString(propertyName);
if (StringUtils.isNotEmpty(requestHandler)) {
if (requestHandler.charAt(0) == StrUtl.CHAR_FORWARDSLASH)
headerBag.setValueByName("request_handler", requestHandler);
else
headerBag.setValueByName("request_handler", StrUtl.CHAR_FORWARDSLASH + requestHandler);
}
int statusCode = aQueryResponse.getStatus();
headerBag.setValueByName("status_code", statusCode);
if (statusCode == Solr.RESPONSE_STATUS_SUCCESS)
headerBag.setValueByName("status_message", "Success");
else
headerBag.setValueByName("status_message", "Failure");
SolrDocumentList solrDocumentList = aQueryResponse.getResults();
if (solrDocumentList != null) {
Float maxScore = solrDocumentList.getMaxScore();
if (maxScore == null)
maxScore = (float) 0.0;
headerBag.setValueByName("max_score", maxScore);
headerBag.setValueByName("fetch_count", solrDocumentList.size());
long totalCount = solrDocumentList.getNumFound();
headerBag.setValueByName("total_count", totalCount);
}
NamedList<Object> headerList = aQueryResponse.getHeader();
if (headerList != null) {
NamedList<Object> paramList = (NamedList<Object>) aQueryResponse.getResponseHeader().get("params");
if (paramList != null) {
Object paramObject = paramList.get("fl");
if (paramObject != null) {
DataField dataField = headerBag.getFieldByName("field_list");
if (paramObject instanceof String)
dataField.addValue(paramObject.toString());
else if (paramObject instanceof List) {
List fieldList = (List) paramObject;
int fieldCount = fieldList.size();
if (fieldCount > 0) {
for (int i = 0; i < fieldCount; i++)
dataField.addValue(fieldList.get(i).toString());
}
}
}
paramObject = paramList.get("hl");
if (paramObject != null) {
DataField dataField = headerBag.getFieldByName("is_highlighted");
if (paramObject instanceof String) {
if (StringUtils.equalsIgnoreCase(paramObject.toString(), "on"))
dataField.setValue(true);
}
}
}
}
}
appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}
From source file:org.apache.jackrabbit.oak.plugins.index.solr.query.SolrQueryIndex.java
License:Apache License
private AbstractIterator<SolrResultRow> getIterator(final Filter filter, final IndexPlan plan, final String parent, final int parentDepth, final OakSolrConfiguration configuration, final SolrServer solrServer, final LMSEstimator estimator) { return new AbstractIterator<SolrResultRow>() { public Collection<FacetField> facetFields = new LinkedList<FacetField>(); private final Set<String> seenPaths = Sets.newHashSet(); private final Deque<SolrResultRow> queue = Queues.newArrayDeque(); private int offset = 0; private boolean noDocs = false; private long numFound = 0; @Override/* w w w.j ava2s .c o m*/ protected SolrResultRow computeNext() { if (!queue.isEmpty() || loadDocs()) { return queue.remove(); } return endOfData(); } private SolrResultRow convertToRow(SolrDocument doc) { String path = String.valueOf(doc.getFieldValue(configuration.getPathField())); if ("".equals(path)) { path = "/"; } if (!parent.isEmpty()) { path = getAncestorPath(path, parentDepth); // avoid duplicate entries if (seenPaths.contains(path)) { return null; } seenPaths.add(path); } float score = 0f; Object scoreObj = doc.get("score"); if (scoreObj != null) { score = (Float) scoreObj; } return new SolrResultRow(path, score, doc, facetFields); } /** * Loads the Solr documents in batches * @return true if any document is loaded */ private boolean loadDocs() { if (noDocs) { return false; } try { if (log.isDebugEnabled()) { log.debug("converting filter {}", filter); } SolrQuery query = FilterQueryParser.getQuery(filter, plan, configuration); if (numFound > 0) { long rows = configuration.getRows(); long maxQueries = numFound / 2; if (maxQueries > configuration.getRows()) { // adjust the rows to avoid making more than 3 Solr requests for this particular query rows = maxQueries; query.setParam("rows", String.valueOf(rows)); } long newOffset = configuration.getRows() + offset * rows; if (newOffset >= numFound) { return false; } query.setParam("start", String.valueOf(newOffset)); offset++; } if (log.isDebugEnabled()) { log.debug("sending query {}", query); } QueryResponse queryResponse = solrServer.query(query); if (log.isDebugEnabled()) { log.debug("getting response {}", queryResponse.getHeader()); } SolrDocumentList docs = queryResponse.getResults(); if (docs != null) { numFound = docs.getNumFound(); estimator.update(filter, docs); Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); for (SolrDocument doc : docs) { // handle highlight if (highlighting != null) { Object pathObject = doc.getFieldValue(configuration.getPathField()); if (pathObject != null && highlighting.get(String.valueOf(pathObject)) != null) { Map<String, List<String>> value = highlighting.get(String.valueOf(pathObject)); for (Map.Entry<String, List<String>> entry : value.entrySet()) { // all highlighted values end up in 'rep:excerpt', regardless of field match for (String v : entry.getValue()) { doc.addField(QueryImpl.REP_EXCERPT, v); } } } } SolrResultRow row = convertToRow(doc); if (row != null) { queue.add(row); } } } // get facets List<FacetField> returnedFieldFacet = queryResponse.getFacetFields(); if (returnedFieldFacet != null) { facetFields.addAll(returnedFieldFacet); } // filter facets on doc paths if (!facetFields.isEmpty() && docs != null) { for (SolrDocument doc : docs) { String path = String.valueOf(doc.getFieldValue(configuration.getPathField())); // if facet path doesn't exist for the calling user, filter the facet for this doc for (FacetField ff : facetFields) { if (!filter.isAccessible(path + "/" + ff.getName())) { filterFacet(doc, ff); } } } } // handle spellcheck SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse(); if (spellCheckResponse != null && spellCheckResponse.getSuggestions() != null && spellCheckResponse.getSuggestions().size() > 0) { putSpellChecks(spellCheckResponse, queue, filter, configuration, solrServer); noDocs = true; } // handle suggest NamedList<Object> response = queryResponse.getResponse(); Map suggest = (Map) response.get("suggest"); if (suggest != null) { Set<Map.Entry<String, Object>> suggestEntries = suggest.entrySet(); if (!suggestEntries.isEmpty()) { putSuggestions(suggestEntries, queue, filter, configuration, solrServer); noDocs = true; } } } catch (Exception e) { if (log.isWarnEnabled()) { log.warn("query via {} failed.", solrServer, e); } } return !queue.isEmpty(); } }; }
From source file:org.eclipse.orion.internal.server.search.SearchServlet.java
License:Open Source License
/** * Writes the response to the search query to the HTTP response's output stream. *///from w w w .j a va 2s .c o m private void writeResponse(SolrQuery query, HttpServletRequest httpRequest, HttpServletResponse httpResponse, QueryResponse queryResponse) throws IOException { SolrCore core = SearchActivator.getInstance().getSolrCore(); //this seems to be the only way to obtain the JSON response representation SolrQueryRequest solrRequest = new LocalSolrQueryRequest(core, query.toNamedList()); SolrQueryResponse solrResponse = new SolrQueryResponse(); //bash the query in the response to remove user info NamedList<Object> params = (NamedList<Object>) queryResponse.getHeader().get("params"); //$NON-NLS-1$ params.remove(CommonParams.Q); params.add(CommonParams.Q, httpRequest.getParameter(CommonParams.Q)); solrResponse.setAllValues(queryResponse.getResponse()); QueryResponseWriter writer = core.getQueryResponseWriter("json"); //$NON-NLS-1$ writer.write(httpResponse.getWriter(), solrRequest, solrResponse); }
From source file:peltomaa.sukija.QueryResponsePrinter.java
License:Open Source License
public static final void print(PrintStream out, QueryResponse response) { NamedList<Object> nl = response.getHeader(); out.println(nl.toString());/*from ww w .j a v a 2 s.c o m*/ TermsResponse tr = response.getTermsResponse(); Map<String, List<TermsResponse.Term>> tmap = tr.getTermMap(); Iterator<Map.Entry<String, List<TermsResponse.Term>>> i = tmap.entrySet().iterator(); while (i.hasNext()) { Map.Entry<String, List<TermsResponse.Term>> e = i.next(); out.println(e.getKey()); for (TermsResponse.Term m : e.getValue()) { out.println(m.getTerm() + " " + m.getFrequency()); } } out.println("========================="); List<TermsResponse.Term> tterms = tr.getTerms("text"); for (TermsResponse.Term t : tr.getTerms("text")) { // out.println (t.getTerm()); } }