Search in sources :

Example 86 with SolrInputDocument

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();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 87 with SolrInputDocument

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();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 88 with SolrInputDocument

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());
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) JsonLoader(org.apache.solr.handler.loader.JsonLoader) Map(java.util.Map) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 89 with SolrInputDocument

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();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 90 with SolrInputDocument

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);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) LongPoint(org.apache.lucene.document.LongPoint) DoublePoint(org.apache.lucene.document.DoublePoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint)

Aggregations

SolrInputDocument (org.apache.solr.common.SolrInputDocument)520 Test (org.junit.Test)166 ArrayList (java.util.ArrayList)98 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)76 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)69 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)63 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)55 SolrQuery (org.apache.solr.client.solrj.SolrQuery)54 IOException (java.io.IOException)47 SolrException (org.apache.solr.common.SolrException)47 IndexSchema (org.apache.solr.schema.IndexSchema)41 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 HashMap (java.util.HashMap)39 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 List (java.util.List)33 SolrServerException (org.apache.solr.client.solrj.SolrServerException)32 SolrDocument (org.apache.solr.common.SolrDocument)31 NamedList (org.apache.solr.common.util.NamedList)31 Map (java.util.Map)30 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)29