Search in sources :

Example 51 with ModifiableSolrParams

use of org.apache.solr.common.params.ModifiableSolrParams in project lucene-solr by apache.

the class SolrTable method handleGroupByMapReduce.

private TupleStream handleGroupByMapReduce(String zk, String collection, Properties properties, final List<Map.Entry<String, Class>> fields, final String query, final List<Pair<String, String>> orders, final List<String> _buckets, final List<Pair<String, String>> metricPairs, final String limit, final String havingPredicate) throws IOException {
    Map<String, Class> fmap = new HashMap();
    for (Map.Entry<String, Class> entry : fields) {
        fmap.put(entry.getKey(), entry.getValue());
    }
    int numWorkers = Integer.parseInt(properties.getProperty("numWorkers", "1"));
    Bucket[] buckets = buildBuckets(_buckets, fields);
    Metric[] metrics = buildMetrics(metricPairs, false).toArray(new Metric[0]);
    if (metrics.length == 0) {
        return handleSelectDistinctMapReduce(zk, collection, properties, fields, query, orders, buckets, limit);
    } else {
        for (Metric metric : metrics) {
            Class c = fmap.get(metric.getIdentifier());
            if (Long.class.equals(c)) {
                metric.outputLong = true;
            }
        }
    }
    Set<String> fieldSet = getFieldSet(metrics, fields);
    if (metrics.length == 0) {
        throw new IOException("Group by queries must include atleast one aggregate function.");
    }
    String fl = getFields(fieldSet);
    String sortDirection = getSortDirection(orders);
    String sort = bucketSort(buckets, sortDirection);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(CommonParams.FL, fl);
    params.set(CommonParams.Q, query);
    params.set(CommonParams.WT, CommonParams.JAVABIN);
    //Always use the /export handler for Group By Queries because it requires exporting full result sets.
    params.set(CommonParams.QT, "/export");
    if (numWorkers > 1) {
        params.set("partitionKeys", getPartitionKeys(buckets));
    }
    params.set(SORT, sort);
    TupleStream tupleStream = null;
    CloudSolrStream cstream = new CloudSolrStream(zk, collection, params);
    tupleStream = new RollupStream(cstream, buckets, metrics);
    StreamFactory factory = new StreamFactory().withFunctionName("search", CloudSolrStream.class).withFunctionName("parallel", ParallelStream.class).withFunctionName("rollup", RollupStream.class).withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class).withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class).withFunctionName("and", AndEvaluator.class).withFunctionName("or", OrEvaluator.class).withFunctionName("not", NotEvaluator.class).withFunctionName("eq", EqualsEvaluator.class).withFunctionName("gt", GreaterThanEvaluator.class).withFunctionName("lt", LessThanEvaluator.class).withFunctionName("val", RawValueEvaluator.class).withFunctionName("lteq", LessThanEqualToEvaluator.class).withFunctionName("having", HavingStream.class).withFunctionName("gteq", GreaterThanEqualToEvaluator.class);
    if (havingPredicate != null) {
        BooleanEvaluator booleanOperation = (BooleanEvaluator) factory.constructEvaluator(StreamExpressionParser.parse(havingPredicate));
        tupleStream = new HavingStream(tupleStream, booleanOperation);
    }
    if (numWorkers > 1) {
        // Do the rollups in parallel
        // Maintain the sort of the Tuples coming from the workers.
        StreamComparator comp = bucketSortComp(buckets, sortDirection);
        ParallelStream parallelStream = new ParallelStream(zk, collection, tupleStream, numWorkers, comp);
        parallelStream.setStreamFactory(factory);
        tupleStream = parallelStream;
    }
    if (orders != null && orders.size() > 0) {
        if (!sortsEqual(buckets, sortDirection, orders)) {
            int lim = (limit == null) ? 100 : Integer.parseInt(limit);
            StreamComparator comp = getComp(orders);
            //Rank the Tuples
            //If parallel stream is used ALL the Rolled up tuples from the workers will be ranked
            //Providing a true Top or Bottom.
            tupleStream = new RankStream(tupleStream, lim, comp);
        } else {
            // Only need to limit the result, not Rank the result
            if (limit != null) {
                tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
            }
        }
    } else {
        //No order by, check for limit
        if (limit != null) {
            tupleStream = new LimitStream(tupleStream, Integer.parseInt(limit));
        }
    }
    return tupleStream;
}
Also used : RawValueEvaluator(org.apache.solr.client.solrj.io.eval.RawValueEvaluator) StreamComparator(org.apache.solr.client.solrj.io.comp.StreamComparator) NotEvaluator(org.apache.solr.client.solrj.io.eval.NotEvaluator) GreaterThanEvaluator(org.apache.solr.client.solrj.io.eval.GreaterThanEvaluator) AndEvaluator(org.apache.solr.client.solrj.io.eval.AndEvaluator) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BooleanEvaluator(org.apache.solr.client.solrj.io.eval.BooleanEvaluator) IOException(java.io.IOException) StreamFactory(org.apache.solr.client.solrj.io.stream.expr.StreamFactory)

Example 52 with ModifiableSolrParams

use of org.apache.solr.common.params.ModifiableSolrParams in project lucene-solr by apache.

the class RequestUtil method processParams.

/**
   * Set default-ish params on a SolrQueryRequest as well as do standard macro processing and JSON request parsing.
   *
   * @param handler The search handler this is for (may be null if you don't want this method touching the content streams)
   * @param req The request whose params we are interested in
   * @param defaults values to be used if no values are specified in the request params
   * @param appends values to be appended to those from the request (or defaults) when dealing with multi-val params, or treated as another layer of defaults for singl-val params.
   * @param invariants values which will be used instead of any request, or default values, regardless of context.
   */
public static void processParams(SolrRequestHandler handler, SolrQueryRequest req, SolrParams defaults, SolrParams appends, SolrParams invariants) {
    boolean searchHandler = handler instanceof SearchHandler;
    SolrParams params = req.getParams();
    // Handle JSON stream for search requests
    if (searchHandler && req.getContentStreams() != null) {
        Map<String, String[]> map = MultiMapSolrParams.asMultiMap(params, false);
        if (!(params instanceof MultiMapSolrParams || params instanceof ModifiableSolrParams)) {
            // need to set params on request since we weren't able to access the original map
            params = new MultiMapSolrParams(map);
            req.setParams(params);
        }
        // params from the query string should come after (and hence override) JSON content streams
        String[] jsonFromParams = map.remove(JSON);
        for (ContentStream cs : req.getContentStreams()) {
            String contentType = cs.getContentType();
            if (contentType == null || !contentType.contains("/json")) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Bad contentType for search handler :" + contentType + " request=" + req);
            }
            try {
                String jsonString = IOUtils.toString(cs.getReader());
                if (jsonString != null) {
                    MultiMapSolrParams.addParam(JSON, jsonString, map);
                }
            } catch (IOException e) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Exception reading content stream for request:" + req, e);
            }
        }
        // append existing "json" params
        if (jsonFromParams != null) {
            for (String json : jsonFromParams) {
                MultiMapSolrParams.addParam(JSON, json, map);
            }
        }
    }
    String[] jsonS = params.getParams(JSON);
    boolean hasAdditions = defaults != null || invariants != null || appends != null || jsonS != null;
    // short circuit processing
    if (!hasAdditions && !params.getBool("expandMacros", true)) {
        // nothing to do...
        return;
    }
    boolean isShard = params.getBool("isShard", false);
    Map<String, String[]> newMap = MultiMapSolrParams.asMultiMap(params, hasAdditions);
    // The parameters we extract will be propagated anyway.
    if (jsonS != null && !isShard) {
        for (String json : jsonS) {
            getParamsFromJSON(newMap, json);
        }
    }
    // first populate defaults, etc..
    if (defaults != null) {
        Map<String, String[]> defaultsMap = MultiMapSolrParams.asMultiMap(defaults);
        for (Map.Entry<String, String[]> entry : defaultsMap.entrySet()) {
            String key = entry.getKey();
            if (!newMap.containsKey(key)) {
                newMap.put(key, entry.getValue());
            }
        }
    }
    if (appends != null) {
        Map<String, String[]> appendsMap = MultiMapSolrParams.asMultiMap(appends);
        for (Map.Entry<String, String[]> entry : appendsMap.entrySet()) {
            String key = entry.getKey();
            String[] arr = newMap.get(key);
            if (arr == null) {
                newMap.put(key, entry.getValue());
            } else {
                String[] appendArr = entry.getValue();
                String[] newArr = new String[arr.length + appendArr.length];
                System.arraycopy(arr, 0, newArr, 0, arr.length);
                System.arraycopy(appendArr, 0, newArr, arr.length, appendArr.length);
                newMap.put(key, newArr);
            }
        }
    }
    if (invariants != null) {
        newMap.putAll(MultiMapSolrParams.asMultiMap(invariants));
    }
    if (!isShard) {
        // Don't expand macros in shard requests
        String[] doMacrosStr = newMap.get("expandMacros");
        boolean doMacros = true;
        if (doMacrosStr != null) {
            doMacros = "true".equals(doMacrosStr[0]);
        }
        if (doMacros) {
            newMap = MacroExpander.expand(newMap);
        }
    }
    // Set these params as soon as possible so if there is an error processing later, things like
    // "wt=json" will take effect from the defaults.
    // newMap may still change below, but that should be OK
    SolrParams newParams = new MultiMapSolrParams(newMap);
    req.setParams(newParams);
    // For example json.command started to be used  in SOLR-6294, and that caused errors here.
    if (!searchHandler)
        return;
    Map<String, Object> json = null;
    // Handle JSON body first, so query params will always overlay on that
    jsonS = newMap.get(JSON);
    if (jsonS != null) {
        if (json == null) {
            json = new LinkedHashMap<>();
        }
        mergeJSON(json, JSON, jsonS, new ObjectUtil.ConflictHandler());
    }
    for (String key : newMap.keySet()) {
        // json.nl, json.wrf are existing query parameters
        if (key.startsWith("json.") && !("json.nl".equals(key) || "json.wrf".equals(key))) {
            if (json == null) {
                json = new LinkedHashMap<>();
            }
            mergeJSON(json, key, newMap.get(key), new ObjectUtil.ConflictHandler());
        }
    }
    // implement compat for existing components...
    if (json != null && !isShard) {
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            String key = entry.getKey();
            String out = null;
            boolean arr = false;
            if ("query".equals(key)) {
                out = "q";
            } else if ("filter".equals(key)) {
                out = "fq";
                arr = true;
            } else if ("fields".equals(key)) {
                out = "fl";
                arr = true;
            } else if ("offset".equals(key)) {
                out = "start";
            } else if ("limit".equals(key)) {
                out = "rows";
            } else if (SORT.equals(key)) {
                out = SORT;
            } else if ("params".equals(key) || "facet".equals(key)) {
                // handled elsewhere
                continue;
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown top-level key in JSON request : " + key);
            }
            Object val = entry.getValue();
            if (arr) {
                String[] existing = newMap.get(out);
                List lst = val instanceof List ? (List) val : null;
                int existingSize = existing == null ? 0 : existing.length;
                int jsonSize = lst == null ? 1 : lst.size();
                String[] newval = new String[existingSize + jsonSize];
                for (int i = 0; i < existingSize; i++) {
                    newval[i] = existing[i];
                }
                if (lst != null) {
                    for (int i = 0; i < jsonSize; i++) {
                        Object v = lst.get(i);
                        newval[existingSize + i] = v.toString();
                    }
                } else {
                    newval[newval.length - 1] = val.toString();
                }
                newMap.put(out, newval);
            } else {
                newMap.put(out, new String[] { val.toString() });
            }
        }
    }
    if (json != null) {
        req.setJSON(json);
    }
}
Also used : SearchHandler(org.apache.solr.handler.component.SearchHandler) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) ContentStream(org.apache.solr.common.util.ContentStream) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Example 53 with ModifiableSolrParams

use of org.apache.solr.common.params.ModifiableSolrParams in project lucene-solr by apache.

the class SubQueryAugmenter method retainAndShiftPrefix.

private SolrParams retainAndShiftPrefix(SolrParams params, String subPrefix) {
    ModifiableSolrParams out = new ModifiableSolrParams();
    Iterator<String> baseKeyIt = params.getParameterNamesIterator();
    while (baseKeyIt.hasNext()) {
        String key = baseKeyIt.next();
        if (key.startsWith(subPrefix)) {
            out.set(key.substring(subPrefix.length()), params.getParams(key));
        }
    }
    return out;
}
Also used : ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 54 with ModifiableSolrParams

use of org.apache.solr.common.params.ModifiableSolrParams in project lucene-solr by apache.

the class QParser method getParser.

/** Create a <code>QParser</code> to parse <code>qstr</code>,
   * assuming that the default query parser is <code>defaultParser</code>.
   * The query parser may be overridden by local parameters in the query
   * string itself.  For example if defaultParser=<code>"dismax"</code>
   * and qstr=<code>foo</code>, then the dismax query parser will be used
   * to parse and construct the query object.  However
   * if qstr=<code>{!prefix f=myfield}foo</code>
   * then the prefix query parser will be used.
   */
public static QParser getParser(String qstr, String defaultParser, SolrQueryRequest req) throws SyntaxError {
    // SolrParams localParams = QueryParsing.getLocalParams(qstr, req.getParams());
    String stringIncludingLocalParams = qstr;
    ModifiableSolrParams localParams = null;
    SolrParams globalParams = req.getParams();
    boolean valFollowedParams = true;
    int localParamsEnd = -1;
    if (qstr != null && qstr.startsWith(QueryParsing.LOCALPARAM_START)) {
        localParams = new ModifiableSolrParams();
        localParamsEnd = QueryParsing.parseLocalParams(qstr, 0, localParams, globalParams);
        String val = localParams.get(QueryParsing.V);
        if (val != null) {
            // val was directly specified in localParams via v=<something> or v=$arg
            valFollowedParams = false;
        } else {
            // use the remainder of the string as the value
            valFollowedParams = true;
            val = qstr.substring(localParamsEnd);
            localParams.set(QueryParsing.V, val);
        }
    }
    String parserName;
    if (localParams == null) {
        parserName = defaultParser;
    } else {
        parserName = localParams.get(QueryParsing.TYPE, defaultParser);
        qstr = localParams.get("v");
    }
    parserName = parserName == null ? QParserPlugin.DEFAULT_QTYPE : parserName;
    QParserPlugin qplug = req.getCore().getQueryPlugin(parserName);
    QParser parser = qplug.createParser(qstr, localParams, req.getParams(), req);
    parser.stringIncludingLocalParams = stringIncludingLocalParams;
    parser.valFollowedParams = valFollowedParams;
    parser.localParamsEnd = localParamsEnd;
    return parser;
}
Also used : ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 55 with ModifiableSolrParams

use of org.apache.solr.common.params.ModifiableSolrParams in project lucene-solr by apache.

the class QueryParsing method getLocalParams.

/**
   * "foo" returns null
   * "{!prefix f=myfield}yes" returns type="prefix",f="myfield",v="yes"
   * "{!prefix f=myfield v=$p}" returns type="prefix",f="myfield",v=params.get("p")
   */
public static SolrParams getLocalParams(String txt, SolrParams params) throws SyntaxError {
    if (txt == null || !txt.startsWith(LOCALPARAM_START)) {
        return null;
    }
    ModifiableSolrParams localParams = new ModifiableSolrParams();
    int start = QueryParsing.parseLocalParams(txt, 0, localParams, params);
    String val = localParams.get(V);
    if (val == null) {
        val = txt.substring(start);
        localParams.set(V, val);
    } else {
    // localParams.put(VAL_EXPLICIT, "true");
    }
    return localParams;
}
Also used : ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Aggregations

ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)524 Test (org.junit.Test)168 ArrayList (java.util.ArrayList)87 NamedList (org.apache.solr.common.util.NamedList)76 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)74 SolrException (org.apache.solr.common.SolrException)68 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)66 HashMap (java.util.HashMap)61 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)58 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)57 List (java.util.List)56 IOException (java.io.IOException)55 Map (java.util.Map)52 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)52 SolrInputDocument (org.apache.solr.common.SolrInputDocument)51 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)48 Tuple (org.apache.solr.client.solrj.io.Tuple)47 SolrParams (org.apache.solr.common.params.SolrParams)42 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 SolrRequest (org.apache.solr.client.solrj.SolrRequest)36