Java Code Examples for org.apache.solr.common.SolrDocument#hasChildDocuments()

The following examples show how to use org.apache.solr.common.SolrDocument#hasChildDocuments() . 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: SolrUtilities.java    From metron with Apache License 2.0 6 votes vote down vote up
protected static void insertChildAlerts(SolrDocument solrDocument, Map<String, Object> document) {
  // Make sure to put child alerts in
  if (solrDocument.hasChildDocuments() && solrDocument
      .getFieldValue(Constants.SENSOR_TYPE)
      .equals(MetaAlertConstants.METAALERT_TYPE)) {
    List<Map<String, Object>> childDocuments = new ArrayList<>();
    for (SolrDocument childDoc : solrDocument.getChildDocuments()) {
      Map<String, Object> childDocMap = new HashMap<>();
      childDoc.getFieldNames().stream()
          .filter(name -> !name.equals(SolrDao.VERSION_FIELD))
          .forEach(name -> childDocMap.put(name, childDoc.getFieldValue(name)));
      childDocuments.add(childDocMap);
    }

    document.put(MetaAlertConstants.ALERT_FIELD, childDocuments);
  }
}
 
Example 2
Source File: XMLWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * The SolrDocument should already have multivalued fields implemented as
 * Collections -- this will not rewrite to &lt;arr&gt;
 */
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx ) throws IOException {
  startTag("doc", name, false);
  incLevel();

  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    Object val = doc.getFieldValue(fname);
    if( "_explain_".equals( fname ) ) {
      if (log.isDebugEnabled()) {
        log.debug(String.valueOf(val));
      }
    }
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    for(SolrDocument childDoc : doc.getChildDocuments()) {
      writeSolrDocument(null, childDoc, new SolrReturnFields(), idx);
    }
  }

  decLevel();
  writer.write("</doc>");
}
 
Example 3
Source File: SolrBoltActionTest.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
protected void doNestedDocTest() throws Exception {
  SolrBoltAction sba = new SolrBoltAction(cloudSolrClient);
  sba.setSolrInputDocumentMapper(new NestedDocumentMapper());
  sba.setUpdateRequestStrategy(new DefaultUpdateRequestStrategy());
  sba.setMaxBufferSize(1); // to avoid buffering docs

  String docId = "1";

  JSONArray jsonDocs = NestedDocumentMapperTest.loadNestedDocs();
  Tuple mockTuple = mock(Tuple.class);
  when(mockTuple.size()).thenReturn(2);
  when(mockTuple.getString(0)).thenReturn(docId);
  when(mockTuple.getValue(1)).thenReturn(jsonDocs.get(0));

  SpringBolt.ExecuteResult result = sba.execute(mockTuple, null);
  assertTrue(result == SpringBolt.ExecuteResult.ACK);
  cloudSolrClient.commit();

  // verify the object to Solr mapping worked correctly using reflection and dynamic fields
  SolrQuery query = new SolrQuery("id:" + docId);
  query.set("fl", "*,[child parentFilter=id:"+docId+" limit=100]");
  QueryResponse qr = cloudSolrClient.query(query);
  SolrDocumentList results = qr.getResults();
  assertTrue(results.getNumFound() == 1);
  SolrDocument doc = results.get(0);
  assertNotNull(doc);

  System.out.println("\n\n>> doc: "+doc+"\n\n");
  if (doc.hasChildDocuments()) {
    for (SolrDocument child : doc.getChildDocuments()) {
      System.out.println("\n>> "+child+"\n");
    }
  } else {
    System.out.println("no child documents returned");
  }

}
 
Example 4
Source File: JSONWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx) throws IOException {
  if( idx > 0 ) {
    writeArraySeparator();
  }

  indent();
  writeMapOpener(doc.size());
  incLevel();

  boolean first=true;
  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    if (first) {
      first=false;
    }
    else {
      writeMapSeparator();
    }

    indent();
    writeKey(fname, true);
    Object val = doc.getFieldValue(fname);
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    if(first == false) {
      writeMapSeparator();
      indent();
    }
    writeKey("_childDocuments_", true);
    writeArrayOpener(doc.getChildDocumentCount());
    List<SolrDocument> childDocs = doc.getChildDocuments();
    for(int i=0; i<childDocs.size(); i++) {
      writeSolrDocument(null, childDocs.get(i), null, i);
    }
    writeArrayCloser();
  }

  decLevel();
  writeMapCloser();
}
 
Example 5
Source File: GeoJSONResponseWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx) throws IOException {
  if( idx > 0 ) {
    writeArraySeparator();
  }

  indent();
  writeMapOpener(-1); 
  incLevel();

  writeKey("type", false);
  writeVal(null, "Feature");
  
  Object val = doc.getFieldValue(geofield);
  if(val != null) {  
    writeFeatureGeometry(val);
  }
  
  boolean first=true;
  for (String fname : doc.getFieldNames()) {
    if (fname.equals(geofield) || ((returnFields!= null && !returnFields.wantsField(fname)))) {
      continue;
    }
    writeMapSeparator();
    if (first) {
      indent();
      writeKey("properties", false);
      writeMapOpener(-1); 
      incLevel();
      
      first=false;
    }

    indent();
    writeKey(fname, true);
    val = doc.getFieldValue(fname);
    writeVal(fname, val);
  }

  // GeoJSON does not really support nested FeatureCollections
  if(doc.hasChildDocuments()) {
    if(first == false) {
      writeMapSeparator();
      indent();
    }
    writeKey("_childDocuments_", true);
    writeArrayOpener(doc.getChildDocumentCount());
    List<SolrDocument> childDocs = doc.getChildDocuments();
    for(int i=0; i<childDocs.size(); i++) {
      writeSolrDocument(null, childDocs.get(i), null, i);
    }
    writeArrayCloser();
  }

  // check that we added any properties
  if(!first) {
    decLevel();
    writeMapCloser();
  }
  
  decLevel();
  writeMapCloser();
}