List of usage examples for org.apache.solr.common.params ModifiableSolrParams get
@Override
public String get(String param)
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 w ww. j a v a 2 s .co 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.gisgraphy.domain.geoloc.service.fulltextsearch.FullTextSearchEngine.java
License:Open Source License
public void executeAndSerialize(FulltextQuery query, OutputStream outputStream) throws FullTextSearchException { statsUsageService.increaseUsage(StatsUsageType.FULLTEXT); Assert.notNull(query, "Can not execute a null query"); Assert.notNull(outputStream, "Can not serialize into a null outputStream"); try {/* w w w .ja v a2 s . c om*/ logger.info(query.toString()); ModifiableSolrParams params = query.parameterize(); CommonsHttpSolrServer server = new CommonsHttpSolrServer(solrClient.getURL(), this.httpClient, new OutputstreamResponseWrapper(outputStream, params.get(Constants.OUTPUT_FORMAT_PARAMETER))); server.query(params); } catch (SolrServerException e) { logger.error("Can not execute query " + query.toQueryString() + "for URL : " + solrClient.getURL() + " : " + e.getCause().getMessage()); throw new FullTextSearchException(e.getCause().getMessage()); } catch (MalformedURLException e1) { logger.error("The URL " + solrClient.getURL() + " is incorrect"); throw new FullTextSearchException(e1); } catch (RuntimeException e2) { logger.error("An error has occurred during fulltext search of query " + query + " : " + e2.getCause().getMessage()); throw new FullTextSearchException(e2.getCause().getMessage()); } }
From source file:com.gisgraphy.fulltext.FullTextSearchEngine.java
License:Open Source License
public void executeAndSerialize(FulltextQuery query, OutputStream outputStream) throws FullTextSearchException { statsUsageService.increaseUsage(StatsUsageType.FULLTEXT); Assert.notNull(query, "Can not execute a null query"); Assert.notNull(outputStream, "Can not serialize into a null outputStream"); String queryString = ZipcodeNormalizer.normalize(query.getQuery(), query.getCountryCode()); query.withQuery(queryString);// w w w . j ava 2 s .com try { if (!disableLogging) { logger.info(query.toString()); } ModifiableSolrParams params = FulltextQuerySolrHelper.parameterize(query); CommonsHttpSolrServer server = new CommonsHttpSolrServer(solrClient.getURL(), this.httpClient, new OutputstreamResponseWrapper(outputStream, params.get(Constants.OUTPUT_FORMAT_PARAMETER))); server.query(params); } catch (SolrServerException e) { logger.error("Can not execute query " + FulltextQuerySolrHelper.toQueryString(query) + "for URL : " + solrClient.getURL() + " : " + e.getCause().getMessage(), e); throw new FullTextSearchException(e.getCause().getMessage()); } catch (MalformedURLException e1) { logger.error("The URL " + solrClient.getURL() + " is incorrect", e1); throw new FullTextSearchException(e1); } catch (RuntimeException e2) { String message = e2.getCause() != null ? e2.getCause().getMessage() : e2.getMessage(); logger.error("An error has occurred during fulltext search of query " + query + " : " + message, e2); throw new FullTextSearchException(message, e2); } }
From source file:com.s24.search.solr.util.SolrParamsUtilTest.java
License:Apache License
@Test public void testModifiable() throws Exception { Map<String, String> map = new HashMap<>(); map.put("foo", "bar"); SolrParams params = new MapSolrParams(map); ModifiableSolrParams modifiable = SolrParamsUtil.modifiable(params); assertEquals(1, modifiable.size());/*from www . ja va 2 s . co m*/ assertEquals("bar", modifiable.get("foo")); }
From source file:com.s24.search.solr.util.SolrParamsUtilTest.java
License:Apache License
@Test public void testModifiable_noCopyIfAlreadyModifiable() throws Exception { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("foo", "bar"); ModifiableSolrParams modifiable = SolrParamsUtil.modifiable(params); assertSame("Should not create unnecessary copy if input is already ModifiableSolrParams", params, modifiable);//from w w w . j av a2s.c om assertEquals(1, modifiable.size()); assertEquals("bar", modifiable.get("foo")); }
From source file:de.qaware.chronix.solr.query.ChronixQueryHandler.java
License:Apache License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams(req.getParams()); String originQuery = modifiableSolrParams.get(CommonParams.Q); long[] startAndEnd = dateRangeParser.getNumericQueryTerms(originQuery); long queryStart = or(startAndEnd[0], -1, 0); long queryEnd = or(startAndEnd[1], -1, Long.MAX_VALUE); modifiableSolrParams.set(ChronixQueryParams.QUERY_START_LONG, String.valueOf(queryStart)); modifiableSolrParams.set(ChronixQueryParams.QUERY_END_LONG, String.valueOf(queryEnd)); String query = dateRangeParser.replaceRangeQueryTerms(originQuery); modifiableSolrParams.set(CommonParams.Q, query); //Set the min required fields if the user define a sub set of fields modifiableSolrParams.set(CommonParams.FL, minRequiredFields(modifiableSolrParams.get(CommonParams.FL))); //Set the updated query req.setParams(modifiableSolrParams); //check the filter queries String[] filterQueries = modifiableSolrParams.getParams(CommonParams.FQ); //if we have an isAggregation if (contains(filterQueries, ChronixQueryParams.AGGREGATION_PARAM) || contains(filterQueries, ChronixQueryParams.ANALYSIS_PARAM)) { analysisHandler.handleRequestBody(req, rsp); } else {/*from w w w. j a v a2 s .c om*/ //let the default search handler do its work searchHandler.handleRequestBody(req, rsp); } //add the converted start and end to the response rsp.getResponseHeader().add(ChronixQueryParams.QUERY_START_LONG, queryStart); rsp.getResponseHeader().add(ChronixQueryParams.QUERY_END_LONG, queryEnd); }
From source file:de.uni_tuebingen.ub.ixTheo.handler.component.FacetPrefixSortComponent.java
License:Apache License
/** * Actually run the query//from w w w . j ava 2 s . c o m */ @Override public void process(ResponseBuilder rb) throws IOException { if (rb.doFacets) { final ModifiableSolrParams params = new ModifiableSolrParams(); final SolrParams origParams = rb.req.getParams(); final Iterator<String> iter = origParams.getParameterNamesIterator(); setCollator(origParams.get("lang")); while (iter.hasNext()) { final String paramName = iter.next(); // Deduplicate the list with LinkedHashSet, but _only_ for facet // params. if (!paramName.startsWith(FacetParams.FACET)) { params.add(paramName, origParams.getParams(paramName)); continue; } final HashSet<String> deDupe = new LinkedHashSet<>(Arrays.asList(origParams.getParams(paramName))); params.add(paramName, deDupe.toArray(new String[deDupe.size()])); } final SimplePrefixSortFacets facets = new SimplePrefixSortFacets(rb.req, rb.getResults().docSet, params, rb); final NamedList<Object> counts = org.apache.solr.handler.component.FacetComponent .getFacetCounts(facets); final String[] pivots = params.getParams(FacetParams.FACET_PIVOT); if (pivots != null && pivots.length > 0) { PivotFacetProcessor pivotProcessor = new PivotFacetProcessor(rb.req, rb.getResults().docSet, params, rb); SimpleOrderedMap<List<NamedList<Object>>> v = pivotProcessor.process(pivots); if (v != null) { counts.add(PIVOT_KEY, v); } } // Check whether we have to reorder out results // according to prefix final String sort = params.get(FacetParams.FACET_SORT); if (FacetPrefixSortParams.FACET_SORT_PREFIX.equals(sort)) { // Determine a score relative to the original query // Determine the query and make it compatible with our metric // class // by splitting the single terms String[] queryTerms = params.getParams(CommonParams.Q); final Collection<String> queryTermsCollection = new ArrayList<>(); for (String s : queryTerms) { // Split at whitespace except we have a quoted term Matcher matcher = WHITE_SPACES_WITH_QUOTES_SPLITTING_PATTERN.matcher(s); while (matcher.find()) { queryTermsCollection.add(matcher.group().replaceAll("^\"|\"$", "")); } } // In some contexts, i.e. in KWC that are derived from ordinary // keywords or if // wildcards occur, also add all the query terms as a single // phrase term // with stripped wildcards StringBuilder sb = new StringBuilder(); for (String s : queryTermsCollection) { s = s.replace("*", ""); sb.append(s); sb.append(" "); } queryTermsCollection.add(sb.toString().trim()); final ArrayList<String> queryList = new ArrayList<>(queryTermsCollection); final String facetfield = params.get(FacetParams.FACET_FIELD); // Get the current facet entry and make it compatible with our // metric class // "facet_fields" itself contains a NamedList with the // facet.field as key final NamedList<Object> facetFieldsNamedList = (NamedList<Object>) counts.get("facet_fields"); final NamedList<Object> facetFields = (NamedList<Object>) facetFieldsNamedList.get(facetfield); final List<Entry<Entry<String, Object>, Double>> facetPrefixListScored = new ArrayList<>(); for (final Entry<String, Object> entry : facetFields) { final String facetTerms = entry.getKey(); // Split up each KWC and calculate the scoring ArrayList<String> facetList = new ArrayList<>( Arrays.asList(facetTerms.split("(?<!" + Pattern.quote("\\") + ")/"))); // For usability reasons sort the result facets according to // the order of the search facetList = KeywordSort.sortToReferenceChain(queryList, facetList); final double score = KeywordChainMetric.calculateSimilarityScore(queryList, facetList); // Collect the result in a sorted list and throw away // garbage if (score > 0) { String facetTermsSorted = StringUtils.join(facetList, "/"); Map.Entry<String, Object> sortedEntry = new AbstractMap.SimpleEntry<>(facetTermsSorted, entry.getValue()); facetPrefixListScored.add(new AbstractMap.SimpleEntry<>(sortedEntry, score)); } } Collections.sort(facetPrefixListScored, ENTRY_COMPARATOR); // Extract all the values wrap it back to NamedList again and // replace in the original structure facetFieldsNamedList.clear(); NamedList<Object> facetNamedListSorted = new NamedList<>(); // We had to disable all limits and offsets sort according // Handle this accordingly now int offset = (params.getInt(FacetParams.FACET_OFFSET) != null) ? params.getInt(FacetParams.FACET_OFFSET) : 0; int limit = (params.getInt(FacetParams.FACET_LIMIT) != null) ? params.getInt(FacetParams.FACET_LIMIT) : 100; // Strip uneeded elements int s = facetPrefixListScored.size(); int off = (offset < s) ? offset : 0; limit = (limit < 0) ? s : limit; // Handle a negative limit // param, i.e. unlimited results int lim = (offset + limit <= s) ? (offset + limit) : s; final List<Entry<Entry<String, Object>, Double>> facetPrefixListScoredTruncated = facetPrefixListScored .subList(off, lim); for (Entry<Entry<String, Object>, Double> e : facetPrefixListScoredTruncated) { facetNamedListSorted.add(e.getKey().getKey(), e.getKey().getValue()); } facetFieldsNamedList.add(facetfield, facetNamedListSorted); NamedList<Object> countList = new NamedList<>(); countList.add("count", facetPrefixListScored.size()); facetFieldsNamedList.add(facetfield + "-count", countList); counts.remove("facet_fields"); counts.add("facet_fields", facetFieldsNamedList); } rb.rsp.add("facet_counts", counts); } }
From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java
License:Open Source License
/** * the usage of getResponseByParams is disencouraged for the embedded Solr connector. Please use request(SolrParams) instead. * Reason: Solr makes a very complex folding/unfolding including data compression for SolrQueryResponses. *///from ww w. jav a 2 s .co m @Override public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException { if (this.server == null) throw new IOException("server disconnected"); // during the solr query we set the thread name to the query string to get more debugging info in thread dumps String q = params.get(CommonParams.Q); String fl = params.get(CommonParams.FL); String[] fq = params.getParams(CommonParams.FQ); String threadname = Thread.currentThread().getName(); if (q != null) { StringBuilder fqa = new StringBuilder(); if (fq != null) for (String f : fq) fqa.append("fq=").append(f).append(' '); Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", " + fqa.toString()) + (fl == null ? "" : ", fl=" + fl)); //System.out.println("solr query: q = " + q + (fq == null ? "" : ", " + fqa.toString()) + (fl == null ? "" : ", fl=" + fl)); } QueryResponse rsp; try { rsp = this.server.query(params); if (q != null) Thread.currentThread().setName(threadname); if (rsp != null) if (log.isFine()) log.fine(rsp.getResults().getNumFound() + " results for q=" + q); return rsp; } catch (final SolrServerException e) { throw new IOException(e); } catch (final Throwable e) { throw new IOException("Error executing query", e); } }
From source file:net.yacy.cora.federate.solr.connector.EmbeddedSolrConnector.java
License:Open Source License
/** * get the solr document list from a query response * This differs from getResponseByParams in such a way that it does only create the fields of the response but * never search snippets and there are also no facets generated. * @param params/*from w ww .j a v a 2 s.co m*/ * @return * @throws IOException * @throws SolrException */ @Override public SolrDocumentList getDocumentListByParams(ModifiableSolrParams params) throws IOException, SolrException { SolrQueryRequest req = this.request(params); SolrQueryResponse response = null; String q = params.get(CommonParams.Q); String fq = params.get(CommonParams.FQ); String sort = params.get(CommonParams.SORT); String threadname = Thread.currentThread().getName(); try { if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq) + (sort == null ? "" : ", sort = " + sort)); // for debugging in Threaddump response = this.query(req); if (q != null) Thread.currentThread().setName(threadname); if (response == null) throw new IOException("response == null"); return SolrQueryResponse2SolrDocumentList(req, response); } finally { req.close(); SolrRequestInfo.clearRequestInfo(); } }
From source file:net.yacy.cora.federate.solr.connector.RemoteSolrConnector.java
License:Open Source License
@Override public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException { // during the solr query we set the thread name to the query string to get more debugging info in thread dumps String q = params.get(CommonParams.Q); String fq = params.get(CommonParams.FQ); String threadname = Thread.currentThread().getName(); if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq)); QueryRequest request = new QueryRequest(params); ResponseParser responseParser = useBinaryResponseWriter ? new BinaryResponseParser() : new XMLResponseParser(); request.setResponseParser(responseParser); long t = System.currentTimeMillis(); NamedList<Object> result = null; try {/* w ww. j a v a2 s. com*/ result = this.server.request(request); } catch (final Throwable e) { //ConcurrentLog.logException(e); throw new IOException(e.getMessage()); /* Log.logException(e); server = instance.getServer(this.corename); super.init(server); try { result = server.request(request); } catch (final Throwable e1) { throw new IOException(e1.getMessage()); } */ } QueryResponse response = new QueryResponse(result, this.server); response.setElapsedTime(System.currentTimeMillis() - t); if (q != null) Thread.currentThread().setName(threadname); return response; }