use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.
the class BasicFunctionalityTest method testSolrParams.
@Test
public void testSolrParams() throws Exception {
NamedList nl = new NamedList();
nl.add("i", 555);
nl.add("s", "bbb");
nl.add("bt", "true");
nl.add("bf", "false");
Map<String, String> m = new HashMap<>();
m.put("f.field1.i", "1000");
m.put("s", "BBB");
m.put("ss", "SSS");
LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, nl);
SolrParams p = req.getParams();
assertEquals(p.get("i"), "555");
assertEquals(p.getInt("i").intValue(), 555);
assertEquals(p.getInt("i", 5), 555);
assertEquals(p.getInt("iii", 5), 5);
assertEquals(p.getFieldParam("field1", "i"), "555");
req.setParams(SolrParams.wrapDefaults(p, new MapSolrParams(m)));
p = req.getParams();
assertEquals(req.getOriginalParams().get("s"), "bbb");
assertEquals(p.get("i"), "555");
assertEquals(p.getInt("i").intValue(), 555);
assertEquals(p.getInt("i", 5), 555);
assertEquals(p.getInt("iii", 5), 5);
assertEquals(p.getFieldParam("field1", "i"), "1000");
assertEquals(p.get("s"), "bbb");
assertEquals(p.get("ss"), "SSS");
assertEquals(!!p.getBool("bt"), !p.getBool("bf"));
assertEquals(p.getBool("foo", true), true);
assertEquals(p.getBool("foo", false), false);
assertEquals(!!p.getBool("bt"), !p.getBool("bf"));
NamedList more = new NamedList();
more.add("s", "aaa");
more.add("s", "ccc");
more.add("ss", "YYY");
more.add("xx", "XXX");
p = SolrParams.wrapAppended(p, SolrParams.toSolrParams(more));
assertEquals(3, p.getParams("s").length);
assertEquals("bbb", p.getParams("s")[0]);
assertEquals("aaa", p.getParams("s")[1]);
assertEquals("ccc", p.getParams("s")[2]);
assertEquals(3, p.getParams("s").length);
assertEquals("SSS", p.get("ss"));
assertEquals("XXX", p.get("xx"));
req.close();
}
use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.
the class UpdateRequestHandler method setAssumeContentType.
protected void setAssumeContentType(String ct) {
if (invariants == null) {
Map<String, String> map = new HashMap<>();
map.put(UpdateParams.ASSUME_CONTENT_TYPE, ct);
invariants = new MapSolrParams(map);
} else {
ModifiableSolrParams params = new ModifiableSolrParams(invariants);
params.set(UpdateParams.ASSUME_CONTENT_TYPE, ct);
invariants = params;
}
}
use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.
the class TermVectorReusingLeafReader method alternateField.
/** Returns the alternate highlight object for this field -- a String[] by default. Null if none. */
@SuppressWarnings("unchecked")
protected Object alternateField(Document doc, int docId, String fieldName, FvhContainer fvhContainer, Query query, IndexReader reader, SolrQueryRequest req) throws IOException {
IndexSchema schema = req.getSearcher().getSchema();
SolrParams params = req.getParams();
String alternateField = params.getFieldParam(fieldName, HighlightParams.ALTERNATE_FIELD);
int alternateFieldLen = params.getFieldInt(fieldName, HighlightParams.ALTERNATE_FIELD_LENGTH, 0);
if (alternateField == null || alternateField.length() == 0) {
return null;
}
if (params.getFieldBool(fieldName, HighlightParams.HIGHLIGHT_ALTERNATE, true) && !alternateField.equals(fieldName)) {
// Try to highlight alternate field
Object fieldHighlights = null;
SchemaField schemaField = schema.getFieldOrNull(alternateField);
if (schemaField != null) {
HashMap<String, String> invariants = new HashMap<>();
invariants.put("f." + alternateField + "." + HighlightParams.SNIPPETS, "1");
// Enforce maxAlternateFieldLength by FRAGSIZE. Minimum 18 due to FVH limitations
invariants.put("f." + alternateField + "." + HighlightParams.FRAGSIZE, alternateFieldLen > 0 ? String.valueOf(Math.max(18, alternateFieldLen)) : String.valueOf(Integer.MAX_VALUE));
SolrParams origParams = req.getParams();
req.setParams(SolrParams.wrapDefaults(new MapSolrParams(invariants), origParams));
fieldHighlights = doHighlightingOfField(doc, docId, schemaField, fvhContainer, query, reader, req, params);
req.setParams(origParams);
if (fieldHighlights != null) {
return fieldHighlights;
}
}
}
// Fallback to static non-highlighted
IndexableField[] docFields = doc.getFields(alternateField);
if (docFields.length == 0) {
// The alternate field did not exist, treat the original field as fallback instead
docFields = doc.getFields(fieldName);
}
List<String> listFields = new ArrayList<>();
for (IndexableField field : docFields) {
if (field.binaryValue() == null)
listFields.add(field.stringValue());
}
if (listFields.isEmpty()) {
return null;
}
String[] altTexts = listFields.toArray(new String[listFields.size()]);
Encoder encoder = getEncoder(fieldName, params);
List<String> altList = new ArrayList<>();
int len = 0;
for (String altText : altTexts) {
if (alternateFieldLen <= 0) {
altList.add(encoder.encodeText(altText));
} else {
altList.add(len + altText.length() > alternateFieldLen ? encoder.encodeText(altText.substring(0, alternateFieldLen - len)) : encoder.encodeText(altText));
len += altText.length();
if (len >= alternateFieldLen)
break;
}
}
return altList;
}
use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.
the class DirectSolrConnection method request.
public String request(String path, SolrParams params, String body) throws Exception {
// Extract the handler from the path or params
SolrRequestHandler handler = core.getRequestHandler(path);
if (handler == null) {
if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
if (params == null)
params = new MapSolrParams(new HashMap<String, String>());
String qt = params.get(CommonParams.QT);
handler = core.getRequestHandler(qt);
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
}
}
}
if (handler == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
}
return request(handler, params, body);
}
use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.
the class DirectSolrConnection method request.
public String request(SolrRequestHandler handler, SolrParams params, String body) throws Exception {
if (params == null)
params = new MapSolrParams(new HashMap<String, String>());
// Make a stream for the 'body' content
List<ContentStream> streams = new ArrayList<>(1);
if (body != null && body.length() > 0) {
streams.add(new ContentStreamBase.StringStream(body));
}
SolrQueryRequest req = null;
try {
req = parser.buildRequestFrom(core, params, streams);
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(handler, req, rsp);
if (rsp.getException() != null) {
throw rsp.getException();
}
// Now write it out
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
StringWriter out = new StringWriter();
responseWriter.write(out, req, rsp);
return out.toString();
} finally {
if (req != null) {
req.close();
}
SolrRequestInfo.clearRequestInfo();
}
}
Aggregations