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

The following examples show how to use org.apache.solr.common.SolrInputDocument#getChildDocuments() . These examples are extracted from open source projects. 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 Project: lucene-solr   File: SolrExampleTests.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * Depth first search of a SolrInputDocument looking for a decendent by id, 
 * returns null if it's not a decendent 
 */
private SolrInputDocument findDecendent(SolrInputDocument parent, String childId) {
  if (childId.equals(parent.getFieldValue("id"))) {
    return parent;
  }
  if (! parent.hasChildDocuments() ) {
    return null;
  }
  for (SolrInputDocument kid : parent.getChildDocuments()) {
    SolrInputDocument result = findDecendent(kid, childId);
    if (null != result) {
      return result;
    }
  }
  return null;
}
 
Example 2
public static RowMutation from(SolrInputDocument doc, String table) {
  validateAsRow(doc);

  RowMutation mutate = new RowMutation();
  String rowid = extractRowId(doc);
  mutate.setRowId(rowid);
  mutate.setTable(table);
  List<RecordMutation> recordMutations = Lists.newArrayList();

  if (doc.hasChildDocuments()) {
    for (SolrInputDocument child : doc.getChildDocuments()) {
      validateAsRecord(child);
      recordMutations.add(createRecordMutation(child, extractRecordId(child)));
    }

  }
  mutate.setRecordMutations(recordMutations);
  return mutate;
}
 
Example 3
Source Project: lucene-solr   File: JavaBinCodec.java    License: Apache License 2.0 5 votes vote down vote up
public void writeSolrInputDocument(SolrInputDocument sdoc) throws IOException {
  List<SolrInputDocument> children = sdoc.getChildDocuments();
  int sz = sdoc.size() + (children==null ? 0 : children.size());
  writeTag(SOLRINPUTDOC, sz);
  writeFloat(1f); // document boost
  sdoc.writeMap(new ConditionalKeyMapWriter.EntryWriterWrapper(ew,IGNORECHILDDOCS));
  if (children != null) {
    for (SolrInputDocument child : children) {
      writeSolrInputDocument(child);
    }
  }
}
 
Example 4
private static SolrDocument toSolrDocument(SolrInputDocument d) {
  SolrDocument doc = new SolrDocument();
  for (SolrInputField field : d) {
    doc.setField(field.getName(), field.getValue());
  }
  if (d.getChildDocuments() != null) {
    for (SolrInputDocument in : d.getChildDocuments()) {
      doc.addChildDocument(toSolrDocument(in));
    }
  }
  return doc;
}
 
Example 5
Source Project: lucene-solr   File: AddUpdateCommand.java    License: Apache License 2.0 5 votes vote down vote up
/** Extract all anonymous child documents from parent. */
private void flattenAnonymous(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc, boolean isRoot) {
  List<SolrInputDocument> children = currentDoc.getChildDocuments();
  if (children != null) {
    for (SolrInputDocument child : children) {
      flattenAnonymous(unwrappedDocs, child);
    }
  }

  if(!isRoot) unwrappedDocs.add(currentDoc);
}
 
Example 6
static long estimate(SolrInputDocument doc) {
  if (doc == null) return 0L;
  long size = 0;
  for (SolrInputField inputField : doc.values()) {
    size += primitiveEstimate(inputField.getName(), 0L);
    size += estimate(inputField.getValue());
  }

  if (doc.hasChildDocuments()) {
    for (SolrInputDocument childDoc : doc.getChildDocuments()) {
      size += estimate(childDoc);
    }
  }
  return size;
}
 
Example 7
Source Project: lucene-solr   File: JsonLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyAnonymousChildDocs() 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();
}
 
Example 8
protected void debugDoc(PrintStream out, SolrInputDocument doc, int depth) {
  Collection<String> fieldNames = doc.getFieldNames();
  Iterator<String> fieldNameIter = fieldNames.iterator();
  String tabs = tabs(depth);
  while (fieldNameIter.hasNext()) {
    String fieldName = fieldNameIter.next();
    if ("id".equals(fieldName))
      continue;

    out.print(tabs);
    out.println(doc.get(fieldName));
  }

  List<SolrInputDocument> childDocs = doc.getChildDocuments();
  if (childDocs != null && !childDocs.isEmpty()) {
    out.print(tabs);
    out.println("_childDocuments_: [");
    int childDepth = depth+1;
    String childTabs = tabs(depth+1);
    for (SolrInputDocument child : childDocs) {
      out.print(childTabs);
      out.println(child.getFieldValue("id") + " : {");
      debugDoc(out, child, childDepth+1);
      out.print(childTabs);
      out.println("}");
    }
    out.print(tabs);
    out.println("]");
  }
}
 
Example 9
Source Project: lucene-solr   File: ClientUtils.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException
{
  writer.write("<doc>");

  for( SolrInputField field : doc ) {
    String name = field.getName();

    for( Object v : field ) {
      String update = null;

      if(v instanceof SolrInputDocument) {
        writeVal(writer, name, v , null);
      } else if (v instanceof Map) {
        // currently only supports a single value
        for (Entry<Object,Object> entry : ((Map<Object,Object>)v).entrySet()) {
          update = entry.getKey().toString();
          v = entry.getValue();
          if (v instanceof Collection) {
            @SuppressWarnings({"rawtypes"})
            Collection values = (Collection) v;
            for (Object value : values) {
              writeVal(writer, name, value, update);
            }
          } else  {
            writeVal(writer, name, v, update);
          }
        }
      } else  {
        writeVal(writer, name, v, update);
      }
    }
  }

  if (doc.hasChildDocuments()) {
    for (SolrInputDocument childDocument : doc.getChildDocuments()) {
      writeXML(childDocument, writer);
    }
  }
  
  writer.write("</doc>");
}
 
Example 10
Source Project: lucene-solr   File: SolrTestCaseJ4.java    License: Apache License 2.0 4 votes vote down vote up
public boolean compareSolrInputDocument(Object expected, Object actual) {

    if (!(expected instanceof SolrInputDocument) || !(actual instanceof SolrInputDocument)) {
      return false;
    }

    if (expected == actual) {
      return true;
    }

    SolrInputDocument sdoc1 = (SolrInputDocument) expected;
    SolrInputDocument sdoc2 = (SolrInputDocument) actual;

    if(sdoc1.getFieldNames().size() != sdoc2.getFieldNames().size()) {
      return false;
    }

    Iterator<String> iter1 = sdoc1.getFieldNames().iterator();
    Iterator<String> iter2 = sdoc2.getFieldNames().iterator();

    while (iter1.hasNext()) {
      String key1 = iter1.next();
      String key2 = iter2.next();

      Object val1 = sdoc1.getFieldValues(key1);
      Object val2 = sdoc2.getFieldValues(key2);

      if(!key1.equals(key2)) {
        return false;
      }

      if(!(sdoc1.get(key1).getFirstValue() instanceof SolrInputDocument)) {
        if(!val1.equals(val2)) {
          return false;
        }
      } else {
        if (!(sdoc2.get(key2).getFirstValue() instanceof SolrInputDocument)) {
          return false;
        }
        @SuppressWarnings({"rawtypes"})
        Collection col1 = (Collection) val1;
        @SuppressWarnings({"rawtypes"})
        Collection col2 = (Collection) val2;
        if (col1.size() != col2.size()) {
          return false;
        }
        @SuppressWarnings({"unchecked"})
        Iterator<SolrInputDocument> colIter1 = col1.iterator();
        @SuppressWarnings({"unchecked"})
        Iterator<SolrInputDocument> colIter2 = col2.iterator();
        while (colIter1.hasNext()) {
          if (!compareSolrInputDocument(colIter1.next(), colIter2.next())) {
            return false;
          }
        }
      }
    }
    if(sdoc1.getChildDocuments() == null && sdoc2.getChildDocuments() == null) {
      return true;
    }
    if(sdoc1.getChildDocuments() == null || sdoc2.getChildDocuments() == null) {
      return false;
    } else if(sdoc1.getChildDocuments().size() != sdoc2.getChildDocuments().size()) {
      return false;
    } else {
      Iterator<SolrInputDocument> childDocsIter1 = sdoc1.getChildDocuments().iterator();
      Iterator<SolrInputDocument> childDocsIter2 = sdoc2.getChildDocuments().iterator();
      while(childDocsIter1.hasNext()) {
        if(!compareSolrInputDocument(childDocsIter1.next(), childDocsIter2.next())) {
          return false;
        }
      }
      return true;
    }
  }