List of usage examples for org.apache.solr.common.params SolrParams get
public abstract String get(String param);
From source file:GeonamesQParserPlugin.java
License:Apache License
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override/*from w w w .ja v a2 s . c om*/ public Query parse() throws ParseException { final int rows = localParams.getInt("rows", 1); final int start = localParams.getInt("start", 0); String topo = localParams.get("topo"); String distFunc = localParams.get("dist"); String latFieldName = localParams.get("lat"); String lonFieldName = localParams.get("lon"); String ghFieldName = localParams.get("gh"); String units = localParams.get("unit", "M"); float boost = localParams.getFloat("boost", 1.0f); double radius = Constants.EARTH_RADIUS_MI; if (units.equalsIgnoreCase("KM")) { radius = Constants.EARTH_RADIUS_KM; } ValueSource vs = null, latVS = null, lonVS = null, ghVS = null; IndexSchema schema = req.getSchema(); DistType distType = DistType.NORM; float power = 2; List<ValueSource> latLon = new ArrayList<ValueSource>(2); if (ghFieldName != null && ghFieldName.equals("") == false) { FieldType ft = schema.getFieldType(ghFieldName); SchemaField sf = schema.getField(ghFieldName); ghVS = ft.getValueSource(sf, this);//Should we pass this here? distType = DistType.GHHSIN; } else if (latFieldName != null && latFieldName.equals("") == false && lonFieldName != null && lonFieldName.equals("") == false) { FieldType ftLat = schema.getFieldType(latFieldName); FieldType ftLon = schema.getFieldType(lonFieldName); SchemaField sfLat = schema.getField(latFieldName); SchemaField sfLon = schema.getField(lonFieldName); latVS = ftLat.getValueSource(sfLat, this); lonVS = ftLon.getValueSource(sfLon, this); latLon.add(latVS); latLon.add(lonVS); try { power = Float.parseFloat(distFunc); distType = DistType.NORM; } catch (NumberFormatException e) { if (distFunc.equals("hsin")) { distType = DistType.HSIN; } else if (distFunc.equals("ghsin")) { distType = DistType.GHHSIN; } } } else { throw new ParseException("Either gh or both lat and lon must be specified."); } ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria(); searchCriteria.setQ(topo); searchCriteria.setMaxRows(rows); searchCriteria.setStartRow(start); Query query = null; try { ToponymSearchResult searchResult = WebService.search(searchCriteria); List<Toponym> topos = searchResult.getToponyms(); if (topos.size() > 1) { BooleanQuery tmp = new BooleanQuery(); for (Toponym toponym : topos) { double lat = toponym.getLatitude(); double lon = toponym.getLongitude(); FunctionQuery fq = getFunction(distType, ghVS, power, latLon, lat, lon, radius); tmp.add(fq, BooleanClause.Occur.SHOULD); } query = tmp; } else if (topos.size() == 1) { Toponym curr = topos.get(0); query = getFunction(distType, ghVS, power, latLon, curr.getLatitude(), curr.getLongitude(), radius); } } catch (Exception e) { //TODO: deal with errors } query.setBoost(boost); return query; } }; }
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/* w w w . j a v a 2 s. c o m*/ 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:alba.solr.transformers.Transformer.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w ww . j a va 2s.c om*/ public TransformerWithContext create(String field, SolrParams params, SolrQueryRequest req) { // TODO Auto-generated method stub Logger logger = LoggerFactory.getLogger(this.getClass().getName()); transformers = (Map<String, CallableFunction>) req.getContext().get(Loader.DOCTRANSFORMERS); String name = params.get("name"); if (name == null) { logger.error("no param name found for transformer " + this.getClass().getName()); return null; } CallableFunction function = transformers.get(name); if (function == null) { logger.error("no mapped function found for transformer " + name); } PluggableDocTransformer docTransformer = new PluggableDocTransformer(function); docTransformer.context = req.getContext(); return docTransformer; }
From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java
License:Apache License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { SolrParams params = req.getParams(); if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) { String q = params.get(CommonParams.Q); if (q == null) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter")); return; }/* ww w .j a v a 2 s.com*/ String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) : FIELDS; if (fields == null) { rsp.add("error", error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter")); return; } String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF); if (df == null) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter")); return; } int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT); if (limit < 1) { rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0")); return; } String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS; Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE); //TODO replace if (multivalue) { rsp.add("error", error(500, "Multivalue suggestions are not yet supported!")); return; } suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue); } else { super.handleRequestBody(req, rsp); } }
From source file:com.adr.bigdata.search.product.cm.CmBackendQuery.java
public CmBackendQuery(SolrQueryRequest req) { SolrParams param = req.getParams(); this.keyword = param.get(Params.KEYWORD); this.start = param.get(Params.START, "0"); this.rows = param.get(Params.ROWS, "10"); this.sort = param.get(Params.SORT); this.dateFromTo = param.get(Params.DATE_FROM_TO); this.categoryIds = param.getParams(Params.CATEGORY_ID); this.warehouseIds = param.getParams(Params.WAREHOUSE_ID); this.warehouseProductItemMappingId = param.getParams(Params.WAREHOUSE_PRODUCT_ITEM_MAPPING_ID); this.merchantIds = param.getParams(Params.MERCHANT_ID); this.brandIds = param.getParams(Params.BRAND_ID); this.productItemIds = param.getParams(Params.PRODUCT_ITEM_ID); this.visible = param.get(Params.VISIBLE); this.productItemStatuses = param.getParams(Params.PRODUCT_ITEM_STATUS); this.merchantProductItemStatuses = param.getParams(Params.MERCHANT_PRODUCT_ITEM_STATUS); this.productItemTypes = param.getParams(Params.PRODUCT_ITEM_TYPE); this.inStock = param.get(Params.IN_STOCK); this.approved = param.get(Params.APPROVED); this.safetyStock = param.get(Params.SAFETY_STOCK); this.isoriginal = param.get(Params.IS_ORIGINAL); }
From source file:com.alipay.tiansuan.solrplugin.SelectInLocalFileQParserPlugin.java
License:Apache License
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override/*from w w w. j av a2 s . c o m*/ public Query parse() throws ParseException { try { String file = localParams.get(QueryParsing.V); String field = localParams.get(QueryParsing.F); SchemaField sf = req.getSchema().getField(field); FieldType ft = sf.getType(); if (ft instanceof StrField) { return new StringSelectInQuery<String>(file, field, new HdfsToSet.TransString()); } if (ft instanceof TrieField) { TrieField tft = (TrieField) ft; TrieTypes cdt = tft.getType(); if (cdt.equals(TrieTypes.INTEGER)) { return new NumericSelectInQuery<Integer>(field, tft.getPrecisionStep(), DataType.INT, new HdfsToSet.TransInt(), file); } if (cdt.equals(TrieTypes.LONG)) { return new NumericSelectInQuery<Long>(field, tft.getPrecisionStep(), DataType.LONG, new HdfsToSet.TransLong(), file); } if (cdt.equals(TrieTypes.FLOAT)) { return new NumericSelectInQuery<Float>(field, tft.getPrecisionStep(), DataType.FLOAT, new HdfsToSet.TransFloat(), file); } if (cdt.equals(TrieTypes.DOUBLE)) { return new NumericSelectInQuery<Double>(field, tft.getPrecisionStep(), DataType.DOUBLE, new HdfsToSet.TransDouble(), file); } if (cdt.equals(TrieTypes.DATE)) { return new NumericSelectInQuery<Long>(field, tft.getPrecisionStep(), DataType.LONG, new HdfsToSet.TransDate(), file); } } throw new ParseException("file type error"); } catch (Exception e) { throw new ParseException(e.toString()); } } }; }
From source file:com.alipay.tiansuan.solrplugin.StringCotainsQParserPlugin.java
License:Apache License
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { return new QParser(qstr, localParams, params, req) { @Override//from w ww .j a v a2s . c om public Query parse() throws ParseException { try { String contains = localParams.get(QueryParsing.V); String field = localParams.get(QueryParsing.F); return new StringContainsQuery(contains.split(","), field); } catch (Exception e) { throw new ParseException(e.toString()); } } }; }
From source file:com.billiger.solr.handler.component.QLTBComponent.java
License:Apache License
/** * Check if this component is disabled for this particular request. * This component can be disabled on a per-request basis by either * adding qltb=false (substitute "qltb" with configured component name) * or adding opt_out=qltb (substitute...) to the SOLR request. */// w w w . ja v a 2 s . c o m private final boolean disabled(final ResponseBuilder responseBuilder) { SolrParams requestParams = responseBuilder.req.getParams(); String componentName = initArgs.get(COMPONENT_NAME); String disable = requestParams.get(DISABLE_COMPONENT_PARAM); if (disable != null && Arrays.asList(disable.split(",")).contains(componentName)) { return true; } return requestParams.getBool(componentName, false); }
From source file:com.browseengine.bobo.servlet.BrowseServlet.java
License:Open Source License
@Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { SolrParams params = new BoboHttpRequestParam(req); String qstring = params.get(CommonParams.Q); String df = params.get(CommonParams.DF); String sortString = params.get(CommonParams.SORT); BoboDefaultQueryBuilder qbuilder = new BoboDefaultQueryBuilder(); Query query = qbuilder.parseQuery(qstring, df); Sort sort = qbuilder.parseSort(sortString); BrowseRequest br = null;//www . ja v a 2 s .c o m try { br = BoboRequestBuilder.buildRequest(params, query, sort); BrowseResult result = _svc.browse(br); res.setCharacterEncoding("UTF-8"); Writer writer = res.getWriter(); try { String val = BrowseJSONSerializer.serialize(result); writer.write(val); } catch (JSONException je) { throw new IOException(je.getMessage()); } } catch (BrowseException e) { throw new ServletException(e.getMessage(), e); } }
From source file:com.cominvent.solr.RequestSanitizerComponent.java
License:Apache License
/** * Modify input params if mappings exist * @param origParams the set of input parameters for the request * @param mappings the mappings parsed from the config * @return a Map of override values which can be applied on top of the original params */// ww w.j a v a 2 s . c o m protected Map<String, String> getModifications(SolrParams origParams, Map<String, Map<String, String>> mappings) { Map<String, String> params = new HashMap<String, String>(); if (mappings == null) { throw new SolrException(ErrorCode.BAD_REQUEST, "mappings cannot be null"); } for (String parToReplace : mappings.keySet()) { Map<String, String> keyVal = mappings.get(parToReplace); if (keyVal.get("invariant") != null) { params.put(parToReplace, keyVal.get("invariant")); log.debug("Param " + parToReplace + " not given in request, setting to invariant: " + keyVal.get("invariant")); continue; } if (origParams.get(parToReplace) == null) { if (keyVal.get("default") != null) { params.put(parToReplace, keyVal.get("default")); log.debug("Param " + parToReplace + " not given in request, setting to default: " + keyVal.get("default")); } continue; } String origVal = origParams.get(parToReplace); for (String k : keyVal.keySet()) { if (k.startsWith(">") || k.startsWith("<")) { int trueCondition = k.startsWith(">") ? 1 : -1; Long cmp; try { cmp = Long.parseLong(k.substring(1)); } catch (NumberFormatException e) { log.error("Wrong format of replace rule for param " + parToReplace + ":" + k + ":" + keyVal.get(k)); throw new SolrException(ErrorCode.BAD_REQUEST, "Wrong format of replace rule for param " + parToReplace + ":" + k + ":" + keyVal.get(k)); } try { Long orig = Long.parseLong(origVal); if (orig.compareTo(cmp) == trueCondition || orig.compareTo(cmp) == 0) { log.debug("Param " + parToReplace + " hit rule " + k); params.put(parToReplace, keyVal.get(k)); break; } } catch (NumberFormatException e) { log.debug("Target value is not a number, ignoring max test"); } } else if (k.equals(origVal)) { log.debug("Replacing param " + parToReplace + "=" + origVal + "=>" + keyVal.get(k)); params.put(parToReplace, keyVal.get(k)); break; } } } return params; }