List of usage examples for org.apache.solr.servlet SolrRequestParsers parseQueryString
public static MultiMapSolrParams parseQueryString(String queryString)
From source file:com.doculibre.constellio.servlets.SolrJExampleMain.java
License:Open Source License
/** * Do the query using a StringBuffer// www. j a va 2s .co m */ public static QueryResponse doFirstQuery(SolrServer server) throws SolrServerException { StringBuffer request = new StringBuffer(); request.append("collectionName=" + myCollection); request.append("&facet=" + facet); request.append("&q=" + query); request.append("&start=" + start); request.append("&rows=" + nbDocuments); SolrParams solrParams = SolrRequestParsers.parseQueryString(request.toString()); return server.query(solrParams); }
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 ww w. ja v a 2s. c om*/ 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.sitewhere.solr.search.SolrSearchProvider.java
License:Open Source License
@Override public List<IDeviceEvent> executeQuery(String query) throws SiteWhereException { try {/*from w w w . j a v a 2s. c o m*/ LOGGER.info("About to execute Solr search with query string: " + query); List<IDeviceEvent> results = new ArrayList<IDeviceEvent>(); MultiMapSolrParams params = SolrRequestParsers.parseQueryString(query); QueryResponse response = getSolr().getSolrServer().query(params); SolrDocumentList docs = response.getResults(); for (SolrDocument doc : docs) { results.add(SiteWhereSolrFactory.parseDocument(doc)); } return results; } catch (SolrServerException e) { throw new SiteWhereException("Unable to execute query.", e); } }
From source file:edu.ku.brc.sgr.BatchMatchResultsInFile.java
License:Open Source License
public BatchMatchResultsInFile(String fileName, SGRMatcher matcher) throws IOException { this.matcher = matcher; final File file = new File(fileName); BufferedReader in = null;/*from w ww .ja va 2 s.c o m*/ try { in = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { } resuming = (in != null); completedIds = Sets.newHashSet(); if (resuming) { SolrParams oldSolrParams = SolrRequestParsers.parseQueryString(in.readLine()); if (!EquateSolrParams.equals(matcher.getBaseQuery(), oldSolrParams)) { in.close(); throw new IllegalArgumentException("cannot resume batchmatch with inconsistent query"); } while (true) { final String line = in.readLine(); if (line == null) break; final String[] fields = line.split("\t"); completedIds.add(fields[0]); } in.close(); } output = new PrintWriter(new FileWriter(file, true), true); if (!resuming) { output.println(matcher.getBaseQuery().toString()); } }
From source file:edu.ku.brc.sgr.SGRMatcher.java
License:Open Source License
public boolean sameQueryAs(String query) { SolrParams params = SolrRequestParsers.parseQueryString(query); return EquateSolrParams.equals(getBaseQuery(), params); }
From source file:net.yacy.http.servlets.SolrSelectServlet.java
License:Open Source License
@Override public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest) request; HttpServletResponse hresponse = (HttpServletResponse) response; SolrQueryRequest req = null;//from ww w . jav a 2s. com final Method reqMethod = Method.getMethod(hrequest.getMethod()); Writer out = null; try { // prepare request to solr MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString()); Switchboard sb = Switchboard.getSwitchboard(); boolean authenticated = true; // count remote searches if this was part of a p2p search if (mmsp.getMap().containsKey("partitions")) { final int partitions = mmsp.getInt("partitions", 30); sb.searchQueriesGlobal += 1.0f / partitions; // increase query counter } // get the ranking profile id int profileNr = mmsp.getInt("profileNr", 0); // rename post fields according to result style String querystring = ""; if (!mmsp.getMap().containsKey(CommonParams.Q) && mmsp.getMap().containsKey(CommonParams.QUERY)) { querystring = mmsp.get(CommonParams.QUERY, ""); mmsp.getMap().remove(CommonParams.QUERY); QueryModifier modifier = new QueryModifier(0); querystring = modifier.parse(querystring); modifier.apply(mmsp); QueryGoal qg = new QueryGoal(querystring); StringBuilder solrQ = qg.collectionTextQuery(); mmsp.getMap().put(CommonParams.Q, new String[] { solrQ.toString() }); // sru patch } String q = mmsp.get(CommonParams.Q, ""); if (querystring.length() == 0) querystring = q; if (!mmsp.getMap().containsKey(CommonParams.START)) { int startRecord = mmsp.getFieldInt("startRecord", null, 0); mmsp.getMap().remove("startRecord"); mmsp.getMap().put(CommonParams.START, new String[] { Integer.toString(startRecord) }); // sru patch } if (!mmsp.getMap().containsKey(CommonParams.ROWS)) { int maximumRecords = mmsp.getFieldInt("maximumRecords", null, 10); mmsp.getMap().remove("maximumRecords"); mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer.toString(maximumRecords) }); // sru patch } mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer .toString(Math.min(mmsp.getInt(CommonParams.ROWS, 10), (authenticated) ? 100000000 : 100)) }); // set ranking according to profile number if ranking attributes are not given in the request Ranking ranking = sb.index.fulltext().getDefaultConfiguration().getRanking(profileNr); if (!mmsp.getMap().containsKey(CommonParams.SORT) && !mmsp.getMap().containsKey(DisMaxParams.BQ) && !mmsp.getMap().containsKey(DisMaxParams.BF) && !mmsp.getMap().containsKey("boost")) { if (!mmsp.getMap().containsKey("defType")) mmsp.getMap().put("defType", new String[] { "edismax" }); String fq = ranking.getFilterQuery(); String bq = ranking.getBoostQuery(); String bf = ranking.getBoostFunction(); if (fq.length() > 0) mmsp.getMap().put(CommonParams.FQ, new String[] { fq }); if (bq.length() > 0) mmsp.getMap().put(DisMaxParams.BQ, new String[] { bq }); if (bf.length() > 0) mmsp.getMap().put("boost", new String[] { bf }); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29 } // get a response writer for the result String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt); if (responseWriter == null) throw new ServletException("no response writer"); if (responseWriter instanceof OpensearchResponseWriter) { // set the title every time, it is possible that it has changed final String promoteSearchPageGreeting = (sb .getConfigBool(SwitchboardConstants.GREETING_NETWORK_NAME, false)) ? sb.getConfig("network.unit.description", "") : sb.getConfig(SwitchboardConstants.GREETING, ""); ((OpensearchResponseWriter) responseWriter).setTitle(promoteSearchPageGreeting); } // if this is a call to YaCys special search formats, enhance the query with field assignments if ((responseWriter instanceof YJsonResponseWriter || responseWriter instanceof OpensearchResponseWriter) && "true".equals(mmsp.get("hl", "true"))) { // add options for snippet generation if (!mmsp.getMap().containsKey("hl.q")) mmsp.getMap().put("hl.q", new String[] { q }); if (!mmsp.getMap().containsKey("hl.fl")) mmsp.getMap().put("hl.fl", new String[] { CollectionSchema.description_txt + "," + CollectionSchema.h4_txt.getSolrFieldName() + "," + CollectionSchema.h3_txt.getSolrFieldName() + "," + CollectionSchema.h2_txt.getSolrFieldName() + "," + CollectionSchema.h1_txt.getSolrFieldName() + "," + CollectionSchema.text_t.getSolrFieldName() }); if (!mmsp.getMap().containsKey("hl.alternateField")) mmsp.getMap().put("hl.alternateField", new String[] { CollectionSchema.description_txt.getSolrFieldName() }); if (!mmsp.getMap().containsKey("hl.simple.pre")) mmsp.getMap().put("hl.simple.pre", new String[] { "<b>" }); if (!mmsp.getMap().containsKey("hl.simple.post")) mmsp.getMap().put("hl.simple.post", new String[] { "</b>" }); if (!mmsp.getMap().containsKey("hl.fragsize")) mmsp.getMap().put("hl.fragsize", new String[] { Integer.toString(SearchEvent.SNIPPET_MAX_LENGTH) }); } // get the embedded connector String requestURI = hrequest.getRequestURI(); boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME) || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME); mmsp.getMap().remove("core"); SolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector() : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME); if (connector == null) { connector = defaultConnector ? sb.index.fulltext().getDefaultConnector() : sb.index.fulltext().getConnectorForRead(WebgraphSchema.CORE_NAME); } if (connector == null) throw new ServletException("no core"); // add default queryfield parameter according to local ranking config (or defaultfield) if (ranking != null) { // ranking normally never null final String qf = ranking.getQueryFields(); if (qf.length() > 4) { // make sure qf has content (else use df) addParam(DisMaxParams.QF, qf, mmsp.getMap()); // add QF that we set to be best suited for our index // TODO: if every peer applies a decent QF itself, this can be reverted to getMap().put() } else { mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() }); } } else { mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() }); } // do the solr request, generate facets if we use a special YaCy format final SolrQueryResponse rsp; if (connector instanceof EmbeddedSolrConnector) { req = ((EmbeddedSolrConnector) connector).request(mmsp); rsp = ((EmbeddedSolrConnector) connector).query(req); // prepare response hresponse.setHeader("Cache-Control", "no-cache, no-store"); HttpCacheHeaderUtil.checkHttpCachingVeto(rsp, hresponse, reqMethod); // check error if (rsp.getException() != null) { AccessTracker.addToDump(querystring, "0", new Date()); sendError(hresponse, rsp.getException()); return; } NamedList<?> values = rsp.getValues(); DocList r = ((ResultContext) values.get("response")).docs; int numFound = r.matches(); AccessTracker.addToDump(querystring, Integer.toString(numFound), new Date()); // write response header final String contentType = responseWriter.getContentType(req, rsp); if (null != contentType) response.setContentType(contentType); if (Method.HEAD == reqMethod) { return; } // write response body if (responseWriter instanceof BinaryResponseWriter) { ((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp); } else { out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset)); responseWriter.write(out, req, rsp); out.flush(); } } else { // write a 'faked' response using a call to the backend SolrDocumentList sdl = connector.getDocumentListByQuery(mmsp.getMap().get(CommonParams.Q)[0], mmsp.getMap().get(CommonParams.SORT) == null ? null : mmsp.getMap().get(CommonParams.SORT)[0], Integer.parseInt(mmsp.getMap().get(CommonParams.START)[0]), Integer.parseInt(mmsp.getMap().get(CommonParams.ROWS)[0]), mmsp.getMap().get(CommonParams.FL)); OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream()); EnhancedXMLResponseWriter.write(osw, req, sdl); osw.close(); } } catch (final Throwable ex) { sendError(hresponse, ex); } finally { if (req != null) { req.close(); } SolrRequestInfo.clearRequestInfo(); if (out != null) try { out.close(); } catch (final IOException e1) { } } }
From source file:net.yacy.http.servlets.SolrServlet.java
License:Open Source License
@Override public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest) request; final Method reqMethod = Method.getMethod(hrequest.getMethod()); // get the embedded connector String requestURI = hrequest.getRequestURI(); MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString()); boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME) || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME); mmsp.getMap().remove("core"); Switchboard sb = Switchboard.getSwitchboard(); EmbeddedSolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector() : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME); if (connector == null) throw new ServletException("no core"); SolrQueryResponse solrRsp = new SolrQueryResponse(); SolrQueryRequest solrReq = null;//from w w w .j av a 2s. c o m try { solrReq = connector.request(mmsp); // SolrRequestParsers.DEFAULT.parse(null, hrequest.getServletPath(), hrequest); solrReq.getContext().put("webapp", hrequest.getContextPath()); SolrRequestHandler handler = sb.index.fulltext().getEmbeddedInstance().getCoreContainer() .getMultiCoreHandler(); connector.getCore().execute(handler, solrReq, solrRsp); // write response header QueryResponseWriter responseWriter = connector.getCore().getQueryResponseWriter(solrReq); final String ct = responseWriter.getContentType(solrReq, solrRsp); if (null != ct) response.setContentType(ct); if (Method.HEAD != reqMethod) { if (responseWriter instanceof BinaryQueryResponseWriter) { BinaryQueryResponseWriter binWriter = (BinaryQueryResponseWriter) responseWriter; binWriter.write(response.getOutputStream(), solrReq, solrRsp); } else { String charset = ContentStreamBase.getCharsetFromContentType(ct); Writer out = (charset == null || charset.equalsIgnoreCase("UTF-8")) ? new OutputStreamWriter(response.getOutputStream(), UTF8) : new OutputStreamWriter(response.getOutputStream(), charset); out = new FastWriter(out); responseWriter.write(out, solrReq, solrRsp); out.flush(); } } ConcurrentLog.info("luke", solrRsp.getValues().toString()); } catch (Exception e) { ConcurrentLog.logException(e); } finally { if (solrReq != null) solrReq.close(); } }
From source file:org.apache.apex.malhar.contrib.solr.SolrInputOperator.java
License:Apache License
@Override public SolrParams getQueryParams() { SolrParams solrParams;/* w w w . j a v a2 s . c o m*/ if (solrQuery != null) { solrParams = SolrRequestParsers.parseQueryString(solrQuery); } else { logger.debug("Solr document fetch query is not set, using wild card query for search."); solrParams = SolrRequestParsers.parseQueryString("*"); } return solrParams; }
From source file:org.opencastproject.search.impl.solr.SolrIndexManager.java
License:Educational Community License
/** * Creates a solr input document for the series metadata of the media package. * //from w w w . ja v a 2 s . co m * @param seriesId * the id of the series * @param acl * the access control list for this mediapackage * @return an input document ready to be posted to solr or null */ private SolrInputDocument createSeriesInputDocument(String seriesId, AccessControlList acl) throws IOException, UnauthorizedException { if (seriesId == null) return null; DublinCoreCatalog dc = null; try { dc = seriesService.getSeries(seriesId); } catch (SeriesException e) { logger.debug("No series dublincore found for series id " + seriesId); return null; } catch (NotFoundException e) { logger.debug("No series dublincore found for series id " + seriesId); return null; } SolrInputDocument doc = new SolrInputDocument(); // Populate document with existing data try { StringBuffer query = new StringBuffer("q="); query = query.append(Schema.ID).append(":").append(SolrUtils.clean(seriesId)); SolrParams params = SolrRequestParsers.parseQueryString(query.toString()); QueryResponse solrResponse = solrServer.query(params); if (solrResponse.getResults().size() > 0) { SolrDocument existingSolrDocument = solrResponse.getResults().get(0); for (String fieldName : existingSolrDocument.getFieldNames()) { doc.addField(fieldName, existingSolrDocument.getFieldValue(fieldName)); } } } catch (Exception e) { logger.error("Error trying to load series " + seriesId, e); } // Fill document Schema.setId(doc, seriesId); // OC specific fields Schema.setOrganization(doc, securityService.getOrganization().getId()); Schema.setOcMediatype(doc, SearchResultItemType.Series.toString()); Schema.setOcModified(doc, new Date()); // DC fields addSeriesMetadata(doc, dc); // Authorization setAuthorization(doc, securityService, acl); return doc; }