/** * Returns spellchecker's suggestions from original response. In case there are no suggestions, returns null. * * @param rb . * @return . */ public static NamedList extractSpellcheckerSuggestions(ResponseBuilder rb) { if (rb.rsp.getValues().get("spellcheck") == null) { return null; } return (NamedList) ((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("suggestions"); }
public static void addComponentResponsesToResult(SolrQueryResponse rsp, SolrQueryResponse newRsp, String prefix) { rsp.add(prefix + "_response", newRsp.getValues().get("response")); rsp.add(prefix + "_grouped", newRsp.getValues().get("grouped")); rsp.add(prefix + "_facet_counts", newRsp.getValues().get("facet_counts")); rsp.add(prefix + "_terms", newRsp.getValues().get("terms")); rsp.add(prefix + "_termVectors", newRsp.getValues().get("termVectors")); rsp.add(prefix + "_highlighting", newRsp.getValues().get("highlighting")); rsp.add(prefix + "_stats", newRsp.getValues().get("stats")); } }
private String getSpellCheckedQuery(SolrQueryResponse rsp) { //check if spellcheck result exists. if(rsp.getValues().get("spellcheck") == null) return null; final NamedList collations = (NamedList)((NamedList)rsp.getValues().get("spellcheck")).get("collations"); if(collations != null && collations.size() > 0) { String s = (String)collations.get("collation"); return s.substring(0,s.length()-1); } else { final String s = (String)((NamedList)((NamedList)rsp.getValues().get("spellcheck")).get("suggestions")).get("collation"); return s != null ? s.substring(0,s.length()-1) : null; } }
/** * Extract spellchecker's collation from original response or null if there was no collation. * @param rb . * @return . */ public static String extractSpellcheckerCollation(ResponseBuilder rb) { if (rb.rsp.getValues().get("spellcheck") != null) { if (((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations") != null) { return (String) (((NamedList) ((SimpleOrderedMap) rb.rsp.getValues().get("spellcheck")).get("collations")).get("collation")); } } return null; }
private long getResultNumFound (SolrQueryResponse rsp) { Object docs = response.getValues().get("response"); if (docs != null) { if (docs instanceof DocList) { return ((DocList)docs).matches(); } else if (docs instanceof SolrDocumentList) { return ((SolrDocumentList)docs).getNumFound(); } } return 0; }
@Override public void handleRequestBody(SolrQueryRequest solrQueryRequest, SolrQueryResponse solrQueryResponse) throws Exception { // delegate to the base class to check the status of this local index super.handleRequestBody(solrQueryRequest, solrQueryResponse); // if ping status is OK, then check cluster state of this core if ("OK".equals(solrQueryResponse.getValues().get("status"))) { verifyThisReplicaIsActive(solrQueryRequest.getCore()); } }
NamedList<Object> values = rsp.getValues(); values.remove("response"); values.add("response", buf.toString().trim());
private SuggestionResult[] getSuggestionResults(String query, String df, String[] singleValueFields, String[] multiValueFields, int termLimit, int limit, SuggestionRequestHandler.LimitType limitType, SuggestionRequestHandler.Type type, SuggestionRequestHandler.Strategy strategy, String suggestionField, Map<String, Map<String, Object>> intervals, SolrQueryResponse response) { final SuggestionResult[] result; if(response.getValues().get("facets") instanceof SimpleOrderedMap) { final SimpleOrderedMap facets = (SimpleOrderedMap) response.getValues().get("facets"); if((Integer) facets.get("count") > 0) { result = createResults(response, singleValueFields, multiValueFields, query, df,type,termLimit, limit, limitType, strategy,suggestionField,intervals); } else { result = null; } } else { final SolrDocumentList facets = (SolrDocumentList) response.getValues().get("facet"); if(facets.getNumFound() > 0) { result = createResults(response, singleValueFields, multiValueFields, query, df,type,termLimit, limit, limitType, strategy,suggestionField,intervals); } else { result = null; } } return result; }
SuggestionResultMulti result = new SuggestionResultMulti(limit, limitType); SimpleOrderedMap facets = (SimpleOrderedMap)((SimpleOrderedMap)rsp.getValues().get("facet_counts")).get("facet_fields");
private static ResponseFormatDocs getResponseFormatDocs(final SolrQueryResponse rsp) { SolrDocumentList docList = (SolrDocumentList)rsp.getValues().get("response"); SolrDocument singleDoc = (SolrDocument)rsp.getValues().get("doc"); if (docList == null && singleDoc == null) { return null; // no documents to filter } if (docList != null && singleDoc != null) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Not able to filter secure reponse, RealTimeGet returned both a doc list and " + "an individual document"); } final boolean useResponseField = docList != null; if (docList == null) { docList = new SolrDocumentList(); docList.add(singleDoc); } return new ResponseFormatDocs(useResponseField, docList); }
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp){ NamedList list = rsp.getValues(); rsp1.setAllValues(rsp.getValues().clone()); rsp2.setAllValues(rsp.getValues().clone()); rsp3.setAllValues(rsp.getValues().clone()); NamedList values = rsp1.getValues(); ResultContext c = (ResultContext) values.get("response"); if (c!=null){ rsp.setAllValues(rsp1.getValues()); return; values = rsp2.getValues(); c = (ResultContext) values.get("response"); if (c!=null){ rsp.setAllValues(rsp2.getValues()); return; rsp.setAllValues(rsp3.getValues());
@Override public void write(Writer w, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException { SolrIndexSearcher searcher = req.getSearcher(); NamedList nl = rsp.getValues(); int sz = nl.size(); for (int li = 0; li < sz; li++) { Object val = nl.getVal(li); if (val instanceof DocList) { //<co id="co.fuzzy.type-ahead.doclist"/> DocList dl = (DocList) val; DocIterator iterator = dl.iterator(); w.append("<ul>\n"); while (iterator.hasNext()) { int id = iterator.nextDoc(); Document doc = searcher.doc(id, fields); //<co id="co.fuzzy.type-ahead.search"/> String name = doc.get("word"); w.append("<li>" + name + "</li>\n"); } w.append("</ul>\n"); } } } }
final SolrQueryRequest request, final SolrQueryResponse response) throws IOException { final NamedList<?> values = response.getValues(); final Query query = (Query)request.getContext().get(Names.QUERY); final QueryExecution execution = (QueryExecution)response.getValues().get(Names.QUERY_EXECUTION); try { final boolean isHybridMode = Boolean.TRUE.equals(request.getContext().get(Names.HYBRID_MODE));
@SuppressWarnings("unchecked") protected void performHighlighting(ReSearcherRequestContext ctx, ResponseBuilder rb) { // check if highlighting is needed SolrParams params = rb.req.getParams(); String highlightRemoved = params.get(RES_HIGHLIGHT_REMOVED_TAG_PARAM_NAME); String highlightReplaced = params.get(RES_HIGHLIGHT_REPLACED_TAG_PARAM_NAME); boolean ignoreQuotes = params.get(RES_HIGHLIGHT_IGNORE_QUOTES_PARAM_NAME, "true").equals("true") ? true : false; if (highlightRemoved == null && highlightReplaced == null) { // nothing to do, so just return; return; } // else, fetch suggestions produced by the component and perform highlighting List<String> sugs = (List<String>) rb.rsp.getValues().get(getSuggestionsTagName()); if (sugs == null) { // if component didn't generate suggestions, there is nothing to highlight return; } List<String> sugsHighlighted = new ArrayList<String>(); for (String sug : sugs) { sugsHighlighted.add(CorrectionHighlighter.highlightCorrections(ctx.getOriginalQueryString(), sug, highlightRemoved, highlightReplaced, ignoreQuotes)); } rb.rsp.getValues().add(getSuggestionsTagName() + "_highlighted", sugsHighlighted); }
rb.rsp.add("spellchecked_response", rsp.getValues().get("response")); rb.rsp.add("spellchecked_facet_counts", rsp.getValues().get("facet_counts")); rb.rsp.add("spellchecked_terms", rsp.getValues().get("terms")); rb.rsp.add("spellchecked_termVectors", rsp.getValues().get("termVectors")); rb.rsp.add("spellchecked_highlighting", rsp.getValues().get("highlighting")); rb.rsp.add("spellchecked_stats", rsp.getValues().get("stats")); rb.rsp.add("spellchecked_grouped", rsp.getValues().get("grouped")); return true;
public void run(SolrQueryResponse rsp, SolrParams params, String query, String df, String[] fields, String[] singlevalue_fields, String[] multiValueFields, String[] fqs, int termLimit, int limit, SuggestionRequestHandler.LimitType limitType, SuggestionRequestHandler.Type type, SuggestionRequestHandler.Strategy strategy, String suggestionField, Map<String, Map<String,Object>> intervals) throws Exception { //analyze query in advance final String analyzedQuery = FieldAnalyzerService.analyzeString(solrCore, df, query); final SolrQueryResponse response = query(analyzedQuery,params,df,fields,fqs,termLimit,suggestionField,intervals); //Create the suggestion results SuggestionResult[] result; result = this.getSuggestionResults(analyzedQuery, df, singlevalue_fields, multiValueFields, termLimit, limit, limitType, type, strategy, suggestionField, intervals, response); //if no results, try spellchecker (if defined and if spellchecked query differs from original) if((Objects.isNull(result) && spellCheckEnabled)) { final String spellCheckedQuery = getSpellCheckedQuery(response); final Object spellCheckResult = response.getValues().get("spellcheck"); //query with checked query if(spellCheckedQuery != null && !analyzedQuery.equals(spellCheckedQuery)) { final SolrQueryResponse spellCheckedResponse = query(spellCheckedQuery,params,df,fields,fqs,termLimit,suggestionField,intervals); result = this.getSuggestionResults(spellCheckedQuery, df, singlevalue_fields, multiValueFields, termLimit, limit, limitType, type, strategy, suggestionField, intervals, spellCheckedResponse); //add result of spellchecker component if(spellCheckResult != null && result != null) { //TODO remove * on last position of collation rsp.add("spellcheck",spellCheckResult); } } } if(Objects.isNull(result)) { result = createEmptyResults(type,limit, limitType); } if(result[0] != null) rsp.add(SuggestionResultParams.SUGGESTIONS, result[0].write()); if(result[1] != null) rsp.add(SuggestionResultParams.MULTI_SUGGESTIONS, result[1].write()); }
return null; Object results = response.getValues().get("response"); if (results == null) { return null;
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void writeResponse() throws IOException { writer.write(XML_PROCESSING_INSTR); final String stylesheet = req.getParams().get("stylesheet"); if (isNotNullOrEmptyString(stylesheet)) { writer.write(XML_STYLESHEET); escapeAttributeValue(stylesheet, writer); writer.write(XML_STYLESHEET_END); } writer.write(RESPONSE_ROOT_ELEMENT_START); final NamedList<?> responseValues = rsp.getValues(); if (req.getParams().getBool(CommonParams.OMIT_HEADER, false)) { responseValues.remove(RESPONSE_HEADER); } else { ((NamedList)responseValues.get(RESPONSE_HEADER)).add(Names.QUERY, responseValues.remove(Names.QUERY).toString()); } for (final Entry<String, ?> entry : responseValues) { writeValue(entry.getKey(), entry.getValue(), responseValues); } writer.write(RESPONSE_ROOT_ELEMENT_END); }
@Override public AtomicValue next() throws XPathException { for (;;) { if (response == null) { getMoreTerms (); } NamedList<?> termFields = (NamedList<?>) response.getValues().get("terms"); NamedList<?> terms = (NamedList<?>) termFields.get(term.field()); if (terms.size() == 0) { return null; } int idx = pos - offset; if (idx >= terms.size()) { response = null; } else { current = terms.getName(idx); // Integer fieldTermCount = (Integer) terms.getVal(pos); pos += 1; return new StringValue(current); } } }
@Override public void process(ResponseBuilder rb) throws IOException { // Handle the initial facets super.process(rb); // And do the facet tree, if required if (rb.doFacets && rb.req.getParams().getBool(FACET_TREE, false)) { HierarchicalFacets ftp = new HierarchicalFacets(rb.req, rb.getResults().docSet, rb.req.getParams(), rb, parameters); @SuppressWarnings("rawtypes") SimpleOrderedMap<NamedList> ftpResponse = ftp.process(rb.req.getParams().getParams(FACET_TREE_FIELD)); @SuppressWarnings("unchecked") NamedList<Object> facetCounts = (NamedList<Object>) rb.rsp.getValues().get("facet_counts"); if (facetCounts != null) { facetCounts.add("facet_trees", ftpResponse); } else { facetCounts = new NamedList<>(); facetCounts.add("facet_trees", ftpResponse); rb.rsp.add("facet_counts", facetCounts); } } }