use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class JsonLoaderTest method checkTwoChildDocs.
private void checkTwoChildDocs(String rawJsonStr) throws Exception {
SolrQueryRequest req = req("commit", "true");
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(rawJsonStr), p);
assertEquals(1, p.addCommands.size());
AddUpdateCommand add = p.addCommands.get(0);
SolrInputDocument d = add.solrDoc;
SolrInputField f = d.getField("id");
assertEquals("1", f.getValue());
SolrInputDocument cd = d.getChildDocuments().get(0);
SolrInputField cf = cd.getField("id");
assertEquals("2", cf.getValue());
cd = d.getChildDocuments().get(1);
cf = cd.getField("id");
assertEquals("3", cf.getValue());
cf = cd.getField("foo_i");
assertEquals(2, cf.getValueCount());
assertEquals(new Object[] { 666L, 777L }, cf.getValues().toArray());
req.close();
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class JsonLoaderTest method testSimpleFormatInAdd.
public void testSimpleFormatInAdd() throws Exception {
String str = "{'add':[{'id':'1'},{'id':'2'}]}".replace('\'', '"');
SolrQueryRequest req = req();
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
assertEquals(2, p.addCommands.size());
AddUpdateCommand add = p.addCommands.get(0);
SolrInputDocument d = add.solrDoc;
SolrInputField f = d.getField("id");
assertEquals("1", f.getValue());
assertEquals(add.commitWithin, -1);
assertEquals(add.overwrite, true);
add = p.addCommands.get(1);
d = add.solrDoc;
f = d.getField("id");
assertEquals("2", f.getValue());
assertEquals(add.commitWithin, -1);
assertEquals(add.overwrite, true);
req.close();
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class JsonLoaderTest method testJsonDocFormat.
public void testJsonDocFormat() throws Exception {
String doc = "\n" + "\n" + "{\"bool\": true,\n" + " \"f0\": \"v0\",\n" + " \"f2\": {\n" + " \t \"boost\": 2.3,\n" + " \t \"value\": \"test\"\n" + " \t },\n" + "\"array\": [ \"aaa\", \"bbb\" ],\n" + "\"boosted\": {\n" + " \t \"boost\": 6.7,\n" + " \t \"value\": [ \"aaa\", \"bbb\" ]\n" + " \t }\n" + " }\n" + "\n" + "\n" + " {\"f1\": \"v1\",\n" + " \"f1\": \"v2\",\n" + " \"f2\": null\n" + " }\n";
SolrQueryRequest req = req("srcField", "_src_");
req.getContext().put("path", "/update/json/docs");
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(2, p.addCommands.size());
doc = "\n" + "\n" + "{\"bool\": true,\n" + " \"f0\": \"v0\",\n" + " \"f2\": {\n" + " \t \"boost\": 2.3,\n" + " \t \"value\": \"test\"\n" + " \t },\n" + "\"array\": [ \"aaa\", \"bbb\" ],\n" + "\"boosted\": {\n" + " \t \"boost\": 6.7,\n" + " \t \"value\": [ \"aaa\", \"bbb\" ]\n" + " \t }\n" + " }\n" + "\n" + "\n" + " {\"f1\": \"v1\",\n" + " \"f2\": \"v2\",\n" + " \"f3\": null\n" + " }\n";
req = req("srcField", "_src_");
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(2, p.addCommands.size());
String content = (String) p.addCommands.get(0).solrDoc.getFieldValue("_src_");
assertNotNull(content);
Map obj = (Map) ObjectBuilder.fromJSON(content);
assertEquals(Boolean.TRUE, obj.get("bool"));
assertEquals("v0", obj.get("f0"));
assertNotNull(obj.get("f0"));
assertNotNull(obj.get("array"));
assertNotNull(obj.get("boosted"));
content = (String) p.addCommands.get(1).solrDoc.getFieldValue("_src_");
assertNotNull(content);
obj = (Map) ObjectBuilder.fromJSON(content);
assertEquals("v1", obj.get("f1"));
assertEquals("v2", obj.get("f2"));
assertTrue(obj.containsKey("f3"));
doc = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
req = req("srcField", "_src_");
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(doc), p);
assertEquals(2, p.addCommands.size());
content = (String) p.addCommands.get(0).solrDoc.getFieldValue("_src_");
assertNotNull(content);
obj = (Map) ObjectBuilder.fromJSON(content);
assertEquals("1", obj.get("id"));
content = (String) p.addCommands.get(1).solrDoc.getFieldValue("_src_");
assertNotNull(content);
obj = (Map) ObjectBuilder.fromJSON(content);
assertEquals("2", obj.get("id"));
String json = "{a:{" + "b:[{c:c1, e:e1},{c:c2, e :e2, d:{p:q}}]," + "x:y" + "}}";
req = req("split", "/|/a/b");
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(json), p);
assertEquals(1, p.addCommands.size());
assertEquals("y", p.addCommands.get(0).solrDoc.getFieldValue("a.x"));
List<SolrInputDocument> children = p.addCommands.get(0).solrDoc.getChildDocuments();
assertEquals(2, children.size());
SolrInputDocument d = children.get(0);
assertEquals(d.getFieldValue("c"), "c1");
assertEquals(d.getFieldValue("e"), "e1");
d = children.get(1);
assertEquals(d.getFieldValue("c"), "c2");
assertEquals(d.getFieldValue("e"), "e2");
assertEquals(d.getFieldValue("d.p"), "q");
req = req(PARENT_TWO_CHILDREN_PARAMS);
req.getContext().put("path", "/update/json/docs");
rsp = new SolrQueryResponse();
p = new BufferingRequestProcessor(null);
loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(PARENT_TWO_CHILDREN_JSON), p);
assertEquals(2, p.addCommands.get(0).solrDoc.getChildDocuments().size());
assertEquals(1, p.addCommands.get(0).solrDoc.getChildDocuments().get(1).getChildDocuments().size());
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class JsonLoaderTest method testEmptyChildDocs.
@Test
public void testEmptyChildDocs() throws Exception {
String str = "{\n" + " \"add\": {\n" + " \"doc\": {\n" + " \"id\": \"1\",\n" + " \"_childDocuments_\": []\n" + " }\n" + " }\n" + "}";
SolrQueryRequest req = req("commit", "true");
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
assertEquals(1, p.addCommands.size());
AddUpdateCommand add = p.addCommands.get(0);
SolrInputDocument d = add.solrDoc;
SolrInputField f = d.getField("id");
assertEquals("1", f.getValue());
List<SolrInputDocument> cd = d.getChildDocuments();
assertNull(cd);
req.close();
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class TestPointFields method doTestPointFieldFunctionQueryError.
/**
* Checks that the specified field can not be used as a value source, even if there are documents
* with (all) the specified values in the index.
*
* @param field the field name to try and sort on
* @param errSubStr substring to look for in the error msg
* @param values one or more values to put into the doc(s) in the index - may be more then one for multivalued fields
*/
private void doTestPointFieldFunctionQueryError(String field, String errSubStr, String... values) throws Exception {
final int numDocs = atLeast(random(), 10);
for (int i = 0; i < numDocs; i++) {
SolrInputDocument doc = sdoc("id", String.valueOf(i));
for (String v : values) {
doc.addField(field, v);
}
assertU(adoc(doc));
}
assertQEx("Should not be able to use field in function: " + field, errSubStr, req("q", "*:*", "fl", "id", "fq", "{!frange l=0 h=100}product(-1, " + field + ")"), SolrException.ErrorCode.BAD_REQUEST);
clearIndex();
assertU(commit());
// empty index should (also) give same error
assertQEx("Should not be able to use field in function: " + field, errSubStr, req("q", "*:*", "fl", "id", "fq", "{!frange l=0 h=100}product(-1, " + field + ")"), SolrException.ErrorCode.BAD_REQUEST);
}
Aggregations