Search in sources :

Example 16 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class SolrExampleTests method testRawFields.

@Test
public void testRawFields() throws Exception {
    String rawJson = "{ \"raw\": 1.234, \"id\":\"111\" }";
    String rawXml = "<hello>this is <some/><xml/></hello>";
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "111");
    doc.addField("name", "doc1");
    doc.addField("json_s", rawJson);
    doc.addField("xml_s", rawXml);
    client.add(doc);
    // make sure this gets in first
    client.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.set(CommonParams.FL, "id,json_s:[json],xml_s:[xml]");
    QueryRequest req = new QueryRequest(query);
    req.setResponseParser(new BinaryResponseParser());
    QueryResponse rsp = req.process(client);
    SolrDocumentList out = rsp.getResults();
    assertEquals(1, out.getNumFound());
    SolrDocument out1 = out.get(0);
    assertEquals("111", out1.getFieldValue("id"));
    // Check that the 'raw' fields are unchanged using the standard formats
    assertEquals(rawJson, out1.get("json_s"));
    assertEquals(rawXml, out1.get("xml_s"));
    if (client instanceof EmbeddedSolrServer) {
        // the EmbeddedSolrServer ignores the configured parser
        return;
    }
    // Check raw JSON Output
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "json");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("json"));
    NamedList<Object> resp = client.request(req);
    String raw = (String) resp.get("response");
    // Check that the response parses as JSON
    JSONParser parser = new JSONParser(raw);
    int evt = parser.nextEvent();
    while (evt != JSONParser.EOF) {
        evt = parser.nextEvent();
    }
    // no escaping
    assertTrue(raw.indexOf(rawJson) > 0);
    // quoted xml
    assertTrue(raw.indexOf('"' + rawXml + '"') > 0);
    // Check raw XML Output
    req.setResponseParser(new NoOpResponseParser("xml"));
    query.set("fl", "id,json_s:[json],xml_s:[xml]");
    query.set(CommonParams.WT, "xml");
    req = new QueryRequest(query);
    req.setResponseParser(new NoOpResponseParser("xml"));
    resp = client.request(req);
    raw = (String) resp.get("response");
    // Check that we get raw xml and json is escaped
    // escaped
    assertTrue(raw.indexOf('>' + rawJson + '<') > 0);
    // raw xml
    assertTrue(raw.indexOf(rawXml) > 0);
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) JSONParser(org.noggit.JSONParser) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 17 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class Utils method fromJSON.

public static Object fromJSON(byte[] utf8) {
    // convert directly from bytes to chars
    // and parse directly from that instead of going through
    // intermediate strings or readers
    CharArr chars = new CharArr();
    ByteUtils.UTF8toUTF16(utf8, 0, utf8.length, chars);
    JSONParser parser = new JSONParser(chars.getArray(), chars.getStart(), chars.length());
    try {
        return ObjectBuilder.getVal(parser);
    } catch (IOException e) {
        // should never happen w/o using real IO
        throw new RuntimeException(e);
    }
}
Also used : JSONParser(org.noggit.JSONParser) IOException(java.io.IOException) CharArr(org.noggit.CharArr)

Example 18 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class ValidatingJsonMap method fromJSON.

public static ValidatingJsonMap fromJSON(Reader s, String includeLocation) {
    try {
        ValidatingJsonMap map = (ValidatingJsonMap) getObjectBuilder(new JSONParser(s)).getObject();
        handleIncludes(map, includeLocation, 4);
        return map;
    } catch (IOException e) {
        throw new RuntimeException();
    }
}
Also used : JSONParser(org.noggit.JSONParser) IOException(java.io.IOException)

Example 19 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaConcurrent method invokeBulkReplaceCall.

private void invokeBulkReplaceCall(int seed, ArrayList<String> errs) throws Exception {
    String payload = "{\n" + "          'replace-field' : {\n" + "                       'name':'replaceFieldA',\n" + "                       'type': 'text',\n" + "                       'stored':true,\n" + "                       'indexed':true\n" + "                       },\n" + "          'replace-dynamic-field' : {\n" + "                       'name' :'replaceDynamicField',\n" + "                        'type':'text',\n" + "                        'stored':true,\n" + "                        'indexed':true\n" + "                        },\n" + "          'replace-field-type' : {\n" + "                       'name' :'myNewFieldTypeName',\n" + "                       'class' : 'solr.TextField'\n" + "                        }\n" + " }";
    String aField = "a" + seed;
    String dynamicFldName = "*_lol" + seed;
    String dynamicCopyFldDest = "hello_lol" + seed;
    String newFieldTypeName = "mystr" + seed;
    payload = payload.replace("replaceFieldA", aField);
    payload = payload.replace("replaceDynamicField", dynamicFldName);
    payload = payload.replace("myNewFieldTypeName", newFieldTypeName);
    RestTestHarness publisher = restTestHarnesses.get(r.nextInt(restTestHarnesses.size()));
    String response = publisher.post("/schema?wt=json", SolrTestCaseJ4.json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    Object errors = map.get("errors");
    if (errors != null) {
        errs.add(new String(Utils.toJSON(errors), StandardCharsets.UTF_8));
        return;
    }
    //get another node
    Set<String> errmessages = new HashSet<>();
    RestTestHarness harness = restTestHarnesses.get(r.nextInt(restTestHarnesses.size()));
    try {
        long startTime = System.nanoTime();
        long maxTimeoutMillis = 100000;
        while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS) < maxTimeoutMillis) {
            errmessages.clear();
            Map m = getObj(harness, aField, "fields");
            if (m == null)
                errmessages.add(StrUtils.formatString("field {0} no longer present", aField));
            m = getObj(harness, dynamicFldName, "dynamicFields");
            if (m == null)
                errmessages.add(StrUtils.formatString("dynamic field {0} no longer present", dynamicFldName));
            List l = getSourceCopyFields(harness, aField);
            if (!checkCopyField(l, aField, dynamicCopyFldDest))
                errmessages.add(StrUtils.formatString("CopyField source={0},dest={1} no longer present", aField, dynamicCopyFldDest));
            m = getObj(harness, newFieldTypeName, "fieldTypes");
            if (m == null)
                errmessages.add(StrUtils.formatString("new type {0} no longer present", newFieldTypeName));
            if (errmessages.isEmpty())
                break;
            Thread.sleep(10);
        }
    } finally {
        harness.close();
    }
    if (!errmessages.isEmpty()) {
        errs.addAll(errmessages);
    }
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Example 20 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaConcurrent method invokeBulkAddCall.

private void invokeBulkAddCall(int seed, ArrayList<String> errs) throws Exception {
    String payload = "{\n" + "          'add-field' : {\n" + "                       'name':'replaceFieldA',\n" + "                       'type': 'string',\n" + "                       'stored':true,\n" + "                       'indexed':false\n" + "                       },\n" + "          'add-dynamic-field' : {\n" + "                       'name' :'replaceDynamicField',\n" + "                       'type':'string',\n" + "                       'stored':true,\n" + "                       'indexed':true\n" + "                       },\n" + "          'add-copy-field' : {\n" + "                       'source' :'replaceFieldA',\n" + "                       'dest':['replaceDynamicCopyFieldDest']\n" + "                       },\n" + "          'add-field-type' : {\n" + "                       'name' :'myNewFieldTypeName',\n" + "                       'class' : 'solr.StrField',\n" + "                       'sortMissingLast':'true'\n" + "                       }\n" + " }";
    String aField = "a" + seed;
    String dynamicFldName = "*_lol" + seed;
    String dynamicCopyFldDest = "hello_lol" + seed;
    String newFieldTypeName = "mystr" + seed;
    payload = payload.replace("replaceFieldA", aField);
    payload = payload.replace("replaceDynamicField", dynamicFldName);
    payload = payload.replace("replaceDynamicCopyFieldDest", dynamicCopyFldDest);
    payload = payload.replace("myNewFieldTypeName", newFieldTypeName);
    RestTestHarness publisher = restTestHarnesses.get(r.nextInt(restTestHarnesses.size()));
    String response = publisher.post("/schema?wt=json", SolrTestCaseJ4.json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    Object errors = map.get("errors");
    if (errors != null) {
        errs.add(new String(Utils.toJSON(errors), StandardCharsets.UTF_8));
        return;
    }
    //get another node
    Set<String> errmessages = new HashSet<>();
    RestTestHarness harness = restTestHarnesses.get(r.nextInt(restTestHarnesses.size()));
    try {
        long startTime = System.nanoTime();
        long maxTimeoutMillis = 100000;
        while (TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS) < maxTimeoutMillis) {
            errmessages.clear();
            Map m = getObj(harness, aField, "fields");
            if (m == null)
                errmessages.add(StrUtils.formatString("field {0} not created", aField));
            m = getObj(harness, dynamicFldName, "dynamicFields");
            if (m == null)
                errmessages.add(StrUtils.formatString("dynamic field {0} not created", dynamicFldName));
            List l = getSourceCopyFields(harness, aField);
            if (!checkCopyField(l, aField, dynamicCopyFldDest))
                errmessages.add(StrUtils.formatString("CopyField source={0},dest={1} not created", aField, dynamicCopyFldDest));
            m = getObj(harness, newFieldTypeName, "fieldTypes");
            if (m == null)
                errmessages.add(StrUtils.formatString("new type {0}  not created", newFieldTypeName));
            if (errmessages.isEmpty())
                break;
            Thread.sleep(10);
        }
    } finally {
        harness.close();
    }
    if (!errmessages.isEmpty()) {
        errs.addAll(errmessages);
    }
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

JSONParser (org.noggit.JSONParser)37 Map (java.util.Map)30 StringReader (java.io.StringReader)25 RestTestHarness (org.apache.solr.util.RestTestHarness)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6 HttpEntity (org.apache.http.HttpEntity)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 SolrException (org.apache.solr.common.SolrException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 NamedList (org.apache.solr.common.util.NamedList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2