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

The following examples show how to use org.apache.solr.common.SolrInputDocument#getFieldNames() . 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: Solr6Index.java    From atlas with Apache License 2.0 6 votes vote down vote up
private void commitChanges(String collectionName,
                           Collection<SolrInputDocument> documents) throws SolrServerException, IOException {
    if (documents.size() == 0) return;

    try {
        solrClient.request(newUpdateRequest().add(documents), collectionName);
    } catch (final HttpSolrClient.RemoteSolrException rse) {
        logger.error("Unable to save documents to Solr as one of the shape objects stored were not compatible with Solr.", rse);
        logger.error("Details in failed document batch: ");
        for (final SolrInputDocument d : documents) {
            final Collection<String> fieldNames = d.getFieldNames();
            for (final String name : fieldNames) {
                logger.error(name + ":" + d.getFieldValue(name));
            }
        }

        throw rse;
    }
}
 
Example 2
Source File: TestRandomFlRTGCloud.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Collection<String> assertRTGResults(final Collection<FlValidator> validators,
                                           final SolrInputDocument expected,
                                           final SolrDocument actual) {

  final Set<String> renamed = new LinkedHashSet<>(validators.size());
  for (FlValidator v : validators) {
    if (v instanceof SuppressRealFields) {
      renamed.addAll(((SuppressRealFields)v).getSuppressedFields());
    }
  }
  
  // every real field name matching the glob that is not renamed should be in the results
  Set<String> result = new LinkedHashSet<>(expected.getFieldNames().size());
  for (String f : expected.getFieldNames()) {
    if ( matchesGlob(f) && (! renamed.contains(f) ) ) {
      result.add(f);
      assertEquals(glob + " => " + f, expected.getFieldValue(f), actual.getFirstValue(f));
    }
  }
  return result;
}
 
Example 3
Source File: SolrIndex.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
private void commitDocumentChanges(String collectionName, Collection<SolrInputDocument> documents) throws SolrServerException, IOException {
    if (documents.size() == 0) return;

    try {
        solrClient.request(newUpdateRequest().add(documents), collectionName);
    } catch (HttpSolrClient.RemoteSolrException rse) {
        logger.error("Unable to save documents to Solr as one of the shape objects stored were not compatible with Solr.", rse);
        logger.error("Details in failed document batch: ");
        for (SolrInputDocument d : documents) {
            Collection<String> fieldNames = d.getFieldNames();
            for (String name : fieldNames) {
                logger.error(name + ":" + d.getFieldValue(name).toString());
            }
        }

        throw rse;
    }
}
 
Example 4
Source File: Solr5Index.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
private void commitDocumentChanges(String collectionName, Collection<SolrInputDocument> documents) throws SolrServerException, IOException {
    if (documents.size() == 0) return;

    try {
        solrClient.request(newUpdateRequest().add(documents), collectionName);
    } catch (HttpSolrClient.RemoteSolrException rse) {
        logger.error("Unable to save documents to Solr as one of the shape objects stored were not compatible with Solr.", rse);
        logger.error("Details in failed document batch: ");
        for (SolrInputDocument d : documents) {
            Collection<String> fieldNames = d.getFieldNames();
            for (String name : fieldNames) {
                logger.error(name + ":" + d.getFieldValue(name).toString());
            }
        }

        throw rse;
    }
}
 
Example 5
Source File: RowMutationHelper.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private static RecordMutation createRecordMutation(SolrInputDocument doc, String id) {
  RecordMutation recordMutation = new RecordMutation();
  // TODO: what's solr default behavior?
  recordMutation.setRecordMutationType(RecordMutationType.REPLACE_ENTIRE_RECORD);
  Record record = new Record();
  record.setFamily(findFamily(doc));
  record.setRecordId(id);

  for (String fieldName : doc.getFieldNames()) {
    if (!fieldName.contains(".")) {
      continue;
    }
    SolrInputField field = doc.getField(fieldName);
    String rawColumnName = fieldName.substring(fieldName.indexOf(".") + 1, fieldName.length());

    if (field.getValueCount() > 1) {
      for (Object fieldVal : field.getValues()) {
        record.addToColumns(new Column(rawColumnName, fieldVal.toString()));
      }
    } else {
      record.addToColumns(new Column(rawColumnName, field.getFirstValue().toString()));
    }
  }
  recordMutation.setRecord(record);
  return recordMutation;
}
 
Example 6
Source File: UpdateLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Add all fields from olderDoc into newerDoc if not already present in newerDoc
 */
private void applyOlderUpdates(@SuppressWarnings({"rawtypes"})SolrDocumentBase newerDoc, SolrInputDocument olderDoc, Set<String> mergeFields) {
  for (String fieldName : olderDoc.getFieldNames()) {
    // if the newerDoc has this field, then this field from olderDoc can be ignored
    if (!newerDoc.containsKey(fieldName) && (mergeFields == null || mergeFields.contains(fieldName))) {
      for (Object val : olderDoc.getFieldValues(fieldName)) {
        newerDoc.addField(fieldName, val);
      }
    }
  }
}
 
Example 7
Source File: FieldMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Calls <code>mutate</code> on any fields identified by the selector 
 * before forwarding the command down the chain.  Any SolrExceptions 
 * thrown by <code>mutate</code> will be logged with the Field name, 
 * wrapped and re-thrown.
 */
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  final SolrInputDocument doc = cmd.getSolrInputDocument();

  // make a copy we can iterate over while mutating the doc
  final Collection<String> fieldNames 
    = new ArrayList<>(doc.getFieldNames());

  for (final String fname : fieldNames) {

    if (! selector.shouldMutate(fname)) continue;
    
    final SolrInputField src = doc.get(fname);

    SolrInputField dest = null;
    try { 
      dest = mutate(src);
    } catch (SolrException e) {
      String msg = "Unable to mutate field '"+fname+"': "+e.getMessage();
      SolrException.log(log, msg, e);
      throw new SolrException(BAD_REQUEST, msg, e);
    }
    if (null == dest) {
      doc.remove(fname);
    } else {
      // semantics of what happens if dest has diff name are hard
      // we could treat it as a copy, or a rename
      // for now, don't allow it.
      if (! fname.equals(dest.getName()) ) {
        throw new SolrException(SERVER_ERROR,
                                "mutate returned field with different name: " 
                                + fname + " => " + dest.getName());
      }
      doc.put(dest.getName(), dest);
    }
  }
  super.processAdd(cmd);
}
 
Example 8
Source File: SolrLogPostToolTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testOtherRecord() throws Exception {
  final String record = "2020-06-11 11:59:08.386 INFO  (main) [   ] o.a.s.c.c.ZkStateReader Updated live nodes from ZooKeeper... (0) -> (2)";
  final List<SolrInputDocument> docs = readDocs(record);
  assertEquals(docs.size(), 1);

  SolrInputDocument doc = docs.get(0);
  final Collection<String> fields = doc.getFieldNames();
  assertEquals(3, fields.size());
  assertEquals("2020-06-11T11:59:08.386", doc.getField("date_dt").getValue());
  assertEquals("other", doc.getField("type_s").getValue());
  assertEquals(record, doc.getField("line_t").getValue());
}
 
Example 9
Source File: TestDocumentBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeepCopy() throws IOException {
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("field1", "value1");
  doc.addField("field2", "value1");
  doc.addField("field3", "value2");
  doc.addField("field4", 15);
  List<Integer> list = new ArrayList<>();
  list.add(45);
  list.add(33);
  list.add(20);
  doc.addField("field5", list);
  
  SolrInputDocument clone = doc.deepCopy();
  
  System.out.println("doc1: "+ doc);
  System.out.println("clone: "+ clone);
  
  assertNotSame(doc, clone);
  
  Collection<String> fieldNames = doc.getFieldNames();
  for (String name : fieldNames) {
    Collection<Object> values = doc.getFieldValues(name);
    Collection<Object> cloneValues = clone.getFieldValues(name);
    
    assertEquals(values.size(), cloneValues.size());
    assertNotSame(values, cloneValues);
    
    Iterator<Object> cloneIt = cloneValues.iterator();
    for (Object value : values) {
      Object cloneValue = cloneIt.next();
      assertSame(value, cloneValue);
    }
  }
}
 
Example 10
Source File: JsonDocumentMapperTest.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
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));
  }
}
 
Example 11
Source File: NestedDocumentMapperTest.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
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 12
Source File: MockSolrDocumentCollection.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Map<String,Object> convert(SolrInputDocument doc) {
    Map<String,Object> m = new HashMap<>();
    for (String f : doc.getFieldNames()) {
        Collection<Object> vs = doc.getFieldValues(f);
        m.put(f, vs);
    }
    return m;
}
 
Example 13
Source File: RowMutationHelper.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private static String findFamily(SolrInputDocument doc) {
  for (String name : doc.getFieldNames()) {
    if (name.contains(".")) {
      return name.substring(0, name.indexOf("."));
    }
  }
  throw new IllegalArgumentException("Unable to determine column family from document");
}
 
Example 14
Source File: RowMutationHelper.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private static void validateAsRow(SolrInputDocument doc) {
  Object rowid = doc.getFieldValue(BlurConstants.ROW_ID);

  if (rowid == null)
    throw new IllegalArgumentException("Document must have rowid field.");

  for (String field : doc.getFieldNames()) {
    if (!BlurConstants.ROW_ID.equals(field)) {
      throw new IllegalArgumentException("Parent documents act as rows and cant have fields.");
    }
  }

}
 
Example 15
Source File: AbstractSolrSentryTestBase.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * Function to validate the content of Solr response with that of input document.
 * @param solrInputDoc - Solr doc inserted into Solr
 * @param solrRespDocs - List of Solr doc obtained as response
 * (NOTE: This function ignores "_version_" field in validating Solr doc content)
 */
public void validateSolrDocContent(SolrInputDocument solrInputDoc,
                                   SolrDocumentList solrRespDocs) {
  for (SolrDocument solrRespDoc : solrRespDocs) {
    String expFieldValue = (String) solrInputDoc.getFieldValue("id");
    String resFieldValue = (String) solrRespDoc.getFieldValue("id");
    if (expFieldValue.equals(resFieldValue)) {
      int expectedRespFieldCount = solrRespDoc.size();
      if (solrRespDoc.containsKey("_version_")) {
        expectedRespFieldCount = expectedRespFieldCount - 1;
      }
      int expectedOrigFieldCount = solrInputDoc.size();
      if (solrInputDoc.containsKey("_version_")) {
        expectedOrigFieldCount = expectedOrigFieldCount - 1;
      }
      assertEquals("Expected " + expectedOrigFieldCount + " fields. But, found "
            + expectedRespFieldCount + " fields", expectedOrigFieldCount , expectedRespFieldCount);
      for (String field : solrInputDoc.getFieldNames()) {
        if (field.equals("_version_") == true) {
          continue;
        }

        expFieldValue = (String) solrInputDoc.getFieldValue(field);
        resFieldValue = (String) solrRespDoc.getFieldValue(field);
        assertEquals("Expected value for field: " + field + " is " + expFieldValue
            + "; But, found " + resFieldValue, expFieldValue, resFieldValue);
      }

      return;
    }
  }

  fail("Solr doc not found in Solr collection");
}
 
Example 16
Source File: SignatureUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
  if (enabled) {
    SolrInputDocument doc = cmd.getSolrInputDocument();
    List<String> currDocSigFields = null;
    boolean isPartialUpdate = AtomicUpdateDocumentMerger.isAtomicUpdate(cmd);
    if (sigFields == null || sigFields.size() == 0) {
      if (isPartialUpdate)  {
        throw new SolrException
            (ErrorCode.SERVER_ERROR,
                "Can't use SignatureUpdateProcessor with partial updates on signature fields");
      }
      Collection<String> docFields = doc.getFieldNames();
      currDocSigFields = new ArrayList<>(docFields.size());
      currDocSigFields.addAll(docFields);
      Collections.sort(currDocSigFields);
    } else {
      currDocSigFields = sigFields;
    }

    Signature sig = req.getCore().getResourceLoader().newInstance(signatureClass, Signature.class);
    sig.init(params);

    for (String field : currDocSigFields) {
      SolrInputField f = doc.getField(field);
      if (f != null) {
        if (isPartialUpdate)  {
          throw new SolrException
              (ErrorCode.SERVER_ERROR,
                  "Can't use SignatureUpdateProcessor with partial update request " +
                      "containing signature field: " + field);
        }
        sig.add(field);
        Object o = f.getValue();
        if (o instanceof Collection) {
          for (Object oo : (Collection)o) {
            sig.add(String.valueOf(oo));
          }
        } else {
          sig.add(String.valueOf(o));
        }
      }
    }

    byte[] signature = sig.getSignature();
    char[] arr = new char[signature.length<<1];
    for (int i=0; i<signature.length; i++) {
      int b = signature[i];
      int idx = i<<1;
      arr[idx]= StrUtils.HEX_DIGITS[(b >> 4) & 0xf];
      arr[idx+1]= StrUtils.HEX_DIGITS[b & 0xf];
    }
    String sigString = new String(arr);
    doc.addField(signatureField, sigString);

    if (overwriteDupes) {
      cmd.updateTerm = new Term(signatureField, sigString);
    }

  }

  if (next != null)
    next.processAdd(cmd);
}