Search in sources :

Example 36 with MapSolrParams

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();
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) Test(org.junit.Test)

Example 37 with MapSolrParams

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;
    }
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 38 with MapSolrParams

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SchemaField(org.apache.solr.schema.SchemaField) IndexableField(org.apache.lucene.index.IndexableField) MapSolrParams(org.apache.solr.common.params.MapSolrParams) Encoder(org.apache.lucene.search.highlight.Encoder) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) IndexSchema(org.apache.solr.schema.IndexSchema)

Example 39 with MapSolrParams

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);
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrException(org.apache.solr.common.SolrException) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Example 40 with MapSolrParams

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();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Aggregations

MapSolrParams (org.apache.solr.common.params.MapSolrParams)50 HashMap (java.util.HashMap)33 SolrCore (org.apache.solr.core.SolrCore)24 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)20 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)19 SolrParams (org.apache.solr.common.params.SolrParams)14 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)10 SolrException (org.apache.solr.common.SolrException)6 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)6 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)6 Map (java.util.Map)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 Utils.makeMap (org.apache.solr.common.util.Utils.makeMap)4 IOException (java.io.IOException)3 SchemaField (org.apache.solr.schema.SchemaField)3 StringWriter (java.io.StringWriter)2 Collections.singletonMap (java.util.Collections.singletonMap)2