Java Code Examples for org.apache.solr.common.SolrInputDocument#addChildDocument()

The following examples show how to use org.apache.solr.common.SolrInputDocument#addChildDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FullSolrCloudDistribCmdsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Recursive helper function for building out child and grandchild docs
 */
private long addChildren(String prefix, SolrInputDocument topDocument, int childIndex, boolean lastLevel, long docId) {
  SolrInputDocument childDocument = new SolrInputDocument();
  childDocument.addField("id", docId++);
  childDocument.addField("type_s", prefix);
  for (int index = 0; index < childIndex; ++index) {
    childDocument.addField(childIndex + prefix + index + "_s", childIndex + "value"+ index);
  }   

  if (!lastLevel) {
    for (int i = 0; i < childIndex * 2; ++i) {
      docId = addChildren("grand", childDocument, i, true, docId);
    }
  }
  topDocument.addChildDocument(childDocument);
  return docId;
}
 
Example 2
Source File: MCRSolrInputDocumentGenerator.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public static SolrInputDocument getSolrInputDocument(MCRSolrInputDocument jaxbDoc) {
    SolrInputDocument doc = new SolrInputDocument();
    HashSet<MCRSolrInputField> duplicateFilter = new HashSet<>();
    for (Object o : jaxbDoc.getFieldOrDoc()) {
        if (o instanceof MCRSolrInputField) {
            MCRSolrInputField field = (MCRSolrInputField) o;
            if (field.getValue().isEmpty() || duplicateFilter.contains(field)) {
                continue;
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("adding {}={}", field.getName(), field.getValue());
            }
            duplicateFilter.add(field);
            doc.addField(field.getName(), field.getValue());
        } else if (o instanceof MCRSolrInputDocument) {
            MCRSolrInputDocument child = (MCRSolrInputDocument) o;
            SolrInputDocument solrChild = getSolrInputDocument(child);
            doc.addChildDocument(solrChild);
        }
    }
    return doc;
}
 
Example 3
Source File: MCRSolrInputDocumentGenerator.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
public static SolrInputDocument getSolrInputDocument(Element input) {
    SolrInputDocument doc = new SolrInputDocument();
    HashSet<MCRSolrInputField> duplicateFilter = new HashSet<>();
    List<Element> fieldElements = input.getChildren("field");
    for (Element fieldElement : fieldElements) {
        MCRSolrInputField field = new MCRSolrInputField();
        field.setName(fieldElement.getAttributeValue("name"));
        field.setValue(fieldElement.getText());
        if (field.getValue().isEmpty() || duplicateFilter.contains(field)) {
            continue;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("adding {}={}", field.getName(), field.getValue());
        }
        duplicateFilter.add(field);
        doc.addField(field.getName(), field.getValue());
    }
    List<Element> docElements = input.getChildren("doc");
    for (Element child : docElements) {
        SolrInputDocument solrChild = getSolrInputDocument(child);
        doc.addChildDocument(solrChild);
    }
    return doc;
}
 
Example 4
Source File: SolrUtilities.java    From metron with Apache License 2.0 6 votes vote down vote up
public static SolrInputDocument toSolrInputDocument(Document document) {
  SolrInputDocument solrInputDocument = new SolrInputDocument();
  for (Map.Entry<String, Object> field : document.getDocument().entrySet()) {
    if (field.getKey().equals(MetaAlertConstants.ALERT_FIELD)) {
      // We have a children, that needs to be translated as a child doc, not a field.
      List<Map<String, Object>> alerts = (List<Map<String, Object>>) field.getValue();
      for (Map<String, Object> alert : alerts) {
        SolrInputDocument childDocument = new SolrInputDocument();
        for (Map.Entry<String, Object> alertField : alert.entrySet()) {
          childDocument.addField(alertField.getKey(), alertField.getValue());
        }
        solrInputDocument.addChildDocument(childDocument);
      }
    } else {
      solrInputDocument.addField(field.getKey(), field.getValue());
    }
  }
  return solrInputDocument;
}
 
Example 5
Source File: RootFieldTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateWithChildDocs() throws Exception {
  SolrClient client = getSolrClient();
  client.deleteByQuery("*:*");// delete everything!

  // Add child free doc
  SolrInputDocument docToUpdate = new SolrInputDocument();
  String docId = "11";
  docToUpdate.addField( "id", docId);
  docToUpdate.addField( "name", "parent doc with a child" );
  SolrInputDocument child = new SolrInputDocument();
  child.addField("id", "111");
  child.addField("name", "child doc");
  docToUpdate.addChildDocument(child);
  if (!useRootSchema) {
    thrown.expect(SolrException.class);
    thrown.expectMessage("Unable to index docs with children:" +
        " the schema must include definitions for both a uniqueKey field" +
        " and the '_root_' field, using the exact same fieldType");
  }
  client.add(docToUpdate);
  client.commit();
}
 
Example 6
Source File: TestChildDocTransformer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void createSimpleIndex() {

    SolrInputDocument parentDocument = new SolrInputDocument();
    parentDocument.addField(ID_FIELD, "1");
    parentDocument.addField("subject", "parentDocument");
    for(int i=0; i< 6; i++) {
      SolrInputDocument childDocument = new SolrInputDocument();
      childDocument.addField(ID_FIELD, Integer.toString(i+2));
      if(i%2==0) {
        childDocument.addField("title", "foo");
      } else {
        childDocument.addField("title", "bar");
      }

      parentDocument.addChildDocument(childDocument);
    }
    try {
      Long version = addAndGetVersion(parentDocument, null);
      assertNotNull(version);
    } catch (Exception e) {
      fail("Failed to add document to the index");
    }
    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='" + 7 + "']");
  }
 
Example 7
Source File: SolrUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
public static SolrInputDocument toSolrInputDocument(SolrDocument solrDocument) {
    SolrInputDocument solrInputDocument = new SolrInputDocument();

    for (String name : solrDocument.getFieldNames()) {
        solrInputDocument.addField(name, solrDocument.getFieldValue(name));
    }

    //Don't forget children documents
    if(solrDocument.getChildDocuments() != null) {
        for(SolrDocument childDocument : solrDocument.getChildDocuments()) {
            //You can add paranoic check against infinite loop childDocument == solrDocument
            solrInputDocument.addChildDocument(toSolrInputDocument(childDocument));
        }
    }
    return solrInputDocument;
}
 
Example 8
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrInputDocument sdocWithChildren(Integer id, String version, int childCount) {
  SolrInputDocument doc = sdoc("id", id, "_version_", version);
  for (int i = 1; i <= childCount; i++) {
    doc.addChildDocument(sdoc("id", (1000)*id + i));
  }
  return doc;
}
 
Example 9
Source File: RowMutationHelperTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void docsMustUseTheNormalBlurFamilyColumnFormat() {
  SolrInputDocument parent = new SolrInputDocument();
  parent.addField("columnWithoutFamily", "123");
  SolrInputDocument child = new SolrInputDocument();
  parent.addChildDocument(child);

  RowMutationHelper.from(parent, "foo");
}
 
Example 10
Source File: AddBlockUpdateTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void addChildren(String prefix, SolrInputDocument topDocument, int childIndex, boolean lastLevel) {
  SolrInputDocument childDocument = new SolrInputDocument();
  for (int index = 0; index < childIndex; ++index) {
    childDocument.addField(childIndex + prefix + index, childIndex + "value"+ index);
  }

  if (!lastLevel) {
    for (int i = 0; i < childIndex * 2; ++i) {
      addChildren("grand", childDocument, i, true);
    }
  }
  topDocument.addChildDocument(childDocument);
}
 
Example 11
Source File: TestChildDocTransformer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void createIndex(String[] titleVals) {

    String[] parentIDS = new String[] {"1", "4"};
    String[] childDocIDS = new String[] {"2", "5"};
    String[] grandChildIDS = new String[] {"3", "6"};

    for(int i=0; i< parentIDS.length; i++) {
      SolrInputDocument parentDocument = new SolrInputDocument();
      parentDocument.addField(ID_FIELD, parentIDS[i]);
      parentDocument.addField("subject", "parentDocument");
      parentDocument.addField("title", titleVals[i]);

      SolrInputDocument childDocument = new SolrInputDocument();
      childDocument.addField(ID_FIELD, childDocIDS[i]);
      childDocument.addField("cat", "childDocument");
      childDocument.addField("title", titleVals[i]);

      SolrInputDocument grandChildDocument = new SolrInputDocument();
      grandChildDocument.addField(ID_FIELD, grandChildIDS[i]);

      childDocument.addChildDocument(grandChildDocument);
      parentDocument.addChildDocument(childDocument);

      try {
        Long version = addAndGetVersion(parentDocument, null);
        assertNotNull(version);
      } catch (Exception e) {
        fail("Failed to add document to the index");
      }
      if (random().nextBoolean()) {
        assertU(commit());
      }
    }

    assertU(commit());
    assertQ(req("q", "*:*"), "//*[@numFound='" + (parentIDS.length + childDocIDS.length + grandChildIDS.length) + "']");

  }
 
Example 12
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public SolrInputDocument sdocWithChildren(String id, String version, int childCount) {
  SolrInputDocument doc = sdoc("id", id, "_version_", version);
  for (int i = 1; i <= childCount; i++) {
    doc.addChildDocument(sdoc("id", id + "_child" + i));
  }
  return doc;
}
 
Example 13
Source File: RowMutationHelperTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void docWithChildrenCantItselfHaveFieldValues() {
  SolrInputDocument parent = new SolrInputDocument();
  parent.addField("id", "1");
  parent.addField("fam.key1", "123");
  SolrInputDocument child = new SolrInputDocument();
  parent.addChildDocument(child);

  RowMutationHelper.from(parent, "foo");
}
 
Example 14
Source File: SolrLookingBlurServerTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test
public void docShouldBeDiscoverableWithMultiValuedFields() throws SolrServerException, IOException, BlurException,
    TException {
  String table = "docShouldBeDiscoverableWithMultiValuedFields";
  createTable(table);
  SolrServer server = new SolrLookingBlurServer(miniCluster.getControllerConnectionStr(), table);
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("rowid", "1");

  SolrInputDocument child = new SolrInputDocument();
  child.addField("recordid", "1");
  child.addField("fam.value", "123");
  child.addField("fam.value", "124");

  doc.addChildDocument(child);

  server.add(doc);

  assertTotalResults(table, "fam.value:123", 1l);
  assertTotalResults(table, "fam.value:124", 1l);
  assertTotalResults(table, "fam.value:justincase", 0l);

  removeTable(table);
}
 
Example 15
Source File: LoadSolrBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
private SolrInputDocument convert(Record record) {
  Map<String, Collection<Object>> map = record.getFields().asMap();
  SolrInputDocument doc = new SolrInputDocument(new HashMap(2 * map.size()));
  for (Map.Entry<String, Collection<Object>> entry : map.entrySet()) {
    String key = entry.getKey();
    if (LOAD_SOLR_CHILD_DOCUMENTS.equals(key)) {
      for (Object value : entry.getValue()) {
        if (value instanceof Record) {
          value = convert((Record) value); // recurse
        }
        if (value instanceof SolrInputDocument) {
          doc.addChildDocument((SolrInputDocument) value);
        } else {
          throw new MorphlineRuntimeException("Child document must be of class " + 
            Record.class.getName() + " or " + SolrInputDocument.class.getName() + ": " + value);
        }
      }
    } else {
      Collection<Object> values = entry.getValue();
      if (values.size() == 1 && values.iterator().next() instanceof Map) {
        doc.setField(key, values.iterator().next(), getBoost(key)); // it is an atomic update
      } else {
        doc.setField(key, values, getBoost(key));
      }
    }
  }      
  return doc;
}
 
Example 16
Source File: TestInPlaceUpdatesStandalone.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdatingFieldNotPresentInDoc() throws Exception {
  long version1 = addAndGetVersion(sdoc("id", "1", "title_s", "first"), null);
  long version2 = addAndGetVersion(sdoc("id", "2", "title_s", "second"), null);
  long version3 = addAndGetVersion(sdoc("id", "3", "title_s", "third"), null);
  assertU(commit("softCommit", "false"));
  assertQ(req("q", "*:*"), "//*[@numFound='3']");

  // subsequent updates shouldn't cause docid changes
  int docid1 = getDocId("1");
  int docid2 = getDocId("2");
  int docid3 = getDocId("3");

  // updating fields which are not present in the document
  // tests both set and inc with different fields
  version1 = addAndAssertVersion(version1, "id", "1", "inplace_updatable_float", map("set", 200));
  version2 = addAndAssertVersion(version2, "id", "2", "inplace_updatable_float", map("inc", 100));
  version3 = addAndAssertVersion(version3, "id", "3", "inplace_updatable_float", map("set", 300));
  version1 = addAndAssertVersion(version1, "id", "1", "inplace_updatable_int", map("set", 300));
  assertU(commit("softCommit", "false"));

  assertQ(req("q", "*:*", "sort", "id asc", "fl", "*,[docid]"),
      "//*[@numFound='3']",
      "//result/doc[1]/float[@name='inplace_updatable_float'][.='200.0']",
      "//result/doc[1]/int[@name='inplace_updatable_int'][.='300']",
      "//result/doc[2]/float[@name='inplace_updatable_float'][.='100.0']",
      "//result/doc[3]/float[@name='inplace_updatable_float'][.='300.0']",
      "//result/doc[1]/long[@name='_version_'][.='"+version1+"']",
      "//result/doc[2]/long[@name='_version_'][.='"+version2+"']",
      "//result/doc[3]/long[@name='_version_'][.='"+version3+"']",
      "//result/doc[1]/int[@name='[docid]'][.='"+docid1+"']",
      "//result/doc[2]/int[@name='[docid]'][.='"+docid2+"']",
      "//result/doc[3]/int[@name='[docid]'][.='"+docid3+"']"
  );

  // adding new field which is not present in any docs but matches dynamic field rule
  // and satisfies inplace condition should be treated as inplace update
  version1 = addAndAssertVersion(version1, "id", "1", "inplace_updatable_i_dvo", map("set", 200));
  assertU(commit("softCommit", "false"));
  assertQ(req("q", "id:1", "sort", "id asc", "fl", "*,[docid]"),
      "//*[@numFound='1']",
      "//result/doc[1]/float[@name='inplace_updatable_float'][.='200.0']",
      "//result/doc[1]/int[@name='inplace_updatable_int'][.='300']",
      "//result/doc[1]/int[@name='[docid]'][.='"+docid1+"']",
      "//result/doc[1]/int[@name='inplace_updatable_i_dvo'][.='200']"
      );

  // delete everything
  deleteAllAndCommit();

  // test for document with child documents
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "1");
  doc.setField("title_s", "parent");

  SolrInputDocument child1 = new SolrInputDocument();
  child1.setField("id", "1_1");
  child1.setField("title_s", "child1");
  SolrInputDocument child2 = new SolrInputDocument();
  child2.setField("id", "1_2");
  child2.setField("title_s", "child2");

  doc.addChildDocument(child1);
  doc.addChildDocument(child2);
  long version = addAndGetVersion(doc, null);
  assertU(commit("softCommit", "false"));
  assertQ(req("q", "*:*"), "//*[@numFound='3']");

  int parentDocId = getDocId("1");
  int childDocid1 = getDocId("1_1");
  int childDocid2 = getDocId("1_2");
  version = addAndAssertVersion(version, "id", "1", "inplace_updatable_float", map("set", 200));
  version = addAndAssertVersion(version, "id", "1", "inplace_updatable_int", map("inc", 300));
  assertU(commit("softCommit", "false"));

  // first child docs would be returned followed by parent doc
  assertQ(req("q", "*:*", "fl", "*,[docid]"),
      "//*[@numFound='3']",
      "//result/doc[3]/float[@name='inplace_updatable_float'][.='200.0']",
      "//result/doc[3]/int[@name='inplace_updatable_int'][.='300']",
      "//result/doc[3]/int[@name='[docid]'][.='"+parentDocId+"']",
      "//result/doc[1]/int[@name='[docid]'][.='"+childDocid1+"']",
      "//result/doc[2]/int[@name='[docid]'][.='"+childDocid2+"']"
  );
}
 
Example 17
Source File: SolrExampleStreamingBinaryTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryAndStreamResponse() throws Exception {
  // index a simple document with one child
  SolrClient client = getSolrClient();
  client.deleteByQuery("*:*");

  SolrInputDocument child = new SolrInputDocument();
  child.addField("id", "child");
  child.addField("type_s", "child");
  child.addField("text_s", "text");

  SolrInputDocument parent = new SolrInputDocument();
  parent.addField("id", "parent");
  parent.addField("type_s", "parent");
  parent.addChildDocument(child);

  client.add(parent);
  client.commit();

  // create a query with child doc transformer
  SolrQuery query = new SolrQuery("{!parent which='type_s:parent'}text_s:text");
  query.addField("*,[child parentFilter='type_s:parent']");

  // test regular query
  QueryResponse response = client.query(query);
  assertEquals(1, response.getResults().size());
  SolrDocument parentDoc = response.getResults().get(0);
  assertEquals(1, parentDoc.getChildDocumentCount());

  // test streaming
  final List<SolrDocument> docs = new ArrayList<>();
  client.queryAndStreamResponse(query, new StreamingResponseCallback() {
    @Override
    public void streamSolrDocument(SolrDocument doc) {
      docs.add(doc);
    }

    @Override
    public void streamDocListInfo(long numFound, long start, Float maxScore) {
    }
  });

  assertEquals(1, docs.size());
  parentDoc = docs.get(0);
  assertEquals(1, parentDoc.getChildDocumentCount());
}
 
Example 18
Source File: TestJsonFacets.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * An explicit test for unique*(_root_) across all methods
 */
public void testUniquesForMethod() throws Exception {
  final Client client = Client.localClient();

  final SolrParams p = params("rows","0");

  client.deleteByQuery("*:*", null);

  SolrInputDocument parent;
  parent = sdoc("id", "1", "type_s","book", "book_s","A", "v_t","q");
  client.add(parent, null);

  parent = sdoc("id", "2", "type_s","book", "book_s","B", "v_t","q w");
  parent.addChildDocument( sdoc("id","2.1", "type_s","page", "page_s","a", "v_t","x y z")  );
  parent.addChildDocument( sdoc("id","2.2", "type_s","page", "page_s","a", "v_t","x1   z")  );
  parent.addChildDocument( sdoc("id","2.3", "type_s","page", "page_s","a", "v_t","x2   z")  );
  parent.addChildDocument( sdoc("id","2.4", "type_s","page", "page_s","b", "v_t","x y  ") );
  parent.addChildDocument( sdoc("id","2.5", "type_s","page", "page_s","c", "v_t","  y z" )  );
  parent.addChildDocument( sdoc("id","2.6", "type_s","page", "page_s","c", "v_t","    z" )  );
  client.add(parent, null);

  parent = sdoc("id", "3", "type_s","book", "book_s","C", "v_t","q w e");
  parent.addChildDocument( sdoc("id","3.1", "type_s","page", "page_s","b", "v_t","x y  ") );
  parent.addChildDocument( sdoc("id","3.2", "type_s","page", "page_s","d", "v_t","x    ")  );
  parent.addChildDocument( sdoc("id","3.3", "type_s","page", "page_s","e", "v_t","  y  ")  );
  parent.addChildDocument( sdoc("id","3.4", "type_s","page", "page_s","f", "v_t","    z")  );
  client.add(parent, null);

  parent = sdoc("id", "4", "type_s","book", "book_s","D", "v_t","e");
  client.add(parent, null);

  client.commit();

  client.testJQ(params(p, "q", "type_s:page"
      , "json.facet", "{" +
          "  types: {" +
          "    type:terms," +
          "    field:type_s," +
          "    limit:-1," +
          "    facet: {" +
          "           in_books: \"unique(_root_)\"," +
          "           via_field:\"uniqueBlock(_root_)\","+
          "           via_query:\"uniqueBlock({!v=type_s:book})\" }"+
          "  }," +
          "  pages: {" +
          "    type:terms," +
          "    field:page_s," +
          "    limit:-1," +
          "    facet: {" +
          "           in_books: \"unique(_root_)\"," +
          "           via_field:\"uniqueBlock(_root_)\","+
          "           via_query:\"uniqueBlock({!v=type_s:book})\" }"+
          "  }" +
          "}" )

      , "response=={numFound:10,start:0,numFoundExact:true,docs:[]}"
      , "facets=={ count:10," +
          "types:{" +
          "    buckets:[ {val:page, count:10, in_books:2, via_field:2, via_query:2 } ]}" +
          "pages:{" +
          "    buckets:[ " +
          "     {val:a, count:3, in_books:1, via_field:1, via_query:1}," +
          "     {val:b, count:2, in_books:2, via_field:2, via_query:2}," +
          "     {val:c, count:2, in_books:1, via_field:1, via_query:1}," +
          "     {val:d, count:1, in_books:1, via_field:1, via_query:1}," +
          "     {val:e, count:1, in_books:1, via_field:1, via_query:1}," +
          "     {val:f, count:1, in_books:1, via_field:1, via_query:1}" +
          "    ]}" +
          "}"
  );
}
 
Example 19
Source File: TestJsonFacetsWithNestedObjects.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void indexDocs(final Client client) throws IOException, SolrServerException, Exception {
  client.deleteByQuery("*:*", null);

  SolrInputDocument book1 = sdoc(
      "id",         "book1",
      "type_s",     "book",
      "title_t",    "The Way of Kings",
      "author_s",   "Brandon Sanderson",
      "cat_s",      "fantasy",
      "pubyear_i",  "2010",
      "publisher_s","Tor");

  book1.addChildDocument(
      sdoc(
          "id", "book1_c1",
          "type_s", "review",
          "review_dt", "2015-01-03T14:30:00Z",
          "stars_i", "5",
          "author_s", "yonik",
          "comment_t", "A great start to what looks like an epic series!"));

  book1.addChildDocument(
      sdoc(
          "id", "book1_c2",
          "type_s", "review",
          "review_dt", "2014-03-15T12:00:00Z",
          "stars_i", "3",
          "author_s", "dan",
          "comment_t", "This book was too long."));
  client.add(book1, null);
  if (rarely()) {
    client.commit();
  }
  SolrInputDocument book2 = sdoc(
      "id",         "book2",
      "type_s",     "book",
      "title_t",    "Snow Crash",
      "author_s",   "Neal Stephenson",
      "cat_s",      "sci-fi",
      "pubyear_i",  "1992",
      "publisher_s","Bantam");

  book2.addChildDocument(
      sdoc(
          "id", "book2_c1",
          "type_s", "review",
          "review_dt", "2015-01-03T14:30:00Z",
          "stars_i", "5",
          "author_s", "yonik",
          "comment_t", "Ahead of its time... I wonder if it helped inspire The Matrix?"));

  book2.addChildDocument(
      sdoc(
          "id", "book2_c2",
          "type_s", "review",
          "review_dt", "2015-04-10T9:00:00Z",
          "stars_i", "2",
          "author_s", "dan",
          "comment_t", "A pizza boy for the Mafia franchise? Really?"));

  book2.addChildDocument(
      sdoc(
          "id", "book2_c3",
          "type_s", "review",
          "review_dt", "2015-06-02T00:00:00Z",
          "stars_i", "4",
          "author_s", "mary",
          "comment_t", "Neal is so creative and detailed! Loved the metaverse!"));

  client.add(book2, null);
  client.commit();
}
 
Example 20
Source File: SolrExampleStreamingBinaryHttp2Test.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryAndStreamResponse() throws Exception {
  // index a simple document with one child
  SolrClient client = getSolrClient();
  client.deleteByQuery("*:*");

  SolrInputDocument child = new SolrInputDocument();
  child.addField("id", "child");
  child.addField("type_s", "child");
  child.addField("text_s", "text");

  SolrInputDocument parent = new SolrInputDocument();
  parent.addField("id", "parent");
  parent.addField("type_s", "parent");
  parent.addChildDocument(child);

  client.add(parent);
  client.commit();

  // create a query with child doc transformer
  SolrQuery query = new SolrQuery("{!parent which='type_s:parent'}text_s:text");
  query.addField("*,[child parentFilter='type_s:parent']");

  // test regular query
  QueryResponse response = client.query(query);
  assertEquals(1, response.getResults().size());
  SolrDocument parentDoc = response.getResults().get(0);
  assertEquals(1, parentDoc.getChildDocumentCount());

  // test streaming
  final List<SolrDocument> docs = new ArrayList<>();
  client.queryAndStreamResponse(query, new StreamingResponseCallback() {
    @Override
    public void streamSolrDocument(SolrDocument doc) {
      docs.add(doc);
    }

    @Override
    public void streamDocListInfo(long numFound, long start, Float maxScore) {
    }
  });

  assertEquals(1, docs.size());
  parentDoc = docs.get(0);
  assertEquals(1, parentDoc.getChildDocumentCount());
}