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);
}
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);
}
}
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();
}
}
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);
}
}
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);
}
}
Aggregations