Set<String> returnFields = rb.rsp.getReturnFields(); if (returnFields != null) SolrHighlighter highligher = rb.req.getCore().getHighlighter(); if (highligher.isHighlightingEnabled(rb.req.getParams())) SchemaField keyField = rb.req.getSearcher().getSchema().getUniqueKeyField(); if (null != keyField) if (!returnFields.contains(keyField)) fieldFilter.add(ByteBufferUtil.bytes(keyField.getName())); rb.req.getSearcher().getReader().document(docIds.get(0), selector);
@Override public void prepare(ResponseBuilder rb) throws IOException { if( SolandraComponent.prepare(rb) ) { ModifiableSolrParams params = new ModifiableSolrParams(rb.req.getParams()); if(params.get(ShardParams.SHARDS_QT) == null) { params.add(ShardParams.SHARDS_QT, "/terms"); } rb.req.setParams(params); } super.prepare(rb); } }
if (!(((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader() instanceof lucandra.IndexReader)) return false; if (rb.req.getParams().getBool(ShardParams.IS_SHARD, false)) String indexName = (String) rb.req.getContext().get("solandra-index"); IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader(); String indexName = (String) rb.req.getContext().get("solandra-index"); IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()) .getWrappedReader();
public void execute(SolrRequestHandler handler, SolrQueryRequest req, SolrQueryResponse rsp) { if (handler==null) { log.warn(logid+"Null Request Handler '" + req.getParams().get(CommonParams.QT) +"' :" + req); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"Null Request Handler '" + req.getParams().get(CommonParams.QT) + "'", true); } // setup response header and handle request final NamedList<Object> responseHeader = new SimpleOrderedMap<Object>(); rsp.add("responseHeader", responseHeader); NamedList toLog = rsp.getToLog(); //toLog.add("core", getName()); toLog.add("webapp", req.getContext().get("webapp")); toLog.add("path", req.getContext().get("path")); toLog.add("params", "{" + req.getParamString() + "}"); handler.handleRequest(req,rsp); setResponseHeaderValues(handler,req,rsp); StringBuilder sb = new StringBuilder(); for (int i=0; i<toLog.size(); i++) { String name = toLog.getName(i); Object val = toLog.getVal(i); sb.append(name).append("=").append(val).append(" "); } log.info(logid + sb.toString()); /*log.info(logid+"" + req.getContext().get("path") + " " + req.getParamString()+ " 0 "+ (int)(rsp.getEndTime() - req.getStartTime()));*/ }
@Override public void process(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); if (!params.getBool(COMPONENT_NAME, false)) { return; throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Illegal query type. The incoming query must be a Lucene SpanNearQuery and it was a " + origQuery.getClass().getName()); SolrIndexSearcher searcher = rb.req.getSearcher(); IndexReader reader = searcher.getIndexReader(); Spans spans = sQuery.getSpans(reader); createWeights(params.get(CommonParams.Q), sQuery, termWeights, bigramWeights, reader); float adjWeight = params.getFloat(ADJACENT_WEIGHT, DEFAULT_ADJACENT_WEIGHT); addPassage(tvm.passage, rankedPassages, termWeights, bigramWeights, adjWeight, secondAdjWeight, bigramWeight); } catch (CloneNotSupportedException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Internal error cloning Passage", e); NamedList qaResp = new NamedList(); rb.rsp.add("qaResponse", qaResp); SchemaField uniqField = rb.req.getSchema().getUniqueKeyField(); if (rankedPassages.size() > 0) { int size = Math.min(rows, rankedPassages.size());
@Override @SuppressWarnings("unchecked") public void process(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) { return; boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD)); String q = params.get(SPELLCHECK_Q); SolrSpellChecker spellChecker = getSpellChecker(params); Collection<Token> tokens = null; throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The spellcheck.q parameter is required."); } else { Integer hitsInteger = (Integer) rb.rsp.getToLog().get("hits"); long hits = 0; if (hitsInteger == null) { IndexReader reader = rb.req.getSearcher().getIndexReader(); SpellingOptions options = new SpellingOptions(tokens, reader, count, alternativeTermCount, suggestMode, extendedResults, accuracy, response.add("suggestions", suggestions); rb.rsp.add("spellcheck", response); throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Specified dictionaries do not exist: " + getDictionaryNameAsSingleString(getDictionaryNames(params)));
@Override public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException { String xsl = request.getParams().get("lux.xml-xsl-stylesheet"); @SuppressWarnings("unchecked") List<String> errors = response.getValues().getAll("xpath-error"); String contentType = getContentType (request, response); NamedList<?> values = (NamedList<?>) response.getValues().get("xpath-results"); if (values == null && !errors.isEmpty()) { StringBuilder buf = new StringBuilder(); buf.append(e).append("\n"); throw new SolrException(ErrorCode.BAD_REQUEST, buf.toString()); } else if (response.getException() != null) { String error = (String) ((NamedList<?>) response.getValues().get("error")).get("msg"); if (error == null) { error = response.getException().toString();
private void addDebugInfo(SolrQueryRequest req, SolrQueryResponse rsp, String q, List<Query> mltFqFilters, MLTResult mltResult) { DocListAndSet mltDocs = mltResult.getResults(); boolean dbg = req.getParams().getBool(CommonParams.DEBUG_QUERY, false); boolean dbgQuery = false, dbgResults = false; if (dbg == false){//if it's true, we are doing everything anyway. String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG); if (dbgParams != null) { for (int i = 0; i < dbgParams.length; i++) { NamedList<Object> dbgInfo = new NamedList<Object>(); NamedList<Object> stdDbg = SolrPluginUtils.doStandardDebug(req, q, mltResult.getQuery(), mltDocs.docList, dbgQuery, dbgResults); if (null != dbgInfo) { rsp.add("debug", dbgInfo); dbgInfo.add( "mltTerms", it ); dbgInfo.addAll(stdDbg); dbgInfo.add("filter_queries",req.getParams().getParams(CommonParams.FQ)); List<String> fqs = new ArrayList<String>(mltFqFilters.size()); for (Query fq : mltFqFilters) { fqs.add(QueryParsing.toString(fq, req.getSchema())); dbgInfo.add("mlt_filter_queries",fqs); SolrException.log(log, "Exception during debug", e); rsp.add("exception_during_debug", SolrException.toStr(e));
ContentStream stream, UpdateRequestProcessor processor) throws Exception { Parser parser = null; String streamType = req.getParams().get(ExtractingParams.STREAM_TYPE, null); if (streamType != null) { String resourceName = req.getParams().get(ExtractingParams.RESOURCE_NAME, null); if (resourceName != null) { metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, resourceName); String xpathExpr = params.get(ExtractingParams.XPATH_EXPRESSION); boolean extractOnly = params.getBool(ExtractingParams.EXTRACT_ONLY, false); SolrContentHandler handler = factory.createSolrContentHandler(metadata, params, req.getSchema()); ContentHandler parsingHandler = handler; InputStream is = req.getCore().getResourceLoader().openResource(pwMapFile); if(is != null) { log.debug("Password file supplied: "+pwMapFile); .append(". metadata=").append(metadata.toString()).toString()); else throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); serializer.endDocument(); rsp.add(stream.getName() + "_metadata", metadataNL); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Stream type of " + streamType + " didn't match any known parsers. Please supply the " + ExtractingParams.STREAM_TYPE + " parameter.");
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { rsp.setHttpCaching(false); final SolrParams solrParams = req.getParams(); String dicPath = solrParams.get("dicPath"); Dictionary dict = Utils.getDict(dicPath, loader); NamedList<Object> result = new NamedList<Object>(); result.add("dicPath", dict.getDicPath().toURI()); boolean check = solrParams.getBool("check", false); //仅仅用于检测词库是否有变化 //用于尝试加载词库,有此参数, check 参数可以省略。 boolean reload = solrParams.getBool("reload", false); check |= reload; boolean changed = false; boolean reloaded = false; if(check) { changed = dict.wordsFileIsChange(); result.add("changed", changed); } if(changed && reload) { reloaded = dict.reload(); result.add("reloaded", reloaded); } rsp.add("result", result); }
/** * Handle Custom Action. * <p/> * This method could be overridden by derived classes to handle custom actions. <br> By default - this method throws a * solr exception. Derived classes are free to write their derivation if necessary. */ protected boolean handleCustomAction(SolrQueryRequest req, SolrQueryResponse rsp) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unsupported operation: " + req.getParams().get(CoreAdminParams.ACTION)); }
@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); } } }
@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); }
public static SolrQueryRequest substituteField(SolrQueryRequest req){ SolrParams params = req.getParams(); String query = params.get("q"); System.out.println("query before ="+query); query = query.replace(' ', '_'); System.out.println("query after ="+query); NamedList values = params.toNamedList(); values.remove("q"); values.add("q", query); params = SolrParams.toSolrParams(values); req.setParams(params); return req; } }
@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); }
@Override public void process(ResponseBuilder rb) throws IOException { if(isEnabled(rb)){ long startTime = System.currentTimeMillis(); SolrParams params = rb.req.getParams(); int topN = getTopN(params); boolean binary = getBinary(params); boolean logTfs = getLogTfs(params); boolean includeExisting = getIncludeExisting(params); final SolrIndexSearcher searcher = rb.req.getSearcher(); IndexReader ir = searcher.getIndexReader(); Analyzer analyzer = searcher.getSchema().getIndexAnalyzer(); DocListAndSet docs = rb.getResults(); DocIterator iterator = docs.docList.iterator(); String uniqueKeyField = searcher.getSchema().getUniqueKeyField().getName(); NamedList<NamedList<Double>> topPredictions = new NamedList<NamedList<Double>>(); while(iterator.hasNext()) { int docNum = iterator.nextDoc(); Map<String, Map<String,Integer>> tf = getFieldTermFrequencyCounts(fields, ir, analyzer, docNum); NamedList<Double> predictions = predict(tf, topN, binary, logTfs, includeExisting); String uniqueFieldValue = getUniqueKeyFieldValue(ir, analyzer, uniqueKeyField, docNum); topPredictions.add(String.format("%s:%s", uniqueKeyField, uniqueFieldValue), predictions); } long duration = System.currentTimeMillis() - startTime; NamedList<Object> results = new NamedList<Object>(); results.add("Time", duration); results.add("values", topPredictions); rb.rsp.add(getPrefix(), results); } }
public ValueSource getValueSource(FunctionQParser fp, String arg) { if (arg==null) return null; SchemaField f = fp.req.getSchema().getField(arg); if (f.getType().getClass() == DateField.class || f.getType().getClass() == LegacyDateField.class) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't use ms() function on non-numeric legacy date field " + arg); } return f.getType().getValueSource(f, fp); }
@Override public void handleRequestBody(SolrQueryRequest request, SolrQueryResponse response) throws Exception { String searchString = request.getParams().get("searchString"); searchString = searchString.trim(); try { DLLIterator it = service.matchPrefix(searchString).iterator(); List<NamedList<String>> matches = new ArrayList<NamedList<String>>(); while (it.hasNext()) { String word = (String) it.next(); NamedList<String> node = new SimpleOrderedMap<String>(); node.add("Title", word); matches.add(node); } if (matches.size() > 0) { response.add("ResultSet", matches); } } catch (RuntimeException e) { response.add("status", "failed"); } }
/** * Handle "UNLOAD" Action * * @param req * @param rsp * * @return true if a modification has resulted that requires persistance * of the CoreContainer configuration. */ protected boolean handleUnloadAction(SolrQueryRequest req, SolrQueryResponse rsp) throws SolrException { SolrParams params = req.getParams(); String cname = params.get(CoreAdminParams.CORE); SolrCore core = coreContainer.remove(cname); if(core == null){ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No such core exists '"+cname+"'"); } core.close(); return coreContainer.isPersistent(); }
@Override @SuppressWarnings("unchecked") public void prepare(ResponseBuilder rb) throws IOException { SolrParams params = rb.req.getParams(); if (!params.getBool(COMPONENT_NAME, false)) { return; } SolrSpellChecker spellChecker = getSpellChecker(params); if (params.getBool(SPELLCHECK_BUILD, false)) { spellChecker.build(rb.req.getCore(), rb.req.getSearcher()); rb.rsp.add("command", "build"); } else if (params.getBool(SPELLCHECK_RELOAD, false)) { spellChecker.reload(rb.req.getCore(), rb.req.getSearcher()); rb.rsp.add("command", "reload"); } }