List of usage examples for org.apache.solr.response SolrQueryResponse setAllValues
public void setAllValues(NamedList<Object> nameValuePairs)
From source file:com.doculibre.constellio.wicket.servlet.SolrServletEmulator.java
License:Open Source License
@SuppressWarnings("deprecation") public void writeResponse(String solrQuery, RecordCollection collection, ConstellioUser user, HttpServletRequest request, HttpServletResponse response) { OutputStream outputStream;//from www . ja v a2 s. c o m try { outputStream = response.getOutputStream(); } catch (IOException e) { throw new RuntimeException(e); } if (user != null && !user.hasSearchPermission(collection)) { throw new RuntimeException("The user doesn't have search permission on the collection"); } else if (user == null && collection.hasSearchPermission()) { throw new RuntimeException("The collection requires a user with search permission"); } else { SimpleSearch simpleSearch = new SimpleSearch(); simpleSearch.setCollectionName(collection.getName()); if (solrQuery.contains("facet=constellio")) { solrQuery = solrQuery.replace("facet=constellio", "facet=on"); solrQuery = addConstellioFacets(simpleSearch, solrQuery, collection, user); LOGGER.info("Using constellio facets, new query is " + solrQuery); } ModifiableSolrParams solrParams = new ModifiableSolrParams( SolrRequestParsers.parseQueryString(solrQuery)); String queryType = solrParams.get("qt"); if (queryType != null && (queryType.toLowerCase().equals("spellchecker") || queryType.toLowerCase().equals("spellcheck"))) { writeSpellCheckerResponse(simpleSearch, solrParams, collection, user, outputStream); } else { String cdf = solrParams.get("cdf"); if (cdf != null && cdf.equals("true")) { SearchResultFieldServices searchResultFieldServices = ConstellioSpringUtils .getSearchResultFieldServices(); List<SearchResultField> fields = searchResultFieldServices.list(); List<String> fieldNames = new ArrayList<String>(); for (SearchResultField field : fields) { if (field.getRecordCollection().equals(collection)) { fieldNames.add(field.getIndexField().getName()); } } solrParams.add("fl", StringUtils.join(fieldNames.toArray(), ",")); solrParams.remove("cdf"); } try { SolrServer solrServer = SolrCoreContext.getSolrServer(collection); QueryResponse queryResponse = solrServer.query(solrParams); if (queryResponse.getStatus() == 0) { String ipAddress = request.getRemoteAddr(); simpleSearch.setQuery(solrParams.get("q")); StatsServices statsServices = ConstellioSpringUtils.getStatsServices(); statsServices.logSearch(simpleSearch, queryResponse, ipAddress); XMLResponseWriter xmlWriter = new XMLResponseWriter(); SolrQueryResponse sResponse = new SolrQueryResponse(); sResponse.setAllValues(queryResponse.getResponse()); try (OutputStreamWriter out = new OutputStreamWriter(outputStream)) { xmlWriter.write(out, new LocalSolrQueryRequest(null, solrParams), sResponse); out.flush(); } catch (IOException e) { throw new RuntimeException("Unable to convert Solr response into XML", e); } } } catch (SolrException e) { // if (!e.logged) LOGGER.log(Level.SEVERE, SolrException.toStr(e), e); // try { // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes()); // } catch (IOException e1) { // throw new RuntimeException(e1); // } // if (!e.logged) SolrException.log(log,e); sendErr(e.code(), SolrException.toStr(e), response, outputStream); } catch (Throwable e) { LOGGER.log(Level.SEVERE, SolrException.toStr(e), e); // try { // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes()); // } catch (IOException e1) { // throw new RuntimeException(e1); // } // SolrException.log(log,e); sendErr(500, SolrException.toStr(e), response, outputStream); } // final SolrCore core = // SolrCoreContext.getCores().getCore(collection.getName()); // // SolrServletRequest solrReq = new SolrServletRequest(core, // request); // SolrQueryRequest solrReq = new LocalSolrQueryRequest(core, // solrParams); // SolrQueryResponse solrRsp = new SolrQueryResponse(); // try { // // SolrRequestHandler handler = // core.getRequestHandler(solrReq.getParams().get(CommonParams.QT)); // if (handler==null) { // LOGGER.log(Level.WARNING, "Unknown Request Handler '" + // solrReq.getParams().get(CommonParams.QT) // + "' :" + solrReq); // // log.warn("Unknown Request Handler '" + // solrReq.getQueryType() +"' :" + solrReq); // throw new // SolrException(SolrException.ErrorCode.BAD_REQUEST,"Unknown Request Handler '" // + solrReq.getParams().get(CommonParams.QT) + "'"); // } // core.execute(handler, solrReq, solrRsp ); // if (solrRsp.getException() == null) { // String ipAddress = request.getRemoteAddr(); // // simpleSearch.setQuery(solrParams.get("q")); // QueryResponse qrsp = new QueryResponse(); // qrsp.setResponse(getParsedResponse(solrReq, solrRsp)); // // StatsServices statsServices = // ConstellioSpringUtils.getStatsServices(); // statsServices.logSearch(simpleSearch, qrsp, ipAddress); // // QueryResponseWriter responseWriter = // core.getQueryResponseWriter(solrReq); // // Now write it out // final String ct = responseWriter.getContentType(solrReq, // solrRsp); // // don't call setContentType on null // if (null != ct) response.setContentType(ct); // if (responseWriter instanceof BinaryQueryResponseWriter) { // BinaryQueryResponseWriter binWriter = // (BinaryQueryResponseWriter) responseWriter; // binWriter.write(outputStream, solrReq, solrRsp); // } else { // String charset = // ContentStreamBase.getCharsetFromContentType(ct); // Writer out = (charset == null || // charset.equalsIgnoreCase("UTF-8")) // ? new OutputStreamWriter(outputStream, UTF8) // : new OutputStreamWriter(outputStream, charset); // out = new FastWriter(out); // responseWriter.write(out, solrReq, solrRsp); // out.flush(); // } // } else { // Exception e = solrRsp.getException(); // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e); // // // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes()); // int rc=500; // if (e instanceof SolrException) { // rc=((SolrException)e).code(); // } // sendErr(rc, SolrException.toStr(e), response, outputStream); // } // } catch (SolrException e) { // // if (!e.logged) // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e); // // try { // // // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes()); // // } catch (IOException e1) { // // throw new RuntimeException(e1); // // } // // if (!e.logged) SolrException.log(log,e); // sendErr(e.code(), SolrException.toStr(e), response, // outputStream); // } catch (Throwable e) { // LOGGER.log(Level.SEVERE, SolrException.toStr(e), e); // // try { // // // outputStream.write(ExceptionUtils.getFullStackTrace(e).getBytes()); // // } catch (IOException e1) { // // throw new RuntimeException(e1); // // } // // SolrException.log(log,e); // sendErr(500, SolrException.toStr(e), response, outputStream); // } finally { // // This releases the IndexReader associated with the request // solrReq.close(); // } } } }
From source file:com.search.MySearchHandlerTest.java
License:Apache License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { // int sleep = req.getParams().getInt("sleep",0); // if (sleep > 0) {log.error("SLEEPING for " + sleep); Thread.sleep(sleep);} /*** Pre-processing of the Query by REGEX starts here --------------***/ SolrParams originalParams = req.getOriginalParams(); SolrParams psuedoParams = req.getParams(); // These psuedoParams keep changing if (originalParams.get(CommonParams.Q) != null) { String finalQuery;//from w ww.j av a2 s . c om String originalQuery = originalParams.get(CommonParams.Q); rsp.add("Original query", originalQuery); /*** Arafath's code to prepare query starts here * The query should be in the following format -> * Example : * Original Query: "Which musical object did russ conway play" * Temporary Query : "relations:instrument AND entity:enty" // Generate the relation * Final Query : "name:"russ conway" AND occupation:musician" */ String tempQuery = "relations:instrumental AND entity:enty"; rsp.add("Temporary query", tempQuery); String desiredField = null; Set<String> fieldSet = null; SchemaField keyField = null; NamedList tempparamsList = req.getParams().toNamedList(); tempparamsList.setVal(tempparamsList.indexOf(CommonParams.Q, 0), tempQuery); psuedoParams = SolrParams.toSolrParams(tempparamsList); // if (psuedoParams.get(CommonParams.SORT) == null) { // tempparamsList.add(CommonParams.SORT, "score desc"); // } else { // tempparamsList.setVal(tempparamsList.indexOf(CommonParams.SORT, 0), "score desc"); // } SolrQueryRequest firstreq = new LocalSolrQueryRequest(req.getCore(), tempparamsList); SolrQueryResponse firstrsp = new SolrQueryResponse(); firstrsp.setAllValues(rsp.getValues()); ResponseBuilder firstrb = new ResponseBuilder(firstreq, firstrsp, components); for (SearchComponent c : components) { c.prepare(firstrb); c.process(firstrb); } rsp.add("response", firstrb.getResults().docList); /*** DocList docs = firstrb.getResults().docList; if (docs == null || docs.size() == 0) { log.debug("No results"); } else { fieldSet = new HashSet <String> (); keyField = firstrb.req.getCore().getLatestSchema().getUniqueKeyField(); if (keyField != null) { fieldSet.add(keyField.getName()); } fieldSet.add("fieldid"); fieldSet.add("relations"); fieldSet.add("entity"); fieldSet.add("count"); NamedList docresults = new NamedList(); DocIterator iterator = docs.iterator(); Document doc; int docScore = 0; rsp.add("doc retrieved ", docs.size()); for (int i=0; i<docs.size(); i++) { try { int docid = iterator.nextDoc(); doc = firstrb.req.getSearcher().doc(docid, fieldSet); if (Integer.parseInt(doc.get("count")) > docScore) { docScore = Integer.parseInt(doc.get("count")); desiredField = doc.get("fieldid"); } docresults.add(String.valueOf(docid), doc); } catch (IOException ex) { java.util.logging.Logger.getLogger(CustomQueryComponent.class.getName()).log(Level.SEVERE, null,ex); } } fieldSet.clear(); rsp.add("Intermediate results", docresults); if (desiredField != null) { rsp.add("Required Field", desiredField); } } ***/ firstreq.close(); /*** Final Phase starts here ***/ /*** finalQuery = "name:\"russ conway\" AND occupation:musician"; NamedList finalparamsList = req.getParams().toNamedList(); finalparamsList.setVal(finalparamsList.indexOf(CommonParams.Q, 0), finalQuery); psuedoParams = SolrParams.toSolrParams(finalparamsList); if (psuedoParams.get(CommonParams.SORT) == null) { finalparamsList.add(CommonParams.SORT, "score desc"); } else { finalparamsList.setVal(finalparamsList.indexOf(CommonParams.SORT, 0), "score desc"); } // if (desiredField != null) { // if (psuedoParams.get(CommonParams.FL) != null) { // finalparamsList.setVal(finalparamsList.indexOf(CommonParams.FL, 0), desiredField); // } else { // finalparamsList.add(CommonParams.FL, desiredField); // } // } SolrQueryRequest finalreq = new LocalSolrQueryRequest(req.getCore(), finalparamsList); rsp.add("Final Query", finalreq.getParams().get(CommonParams.Q)); ResponseBuilder rb = new ResponseBuilder(finalreq,rsp,components); for (SearchComponent c : components) { c.prepare(rb); c.process(rb); } ***/ /*** testing DocList finaldocs = rb.getResults().docList; if (finaldocs == null || finaldocs.size() == 0) { log.debug("No results"); } else { keyField = rb.req.getCore().getLatestSchema().getUniqueKeyField(); if (keyField != null) { fieldSet.add(keyField.getName()); } if (desiredField != null) { fieldSet.add(desiredField); } fieldSet.add("name"); NamedList finaldocresults = new NamedList(); DocIterator finaliterator = finaldocs.iterator(); Document finaldoc; rsp.add("finaldocs retrieved ", finaldocs.size()); for (int i=0; i<docs.size(); i++) { try { if (finaliterator.hasNext()) { int finaldocid = finaliterator.nextDoc(); finaldoc = rb.req.getSearcher().doc(finaldocid, fieldSet); finaldocresults.add(String.valueOf(finaldocid), finaldoc); } } catch (IOException ex) { java.util.logging.Logger.getLogger(MySearchHandler.class.getName()).log(Level.SEVERE, null,ex); } } rsp.add("final results", finaldocresults); } ***/ // finalreq.close(); } else { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Need to give at least one word as query!"); } }
From source file:io.logspace.hq.core.solr.event.SolrNativeQueryService.java
License:Open Source License
private InputStream serializeResponse(SolrParams params, QueryResponse response) throws IOException { LocalSolrQueryRequest solrQueryRequest = new LocalSolrQueryRequest(null, params); SolrQueryResponse solrQueryResponse = new SolrQueryResponse(); solrQueryResponse.setAllValues(response.getResponse()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(baos, "UTF-8"); this.jsonResponseWriter.write(writer, solrQueryRequest, solrQueryResponse); writer.flush();/*from ww w . j a va 2 s . co m*/ return new ByteArrayInputStream(baos.toByteArray()); }
From source file:opennlp.tools.similarity.apps.solr.ContentGeneratorRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { String query = req.getParams().get("q"); LOG.info(query);/*from ww w . ja v a 2 s .co m*/ String[] runCommand = new String[12], runInternal = new String[8]; runCommand[0] = "java"; runCommand[1] = "-Xmx1g"; runCommand[2] = "-jar"; runCommand[3] = "pt.jar"; runCommand[4] = "\"" + query + "\""; runCommand[5] = req.getParams().get("email"); runCommand[6] = req.getParams().get("resourceDir"); runCommand[7] = req.getParams().get("stepsNum"); runCommand[8] = req.getParams().get("searchResultsNum"); runCommand[9] = req.getParams().get("relevanceThreshold"); runCommand[10] = req.getParams().get("lang"); runCommand[11] = req.getParams().get("bingKey"); for (int i = 0; i < 8; i++) { runInternal[i] = runCommand[i + 4]; } String resultText = null; try { resultText = cgRunner(runInternal); } catch (Exception e1) { /* Runtime r = Runtime.getRuntime(); Process mStartProcess = null; String workDir = req.getParams().get("workDir"); if (workDir == null) System.err.println("workDir = null"); try { mStartProcess = r.exec(runCommand, null, new File(workDir)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } StreamLogger outputGobbler = new StreamLogger(mStartProcess.getInputStream()); outputGobbler.start(); } */ } NamedList<Object> values = rsp.getValues(); values.remove("response"); values.add("response", "We completed your request to write an essay on '" + query + "' and sent you an email at " + runCommand[5]); values.add("text", resultText); rsp.setAllValues(values); }
From source file:opennlp.tools.similarity.apps.solr.IterativeSearchRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { SolrQueryResponse rsp1 = new SolrQueryResponse(), rsp2 = new SolrQueryResponse(), rsp3 = new SolrQueryResponse(); NamedList list = rsp.getValues();//from w ww . j a v a2s . c o m rsp1.setAllValues(rsp.getValues().clone()); rsp2.setAllValues(rsp.getValues().clone()); rsp3.setAllValues(rsp.getValues().clone()); rsp1 = runSearchIteration(req, rsp1, "cat"); NamedList values = rsp1.getValues(); ResultContext c = (ResultContext) values.get("response"); if (c != null) { DocList dList = c.docs; if (dList.size() < 1) { rsp2 = runSearchIteration(req, rsp2, "name"); } else { rsp.setAllValues(rsp1.getValues()); return; } } values = rsp2.getValues(); c = (ResultContext) values.get("response"); if (c != null) { DocList dList = c.docs; if (dList.size() < 1) { rsp3 = runSearchIteration(req, rsp3, "content"); } else { rsp.setAllValues(rsp2.getValues()); return; } } rsp.setAllValues(rsp3.getValues()); }
From source file:opennlp.tools.similarity.apps.solr.NLProgram2CodeRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { // get query string String requestExpression = req.getParamString(); String[] exprParts = requestExpression.split("&"); String[] text = new String[exprParts.length]; int count = 0; for (String val : exprParts) { if (val.startsWith("line=")) { val = StringUtils.mid(val, 5, val.length()); text[count] = val; count++;/*from w w w . j a va2 s . c o m*/ } } StringBuffer buf = new StringBuffer(); for (String sent : text) { ObjectPhraseListForSentence opls = null; try { opls = compiler.convertSentenceToControlObjectPhrase(sent); } catch (Exception e) { e.printStackTrace(); } System.out.println(sent + "\n" + opls + "\n"); buf.append(sent + "\n |=> " + opls + "\n"); } LOG.info("re-ranking results: " + buf.toString()); NamedList<Object> values = rsp.getValues(); values.remove("response"); values.add("response", buf.toString().trim()); rsp.setAllValues(values); }
From source file:opennlp.tools.similarity.apps.solr.SearchResultsReRankerRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { // get query string String requestExpression = req.getParamString(); String[] exprParts = requestExpression.split("&"); for (String part : exprParts) { if (part.startsWith("q=")) requestExpression = part;/*w w w. j a va 2s.c o m*/ } String query = StringUtils.substringAfter(requestExpression, ":"); LOG.info(requestExpression); SolrParams ps = req.getOriginalParams(); Iterator<String> iter = ps.getParameterNamesIterator(); List<String> keys = new ArrayList<String>(); while (iter.hasNext()) { keys.add(iter.next()); } List<HitBase> searchResults = new ArrayList<HitBase>(); for (Integer i = 0; i < MAX_SEARCH_RESULTS; i++) { String title = req.getParams().get("t" + i.toString()); String descr = req.getParams().get("d" + i.toString()); if (title == null || descr == null) continue; HitBase hit = new HitBase(); hit.setTitle(title); hit.setAbstractText(descr); hit.setSource(i.toString()); searchResults.add(hit); } /* * http://173.255.254.250:8983/solr/collection1/reranker/? * q=search_keywords:design+iphone+cases&fields=spend+a+day+with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+fresh+design+with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+with+mobile+case+for+your+family&fields=Add+style+to+your+iPhone+and+iPad&fields=Add+Apple+fashion+to+your+iPhone+and+iPad * */ if (searchResults.size() < 1) { int count = 0; for (String val : exprParts) { if (val.startsWith("fields=")) { val = StringUtils.mid(val, 7, val.length()); HitBase hit = new HitBase(); hit.setTitle(""); hit.setAbstractText(val); hit.setSource(new Integer(count).toString()); searchResults.add(hit); count++; } } } List<HitBase> reRankedResults = null; query = query.replace('+', ' '); if (tooFewKeywords(query) || orQuery(query)) { reRankedResults = searchResults; LOG.info("No re-ranking for " + query); } else reRankedResults = calculateMatchScoreResortHits(searchResults, query); /* * <scores> <score index="2">3.0005</score> <score index="1">2.101</score> <score index="3">2.1003333333333334</score> <score index="4">2.00025</score> <score index="5">1.1002</score> </scores> * * */ StringBuffer buf = new StringBuffer(); buf.append("<scores>"); for (HitBase hit : reRankedResults) { buf.append("<score index=\"" + hit.getSource() + "\">" + hit.getGenerWithQueryScore() + "</score>"); } buf.append("</scores>"); NamedList<Object> scoreNum = new NamedList<Object>(); for (HitBase hit : reRankedResults) { scoreNum.add(hit.getSource(), hit.getGenerWithQueryScore()); } StringBuffer bufNums = new StringBuffer(); bufNums.append("order>"); for (HitBase hit : reRankedResults) { bufNums.append(hit.getSource() + "_"); } bufNums.append("/order>"); LOG.info("re-ranking results: " + buf.toString()); NamedList<Object> values = rsp.getValues(); values.remove("response"); values.add("response", scoreNum); //values.add("new_order", bufNums.toString().trim()); rsp.setAllValues(values); }
From source file:opennlp.tools.similarity.apps.solr.SearchResultsReRankerStanfRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { // get query string String requestExpression = req.getParamString(); String[] exprParts = requestExpression.split("&"); for (String part : exprParts) { if (part.startsWith("q=")) requestExpression = part;/*from w w w . j a v a 2 s. co m*/ } String query = StringUtils.substringAfter(requestExpression, ":"); LOG.info(requestExpression); SolrParams ps = req.getOriginalParams(); Iterator<String> iter = ps.getParameterNamesIterator(); List<String> keys = new ArrayList<String>(); while (iter.hasNext()) { keys.add(iter.next()); } List<HitBase> searchResults = new ArrayList<HitBase>(); for (Integer i = 0; i < MAX_SEARCH_RESULTS; i++) { String title = req.getParams().get("t" + i.toString()); String descr = req.getParams().get("d" + i.toString()); if (title == null || descr == null) continue; HitBase hit = new HitBase(); hit.setTitle(title); hit.setAbstractText(descr); hit.setSource(i.toString()); searchResults.add(hit); } /* * http://173.255.254.250:8983/solr/collection1/reranker/? * q=search_keywords:design+iphone+cases&fields=spend+a+day+with+a+ * custom+iPhone+case&fields=Add+style+to+your+every+day+fresh+design+ * with+a+custom+iPhone+case&fields=Add+style+to+your+every+day+with+ * mobile+case+for+your+family&fields=Add+style+to+your+iPhone+and+iPad& * fields=Add+Apple+fashion+to+your+iPhone+and+iPad * */ if (searchResults.size() < 1) { int count = 0; for (String val : exprParts) { if (val.startsWith("fields=")) { val = StringUtils.mid(val, 7, val.length()); HitBase hit = new HitBase(); hit.setTitle(""); hit.setAbstractText(val); hit.setSource(new Integer(count).toString()); searchResults.add(hit); count++; } } } List<HitBase> reRankedResults = null; query = query.replace('+', ' '); if (tooFewKeywords(query) || orQuery(query)) { reRankedResults = searchResults; LOG.info("No re-ranking for " + query); } else reRankedResults = calculateMatchScoreResortHits(searchResults, query); /* * <scores> <score index="2">3.0005</score> <score * index="1">2.101</score> <score index="3">2.1003333333333334</score> * <score index="4">2.00025</score> <score index="5">1.1002</score> * </scores> * * */ StringBuffer buf = new StringBuffer(); buf.append("<scores>"); for (HitBase hit : reRankedResults) { buf.append("<score index=\"" + hit.getSource() + "\">" + hit.getGenerWithQueryScore() + "</score>"); } buf.append("</scores>"); NamedList<Object> scoreNum = new NamedList<Object>(); for (HitBase hit : reRankedResults) { scoreNum.add(hit.getSource(), hit.getGenerWithQueryScore()); } StringBuffer bufNums = new StringBuffer(); bufNums.append("order>"); for (HitBase hit : reRankedResults) { bufNums.append(hit.getSource() + "_"); } bufNums.append("/order>"); LOG.info("re-ranking results: " + buf.toString()); NamedList<Object> values = rsp.getValues(); values.remove("response"); values.add("response", scoreNum); values.add("new_order", bufNums.toString().trim()); rsp.setAllValues(values); }
From source file:opennlp.tools.similarity.apps.solr.SyntGenRequestHandler.java
License:Apache License
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) { try {/*from w w w . j a v a2 s .c o m*/ super.handleRequestBody(req, rsp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } SolrParams reqValues = req.getOriginalParams(); Iterator<String> iter = reqValues.getParameterNamesIterator(); while (iter.hasNext()) { System.out.println(iter.next()); } String param = req.getParamString(); //modify rsp NamedList values = rsp.getValues(); ResultContext c = (ResultContext) values.get("response"); if (c == null) return; String val1 = (String) values.get("t1"); String k1 = values.getName(0); k1 = values.getName(1); k1 = values.getName(2); k1 = values.getName(3); k1 = values.getName(4); DocList dList = c.docs; DocList dListResult = null; try { dListResult = filterResultsBySyntMatchReduceDocSet(dList, req, req.getParams()); } catch (Exception e) { dListResult = dList; // TODO Auto-generated catch block e.printStackTrace(); } c.docs = dListResult; values.remove("response"); rsp.setAllValues(values); }
From source file:org.opencms.search.solr.CmsSolrIndex.java
License:Open Source License
/** * Performs the actual search.<p>//from www .ja v a2 s . c om * * @param cms the current OpenCms context * @param ignoreMaxRows <code>true</code> to return all all requested rows, <code>false</code> to use max rows * @param query the OpenCms Solr query * @param response the servlet response to write the query result to, may also be <code>null</code> * @param ignoreSearchExclude if set to false, only contents with search_exclude unset or "false" will be found - typical for the the non-gallery case * @param filter the resource filter to use * * @return the found documents * * @throws CmsSearchException if something goes wrong * * @see #search(CmsObject, CmsSolrQuery, boolean) */ @SuppressWarnings("unchecked") public CmsSolrResultList search(CmsObject cms, final CmsSolrQuery query, boolean ignoreMaxRows, ServletResponse response, boolean ignoreSearchExclude, CmsResourceFilter filter) throws CmsSearchException { // check if the user is allowed to access this index checkOfflineAccess(cms); if (!ignoreSearchExclude) { query.addFilterQuery(CmsSearchField.FIELD_SEARCH_EXCLUDE + ":\"false\""); } int previousPriority = Thread.currentThread().getPriority(); long startTime = System.currentTimeMillis(); // remember the initial query SolrQuery initQuery = query.clone(); query.setHighlight(false); LocalSolrQueryRequest solrQueryRequest = null; try { // initialize the search context CmsObject searchCms = OpenCms.initCmsObject(cms); // change thread priority in order to reduce search impact on overall system performance if (getPriority() > 0) { Thread.currentThread().setPriority(getPriority()); } // the lists storing the found documents that will be returned List<CmsSearchResource> resourceDocumentList = new ArrayList<CmsSearchResource>(); SolrDocumentList solrDocumentList = new SolrDocumentList(); // Initialize rows, offset, end and the current page. int rows = query.getRows() != null ? query.getRows().intValue() : CmsSolrQuery.DEFAULT_ROWS.intValue(); if (!ignoreMaxRows && (rows > ROWS_MAX)) { rows = ROWS_MAX; } int start = query.getStart() != null ? query.getStart().intValue() : 0; int end = start + rows; int page = 0; if (rows > 0) { page = Math.round(start / rows) + 1; } // set the start to '0' and expand the rows before performing the query query.setStart(new Integer(0)); query.setRows(new Integer((5 * rows * page) + start)); // perform the Solr query and remember the original Solr response QueryResponse queryResponse = m_solr.query(query); long solrTime = System.currentTimeMillis() - startTime; // initialize the counts long hitCount = queryResponse.getResults().getNumFound(); start = -1; end = -1; if ((rows > 0) && (page > 0) && (hitCount > 0)) { // calculate the final size of the search result start = rows * (page - 1); end = start + rows; // ensure that both i and n are inside the range of foundDocuments.size() start = new Long((start > hitCount) ? hitCount : start).intValue(); end = new Long((end > hitCount) ? hitCount : end).intValue(); } else { // return all found documents in the search result start = 0; end = new Long(hitCount).intValue(); } long visibleHitCount = hitCount; float maxScore = 0; // If we're using a postprocessor, (re-)initialize it before using it if (m_postProcessor != null) { m_postProcessor.init(); } // process found documents List<CmsSearchResource> allDocs = new ArrayList<CmsSearchResource>(); int cnt = 0; for (int i = 0; (i < queryResponse.getResults().size()) && (cnt < end); i++) { try { SolrDocument doc = queryResponse.getResults().get(i); CmsSolrDocument searchDoc = new CmsSolrDocument(doc); if (needsPermissionCheck(searchDoc)) { // only if the document is an OpenCms internal resource perform the permission check CmsResource resource = filter == null ? getResource(searchCms, searchDoc) : getResource(searchCms, searchDoc, filter); if (resource != null) { // permission check performed successfully: the user has read permissions! if (cnt >= start) { if (m_postProcessor != null) { doc = m_postProcessor.process(searchCms, resource, (SolrInputDocument) searchDoc.getDocument()); } resourceDocumentList.add(new CmsSearchResource(resource, searchDoc)); if (null != doc) { solrDocumentList.add(doc); } maxScore = maxScore < searchDoc.getScore() ? searchDoc.getScore() : maxScore; } allDocs.add(new CmsSearchResource(resource, searchDoc)); cnt++; } else { visibleHitCount--; } } else { // if permission check is not required for this index, // add a pseudo resource together with document to the results resourceDocumentList.add(new CmsSearchResource(PSEUDO_RES, searchDoc)); solrDocumentList.add(doc); maxScore = maxScore < searchDoc.getScore() ? searchDoc.getScore() : maxScore; cnt++; } } catch (Exception e) { // should not happen, but if it does we want to go on with the next result nevertheless LOG.warn(Messages.get().getBundle().key(Messages.LOG_SOLR_ERR_RESULT_ITERATION_FAILED_0), e); } } // the last documents were all secret so let's take the last found docs if (resourceDocumentList.isEmpty() && (allDocs.size() > 0)) { page = Math.round(allDocs.size() / rows) + 1; int showCount = allDocs.size() % rows; showCount = showCount == 0 ? rows : showCount; start = allDocs.size() - new Long(showCount).intValue(); end = allDocs.size(); if (allDocs.size() > start) { resourceDocumentList = allDocs.subList(start, end); for (CmsSearchResource r : resourceDocumentList) { maxScore = maxScore < r.getDocument().getScore() ? r.getDocument().getScore() : maxScore; solrDocumentList.add(((CmsSolrDocument) r.getDocument()).getSolrDocument()); } } } long processTime = System.currentTimeMillis() - startTime - solrTime; // create and return the result solrDocumentList.setStart(start); solrDocumentList.setMaxScore(new Float(maxScore)); solrDocumentList.setNumFound(visibleHitCount); queryResponse.getResponse().setVal(queryResponse.getResponse().indexOf(QUERY_RESPONSE_NAME, 0), solrDocumentList); queryResponse.getResponseHeader().setVal(queryResponse.getResponseHeader().indexOf(QUERY_TIME_NAME, 0), new Integer(new Long(System.currentTimeMillis() - startTime).intValue())); long highlightEndTime = System.currentTimeMillis(); SolrCore core = m_solr instanceof EmbeddedSolrServer ? ((EmbeddedSolrServer) m_solr).getCoreContainer().getCore(getCoreName()) : null; CmsSolrResultList result = null; try { SearchComponent highlightComponenet = null; if (core != null) { highlightComponenet = core.getSearchComponent("highlight"); solrQueryRequest = new LocalSolrQueryRequest(core, queryResponse.getResponseHeader()); } SolrQueryResponse solrQueryResponse = null; if (solrQueryRequest != null) { // create and initialize the solr response solrQueryResponse = new SolrQueryResponse(); solrQueryResponse.setAllValues(queryResponse.getResponse()); int paramsIndex = queryResponse.getResponseHeader().indexOf(HEADER_PARAMS_NAME, 0); NamedList<Object> header = null; Object o = queryResponse.getResponseHeader().getVal(paramsIndex); if (o instanceof NamedList) { header = (NamedList<Object>) o; header.setVal(header.indexOf(CommonParams.ROWS, 0), new Integer(rows)); header.setVal(header.indexOf(CommonParams.START, 0), new Long(start)); } // set the OpenCms Solr query as parameters to the request solrQueryRequest.setParams(initQuery); // perform the highlighting if ((header != null) && (initQuery.getHighlight()) && (highlightComponenet != null)) { header.add(HighlightParams.HIGHLIGHT, "on"); if ((initQuery.getHighlightFields() != null) && (initQuery.getHighlightFields().length > 0)) { header.add(HighlightParams.FIELDS, CmsStringUtil.arrayAsString(initQuery.getHighlightFields(), ",")); } String formatter = initQuery.getParams(HighlightParams.FORMATTER) != null ? initQuery.getParams(HighlightParams.FORMATTER)[0] : null; if (formatter != null) { header.add(HighlightParams.FORMATTER, formatter); } if (initQuery.getHighlightFragsize() != 100) { header.add(HighlightParams.FRAGSIZE, new Integer(initQuery.getHighlightFragsize())); } if (initQuery.getHighlightRequireFieldMatch()) { header.add(HighlightParams.FIELD_MATCH, new Boolean(initQuery.getHighlightRequireFieldMatch())); } if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(initQuery.getHighlightSimplePost())) { header.add(HighlightParams.SIMPLE_POST, initQuery.getHighlightSimplePost()); } if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(initQuery.getHighlightSimplePre())) { header.add(HighlightParams.SIMPLE_PRE, initQuery.getHighlightSimplePre()); } if (initQuery.getHighlightSnippets() != 1) { header.add(HighlightParams.SNIPPETS, new Integer(initQuery.getHighlightSnippets())); } ResponseBuilder rb = new ResponseBuilder(solrQueryRequest, solrQueryResponse, Collections.singletonList(highlightComponenet)); try { rb.doHighlights = true; DocListAndSet res = new DocListAndSet(); SchemaField idField = OpenCms.getSearchManager().getSolrServerConfiguration() .getSolrSchema().getUniqueKeyField(); int[] luceneIds = new int[rows]; int docs = 0; for (SolrDocument doc : solrDocumentList) { String idString = (String) doc.getFirstValue(CmsSearchField.FIELD_ID); int id = solrQueryRequest.getSearcher().getFirstMatch( new Term(idField.getName(), idField.getType().toInternal(idString))); luceneIds[docs++] = id; } res.docList = new DocSlice(0, docs, luceneIds, null, docs, 0); rb.setResults(res); rb.setQuery(QParser.getParser(initQuery.getQuery(), null, solrQueryRequest).getQuery()); rb.setQueryString(initQuery.getQuery()); highlightComponenet.prepare(rb); highlightComponenet.process(rb); highlightComponenet.finishStage(rb); } catch (Exception e) { LOG.error(e.getMessage() + " in query: " + initQuery, new Exception(e)); } // Make highlighting also available via the CmsSolrResultList queryResponse.setResponse(solrQueryResponse.getValues()); highlightEndTime = System.currentTimeMillis(); } } result = new CmsSolrResultList(initQuery, queryResponse, solrDocumentList, resourceDocumentList, start, new Integer(rows), end, page, visibleHitCount, new Float(maxScore), startTime, highlightEndTime); if (LOG.isDebugEnabled()) { Object[] logParams = new Object[] { new Long(System.currentTimeMillis() - startTime), new Long(result.getNumFound()), new Long(solrTime), new Long(processTime), new Long(result.getHighlightEndTime() != 0 ? result.getHighlightEndTime() - startTime : 0) }; LOG.debug(query.toString() + "\n" + Messages.get().getBundle().key(Messages.LOG_SOLR_SEARCH_EXECUTED_5, logParams)); } if (response != null) { writeResp(response, solrQueryRequest, solrQueryResponse); } } finally { if (solrQueryRequest != null) { solrQueryRequest.close(); } if (core != null) { core.close(); } } return result; } catch (Exception e) { throw new CmsSearchException(Messages.get().container(Messages.LOG_SOLR_ERR_SEARCH_EXECUTION_FAILD_1, CmsEncoder.decode(query.toString()), e), e); } finally { if (solrQueryRequest != null) { solrQueryRequest.close(); } // re-set thread to previous priority Thread.currentThread().setPriority(previousPriority); } }