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));
}
}
}
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 "¶mToSet=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);
}
}
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);
}
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();
}
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);
}
Aggregations