List of usage examples for org.apache.solr.common.params SolrParams getParameterNamesIterator
public abstract Iterator<String> getParameterNamesIterator();
From source file:alba.solr.core.DynamicQueryParser.java
License:Apache License
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { postFilters = (Map<String, CallableFunction>) req.getContext().get(Loader.POSTFILTERS); cachedResults = (Map<Object, CachedResult>) req.getContext().get(Loader.CACHEDRESULTS); CallableFunction function = postFilters.get(localParams.get("name")); return new QParser(qstr, localParams, params, req) { private ValueSource functionParamValueSource; @Override//ww w. j a va 2s . c om public Query parse() throws SyntaxError { ValueSource vs = null; Map<String, Object> params = new HashMap<String, Object>(); String funcStr = localParams.get(QueryParsing.V, null); int nParams = 1; if ((function != null) && (function.getMethod() != null)) { nParams = function.getMethod().getParameterCount(); } boolean cache = false; Object functionParams[] = new Object[nParams]; int i = 1; //in the 0th positions there's the parametric function result (as ValueSource) Iterator<String> it = localParams.getParameterNamesIterator(); while (it.hasNext()) { String p = it.next(); /* does it make sense to be able to switch on/off the cache? what would it imply? if ("cache".equals(p)) { cache = ("1".equals(localParams.get(p))); } */ if (!"v".equals(p) && !"cache".equals(p) && !"type".equals(p) && !"name".equals(p)) { params.put(p, localParams.get(p)); Class<?> expectedType = function.getMethod().getParameters()[i].getType(); if (expectedType == Integer.class) { functionParams[i] = Integer.parseInt(localParams.get(p)); } else { logger.error("param " + i + " should be of type " + expectedType + " but I don't know how to parse it."); // right place for magic params? like passing the request & so on.. ? } i++; } } if ((funcStr != null) && (funcStr != "")) { Query funcQ = subQuery(funcStr, FunctionQParserPlugin.NAME).getQuery(); //if (funcQ instanceof FunctionQuery) { //what else could be? vs = ((FunctionQuery) funcQ).getValueSource(); functionParamValueSource = vs; //todo must call getValues when using it! } else { logger.error("!!!! no function defined for the postfilter???"); } DynamicQuery dq = new DynamicQuery(vs, cache, function, functionParams, cachedResults); dq.setParams(params); return dq; } }; }
From source file:com.doculibre.constellio.servlets.ConstellioServletUtils.java
License:Open Source License
private static void addParams(NamedList<Object> responseHeader, SolrParams solrParams) { NamedList<Object> params = new NamedList<Object>(); responseHeader.add("params", params); Iterator<String> enumParams = solrParams.getParameterNamesIterator(); while (enumParams.hasNext()) { String param = enumParams.next(); if (!param.equals(ServletsConstants.DIGEST_PARAM)) { String[] values = solrParams.getParams(param); params.add(param, values.length > 1 ? Arrays.asList(values) : values[0]); }//from w w w . j av a 2 s.com } }
From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java
License:Apache License
private static String getSimpleSearchStr(SolrParams params) { // return params.toString(); StringBuffer result = new StringBuffer(); for (Iterator<String> it = params.getParameterNamesIterator(); it.hasNext();) { final String name = it.next(); if (name.equals("shard.url")) continue; final String[] values = params.getParams(name); if (values.length == 1) { result.append(name + "=" + values[0] + "&"); } else {//from ww w. jav a 2 s .c o m // currently no reason not to use the same array result.append(name + "=" + values + "&"); } } return result.toString(); }
From source file:com.su.search.client.solrj.PaHttpSolrServer.java
License:Apache License
public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { HttpRequestBase method = null;//from w w w . j a v a 2 s . co m InputStream is = null; SolrParams params = request.getParams(); // modified by wangqiang406 2012-07-27 // ?? if (null != password) { ModifiableSolrParams wparams = new ModifiableSolrParams(params); wparams.set(SimpleIndexClient.KEY_PA_AUTH, password); params = wparams; } Collection<ContentStream> streams = requestWriter.getContentStreams(request); String path = requestWriter.getPath(request); if (path == null || !path.startsWith("/")) { path = DEFAULT_PATH; } ResponseParser parser = request.getResponseParser(); if (parser == null) { parser = this.parser; } // The parser 'wt=' and 'version=' params are used instead of the original // params ModifiableSolrParams wparams = new ModifiableSolrParams(params); wparams.set(CommonParams.WT, parser.getWriterType()); wparams.set(CommonParams.VERSION, parser.getVersion()); if (invariantParams != null) { wparams.add(invariantParams); } params = wparams; int tries = maxRetries + 1; try { while (tries-- > 0) { // Note: since we aren't do intermittent time keeping // ourselves, the potential non-timeout latency could be as // much as tries-times (plus scheduling effects) the given // timeAllowed. try { if (SolrRequest.METHOD.GET == request.getMethod()) { if (streams != null) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); } method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(params, false)); } else if (SolrRequest.METHOD.POST == request.getMethod()) { String url = baseUrl + path; boolean isMultipart = (streams != null && streams.size() > 1); LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>(); if (streams == null || isMultipart) { HttpPost post = new HttpPost(url); post.setHeader("Content-Charset", "UTF-8"); if (!this.useMultiPartPost && !isMultipart) { post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); } List<FormBodyPart> parts = new LinkedList<FormBodyPart>(); Iterator<String> iter = params.getParameterNamesIterator(); while (iter.hasNext()) { String p = iter.next(); String[] vals = params.getParams(p); if (vals != null) { for (String v : vals) { if (this.useMultiPartPost || isMultipart) { parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8")))); } else { postParams.add(new BasicNameValuePair(p, v)); } } } } if (isMultipart) { for (ContentStream content : streams) { parts.add(new FormBodyPart(content.getName(), new InputStreamBody(content.getStream(), content.getName()))); } } if (parts.size() > 0) { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); for (FormBodyPart p : parts) { entity.addPart(p); } post.setEntity(entity); } else { //not using multipart HttpEntity e; post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8")); } method = post; } // It is has one stream, it is the post body, put the params in the URL else { String pstr = ClientUtils.toQueryString(params, false); HttpPost post = new HttpPost(url + pstr); // Single stream as body // Using a loop just to get the first one final ContentStream[] contentStream = new ContentStream[1]; for (ContentStream content : streams) { contentStream[0] = content; break; } if (contentStream[0] instanceof RequestWriter.LazyContentStream) { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } else { post.setEntity(new InputStreamEntity(contentStream[0].getStream(), -1) { @Override public Header getContentType() { return new BasicHeader("Content-Type", contentStream[0].getContentType()); } @Override public boolean isRepeatable() { return false; } }); } method = post; } } else { throw new SolrServerException("Unsupported method: " + request.getMethod()); } } catch (NoHttpResponseException r) { method = null; if (is != null) { is.close(); } // If out of tries then just rethrow (as normal error). if (tries < 1) { throw r; } } } } catch (IOException ex) { throw new SolrServerException("error reading streams", ex); } // TODO: move to a interceptor? method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects); method.addHeader("User-Agent", AGENT); InputStream respBody = null; try { // Execute the method. final HttpResponse response = httpClient.execute(method); int httpStatus = response.getStatusLine().getStatusCode(); // Read the contents String charset = EntityUtils.getContentCharSet(response.getEntity()); respBody = response.getEntity().getContent(); // handle some http level checks before trying to parse the response switch (httpStatus) { case HttpStatus.SC_OK: break; case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: if (!followRedirects) { throw new SolrServerException( "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ")."); } break; case HttpStatus.SC_NOT_FOUND: throw new SolrServerException("Server at " + getBaseURL() + " was not found (404)."); default: throw new SolrServerException("Server at " + getBaseURL() + " returned non ok status:" + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase()); } NamedList<Object> rsp = processor.processResponse(respBody, charset); if (httpStatus != HttpStatus.SC_OK) { String reason = null; try { NamedList err = (NamedList) rsp.get("error"); if (err != null) { reason = (String) err.get("msg"); // TODO? get the trace? } } catch (Exception ex) { } if (reason == null) { StringBuilder msg = new StringBuilder(); msg.append(response.getStatusLine().getReasonPhrase()); msg.append("\n\n"); msg.append("request: " + method.getURI()); reason = java.net.URLDecoder.decode(msg.toString(), UTF_8); } throw new SolrException(SolrException.ErrorCode.getErrorCode(httpStatus), reason); } return rsp; } catch (ConnectException e) { throw new SolrServerException("Server refused connection at: " + getBaseURL(), e); } catch (SocketTimeoutException e) { throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e); } catch (IOException e) { throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e); } finally { if (respBody != null) { try { respBody.close(); } catch (Throwable t) { } // ignore } } }
From source file:de.uni_tuebingen.ub.ixTheo.handler.component.FacetPrefixSortComponent.java
License:Apache License
/** * Actually run the query// ww w . j av a2 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:edu.ku.brc.sgr.EquateSolrParams.java
License:Open Source License
public static boolean equals(SolrParams a, SolrParams b) { ImmutableSet<String> aParamNames = ImmutableSet.copyOf(a.getParameterNamesIterator()); ImmutableSet<String> bParamNames = ImmutableSet.copyOf(b.getParameterNamesIterator()); if (!aParamNames.equals(bParamNames)) return false; for (String name : aParamNames) { if (!Arrays.equals(a.getParams(name), b.getParams(name))) return false; }//from w w w. j a v a 2s .com return true; }
From source file:io.yucca.solr.processor.HierarchyExtractorUpdateProcessor.java
License:Apache License
/** * Initialise the extraction patterns//from w w w .ja v a 2s . c o m * * @param core * SolrCore * @param params * SolrParams */ private Map<String, ExtractionPattern> initPatterns(SolrCore core, SolrParams params) { Map<String, ExtractionPattern> patterns = new LinkedHashMap<String, ExtractionPattern>(); Iterator<String> it = params.getParameterNamesIterator(); while (it.hasNext()) { String param = it.next(); addLevel(patterns, param, params); } return patterns; }
From source file:lux.solr.XQueryComponent.java
License:Mozilla Public License
private String buildHttpInfo(SolrQueryRequest req) { StringBuilder buf = new StringBuilder(); buf.append(String.format("<http>")); buf.append("<params>"); SolrParams params = req.getParams(); Iterator<String> paramNames = params.getParameterNamesIterator(); while (paramNames.hasNext()) { String param = paramNames.next(); if (param.startsWith("lux.")) { continue; }//from ww w. j a v a2 s.c o m buf.append(String.format("<param name=\"%s\">", param)); String[] values = params.getParams(param); for (String value : values) { buf.append(String.format("<value>%s</value>", xmlEscape(value))); } buf.append("</param>"); } buf.append("</params>"); String pathInfo = params.get(LUX_PATH_INFO); if (pathInfo != null) { buf.append("<path-info>").append(xmlEscape(pathInfo)).append("</path-info>"); } Map<Object, Object> context = req.getContext(); String webapp = (String) context.get("webapp"); if (webapp == null) { webapp = ""; } buf.append("<context-path>").append(webapp).append("</context-path>"); // TODO: headers, path, etc? buf.append("</http>"); return buf.toString(); }
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;//from ww w. j a v a 2 s . 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;/*ww w . j a v a 2 s .com*/ } 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); }