Search in sources :

Example 6 with MultiMapSolrParams

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

the class SolrRequestParserTest method testStreamFile.

@Test
public void testStreamFile() throws Exception {
    File file = getFile("README");
    byte[] bytes = FileUtils.readFileToByteArray(file);
    SolrCore core = h.getCore();
    Map<String, String[]> args = new HashMap<>();
    args.put(CommonParams.STREAM_FILE, new String[] { file.getAbsolutePath() });
    // Make sure it got a single stream in and out ok
    List<ContentStream> streams = new ArrayList<>();
    try (SolrQueryRequest req = parser.buildRequestFrom(core, new MultiMapSolrParams(args), streams)) {
        assertEquals(1, streams.size());
        try (InputStream in = streams.get(0).getStream()) {
            assertArrayEquals(bytes, IOUtils.toByteArray(in));
        }
    }
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) BufferedInputStream(java.io.BufferedInputStream) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 7 with MultiMapSolrParams

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

the class RestTestBase method setParam.

/**
   * Insures that the given param is included in the query with the given value.
   *
   * <ol>
   *   <li>If the param is already included with the given value, the request is returned unchanged.</li>
   *   <li>If the param is not already included, it is added with the given value.</li>
   *   <li>If the param is already included, but with a different value, the value is replaced with the given value.</li>
   *   <li>If the param is already included multiple times, they are replaced with a single param with given value.</li>
   * </ol>
   *
   * The passed-in valueToSet should NOT be URL encoded, as it will be URL encoded by this method.
   *
   * @param query The query portion of a request URL, e.g. "wt=json&indent=on&fl=id,_version_"
   * @param paramToSet The parameter name to insure the presence of in the returned request 
   * @param valueToSet The parameter value to insure in the returned request
   * @return The query with the given param set to the given value 
   */
private static String setParam(String query, String paramToSet, String valueToSet) {
    if (null == valueToSet) {
        valueToSet = "";
    }
    try {
        StringBuilder builder = new StringBuilder();
        if (null == query || query.trim().isEmpty()) {
            // empty query -> return "paramToSet=valueToSet"
            builder.append(paramToSet);
            builder.append('=');
            StrUtils.partialURLEncodeVal(builder, valueToSet);
            return builder.toString();
        }
        MultiMapSolrParams requestParams = SolrRequestParsers.parseQueryString(query);
        String[] values = requestParams.getParams(paramToSet);
        if (null == values) {
            // paramToSet isn't present in the request -> append "&paramToSet=valueToSet"
            builder.append(query);
            builder.append('&');
            builder.append(paramToSet);
            builder.append('=');
            StrUtils.partialURLEncodeVal(builder, valueToSet);
            return builder.toString();
        }
        if (1 == values.length && valueToSet.equals(values[0])) {
            // paramToSet=valueToSet is already in the query - just return the query as-is.
            return query;
        }
        // More than one value for paramToSet on the request, or paramToSet's value is not valueToSet
        // -> rebuild the query
        boolean isFirst = true;
        for (Map.Entry<String, String[]> entry : requestParams.getMap().entrySet()) {
            String key = entry.getKey();
            String[] valarr = entry.getValue();
            if (!key.equals(paramToSet)) {
                for (String val : valarr) {
                    builder.append(isFirst ? "" : '&');
                    isFirst = false;
                    builder.append(key);
                    builder.append('=');
                    StrUtils.partialURLEncodeVal(builder, null == val ? "" : val);
                }
            }
        }
        builder.append(isFirst ? "" : '&');
        builder.append(paramToSet);
        builder.append('=');
        StrUtils.partialURLEncodeVal(builder, valueToSet);
        return builder.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) IOException(java.io.IOException) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 8 with MultiMapSolrParams

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

the class SignatureUpdateProcessorFactoryTest method testNonStringFieldsValues.

@Test
public void testNonStringFieldsValues() throws Exception {
    this.chain = "dedupe-allfields";
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(chain);
    SignatureUpdateProcessorFactory factory = ((SignatureUpdateProcessorFactory) chained.getProcessors().get(0));
    factory.setEnabled(true);
    Map<String, String[]> params = new HashMap<>();
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    params.put(UpdateParams.UPDATE_CHAIN, new String[] { chain });
    UpdateRequest ureq = new UpdateRequest();
    {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 1.0f);
        doc.addField("ints_is", 34);
        doc.addField("ints_is", 42);
        ureq.add(doc);
    }
    {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 2.0f);
        doc.addField("ints_is", 42);
        doc.addField("ints_is", 66);
        ureq.add(doc);
    }
    {
        // A and B should have same sig as eachother
        // even though the particulars of how the the ints_is list are built
        SolrInputDocument docA = new SolrInputDocument();
        SolrInputDocument docB = new SolrInputDocument();
        UnusualList<Integer> ints = new UnusualList<>(3);
        for (int val : new int[] { 42, 66, 34 }) {
            docA.addField("ints_is", new Integer(val));
            ints.add(val);
        }
        docB.addField("ints_is", ints);
        for (SolrInputDocument doc : new SolrInputDocument[] { docA, docB }) {
            doc.addField("v_t", "same");
            doc.addField("weight", 3.0f);
            ureq.add(doc);
        }
    }
    {
        // now add another doc with the same values as A & B above, 
        // but diff ints_is collection (diff order)
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 3.0f);
        for (int val : new int[] { 66, 42, 34 }) {
            doc.addField("ints_is", new Integer(val));
        }
        ureq.add(doc);
    }
    ArrayList<ContentStream> streams = new ArrayList<>(2);
    streams.add(new BinaryRequestWriter().getContentStream(ureq));
    LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), mmparams);
    try {
        req.setContentStreams(streams);
        UpdateRequestHandler h = new UpdateRequestHandler();
        h.init(new NamedList());
        h.handleRequestBody(req, new SolrQueryResponse());
    } finally {
        req.close();
    }
    addDoc(commit());
    checkNumDocs(4);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ContentStream(org.apache.solr.common.util.ContentStream) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) Test(org.junit.Test)

Example 9 with MultiMapSolrParams

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

the class UniqFieldsUpdateProcessorFactoryTest method addDoc.

private void addDoc(String doc) throws Exception {
    Map<String, String[]> params = new HashMap<>();
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    params.put(UpdateParams.UPDATE_CHAIN, new String[] { "uniq-fields" });
    SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), (SolrParams) mmparams) {
    };
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    ArrayList<ContentStream> streams = new ArrayList<>(2);
    streams.add(new ContentStreamBase.StringStream(doc));
    req.setContentStreams(streams);
    handler.handleRequestBody(req, new SolrQueryResponse());
    req.close();
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 10 with MultiMapSolrParams

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

the class TestCollectionAPIs method makeCall.

public static Pair<SolrQueryRequest, SolrQueryResponse> makeCall(final ApiBag apiBag, String path, final SolrRequest.METHOD method, final String payload, final CoreContainer cc) throws Exception {
    SolrParams queryParams = new MultiMapSolrParams(Collections.emptyMap());
    if (path.indexOf('?') > 0) {
        String queryStr = path.substring(path.indexOf('?') + 1);
        path = path.substring(0, path.indexOf('?'));
        queryParams = SolrRequestParsers.parseQueryString(queryStr);
    }
    final HashMap<String, String> parts = new HashMap<>();
    Api api = apiBag.lookup(path, method.toString(), parts);
    if (api == null)
        throw new RuntimeException("No handler at path :" + path);
    SolrQueryResponse rsp = new SolrQueryResponse();
    LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, queryParams) {

        @Override
        public List<CommandOperation> getCommands(boolean validateInput) {
            if (payload == null)
                return Collections.emptyList();
            return ApiBag.getCommandOperations(new StringReader(payload), api.getCommandSchema(), true);
        }

        @Override
        public Map<String, String> getPathTemplateValues() {
            return parts;
        }

        @Override
        public String getHttpMethod() {
            return method.toString();
        }
    };
    try {
        api.call(req, rsp);
    } catch (ApiBag.ExceptionWithErrObject e) {
        throw new RuntimeException(e.getMessage() + Utils.toJSONString(e.getErrs()), e);
    }
    return new Pair<>(req, rsp);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) CommandOperation(org.apache.solr.common.util.CommandOperation) ApiBag(org.apache.solr.api.ApiBag) Utils.fromJSONString(org.apache.solr.common.util.Utils.fromJSONString) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) StringReader(java.io.StringReader) SolrParams(org.apache.solr.common.params.SolrParams) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) Api(org.apache.solr.api.Api) Pair(org.apache.solr.common.util.Pair)

Aggregations

MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)11 HashMap (java.util.HashMap)9 ContentStream (org.apache.solr.common.util.ContentStream)7 ArrayList (java.util.ArrayList)6 SolrCore (org.apache.solr.core.SolrCore)4 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 Map (java.util.Map)3 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)3 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 LinkedHashMap (java.util.LinkedHashMap)2 ServletInputStream (javax.servlet.ServletInputStream)2 SolrException (org.apache.solr.common.SolrException)2 SolrParams (org.apache.solr.common.params.SolrParams)2 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)2 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)2