org.apache.solr.common.SolrInputField Java Examples

The following examples show how to use org.apache.solr.common.SolrInputField. 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: IdAddingSolrUpdateWriter.java    From hbase-indexer with Apache License 2.0 6 votes vote down vote up
/**
 * Add a SolrInputDocument to this writer.
 * <p>
 * Adding multiple documents without ids will result in an IllegalStateException being thrown.
 */
@Override
public void add(SolrInputDocument solrDocument) {
    String docId = documentId;
    SolrInputField uniqueKeySolrField = solrDocument.getField(uniqueKeyField);
    if (uniqueKeySolrField == null) {
        if (idUsed) {
            throw new IllegalStateException("Document id '" + documentId + "' has already been used by this record");
        }
        solrDocument.addField(uniqueKeyField, documentId);
        idUsed = true;
    } else {
        docId = uniqueKeySolrField.getValue().toString();
    }
    
    if (tableNameField != null) {
        solrDocument.addField(tableNameField, tableName);
    }
    
    updateCollector.add(docId, solrDocument);
}
 
Example #2
Source File: DefaultResultToSolrMapperTest.java    From hbase-indexer with Apache License 2.0 6 votes vote down vote up
@Test
public void testMap() {
    FieldDefinition fieldDefA = new FieldDefinition("fieldA", "cfA:qualifierA", ValueSource.VALUE, "int");
    FieldDefinition fieldDefB = new FieldDefinition("fieldB", "cfB:qualifierB", ValueSource.VALUE,
            DummyValueMapper.class.getName());
    DefaultResultToSolrMapper resultMapper = new DefaultResultToSolrMapper("index-name", Lists.newArrayList(fieldDefA, fieldDefB),
            Collections.<DocumentExtractDefinition> emptyList());

    KeyValue kvA = new KeyValue(ROW, COLUMN_FAMILY_A, QUALIFIER_A, Bytes.toBytes(42));
    KeyValue kvB = new KeyValue(ROW, COLUMN_FAMILY_B, QUALIFIER_B, Bytes.toBytes("dummy value"));
    Result result = Result.create(Lists.<Cell>newArrayList(kvA, kvB));

    resultMapper.map(result, solrUpdateWriter);
    verify(solrUpdateWriter).add(solrInputDocCaptor.capture());
    
    SolrInputDocument solrDocument = solrInputDocCaptor.getValue();

    assertEquals(Sets.newHashSet("fieldA", "fieldB"), solrDocument.keySet());

    SolrInputField fieldA = solrDocument.get("fieldA");
    SolrInputField fieldB = solrDocument.get("fieldB");

    assertEquals(Lists.newArrayList(42), fieldA.getValues());
    assertEquals(Lists.newArrayList("A", "B", "C"), fieldB.getValues());
}
 
Example #3
Source File: PreAnalyzedUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected SolrInputField mutate(SolrInputField src) {
  SchemaField sf = schema.getFieldOrNull(src.getName());
  if (sf == null) { // remove this field
    return null;
  }
  FieldType type = PreAnalyzedField.createFieldType(sf);
  if (type == null) { // neither indexed nor stored - skip
    return null;
  }
  SolrInputField res = new SolrInputField(src.getName());
  for (Object o : src) {
    if (o == null) {
      continue;
    }
    Field pre = (Field)parser.createField(sf, o);
    if (pre != null) {
      res.addValue(pre);
    } else { // restore the original value
      log.warn("Could not parse field {} - using original value as is: {}", src.getName(), o);
      res.addValue(o);
    }
  }
  return res;
}
 
Example #4
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public boolean assertSolrInputFieldEquals(Object expected, Object actual) {
  if (!(expected instanceof SolrInputField) || !(actual instanceof  SolrInputField)) {
    return false;
  }

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

  SolrInputField sif1 = (SolrInputField) expected;
  SolrInputField sif2 = (SolrInputField) actual;

  if (!sif1.getName().equals(sif2.getName())) {
    return false;
  }

  if (!sif1.getValue().equals(sif2.getValue())) {
    return false;
  }

  return true;
}
 
Example #5
Source File: TestJavaBinCodec.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void compareObjects(@SuppressWarnings({"rawtypes"})List unmarshaledObj,
                            @SuppressWarnings({"rawtypes"})List matchObj) {
  assertEquals(unmarshaledObj.size(), matchObj.size());
  for (int i = 0; i < unmarshaledObj.size(); i++) {

    if (unmarshaledObj.get(i) instanceof byte[] && matchObj.get(i) instanceof byte[]) {
      byte[] b1 = (byte[]) unmarshaledObj.get(i);
      byte[] b2 = (byte[]) matchObj.get(i);
      assertTrue(Arrays.equals(b1, b2));
    } else if (unmarshaledObj.get(i) instanceof SolrDocument && matchObj.get(i) instanceof SolrDocument) {
      assertTrue(compareSolrDocument(unmarshaledObj.get(i), matchObj.get(i)));
    } else if (unmarshaledObj.get(i) instanceof SolrDocumentList && matchObj.get(i) instanceof SolrDocumentList) {
      assertTrue(compareSolrDocumentList(unmarshaledObj.get(i), matchObj.get(i)));
    } else if (unmarshaledObj.get(i) instanceof SolrInputDocument && matchObj.get(i) instanceof SolrInputDocument) {
      assertTrue(compareSolrInputDocument(unmarshaledObj.get(i), matchObj.get(i)));
    } else if (unmarshaledObj.get(i) instanceof SolrInputField && matchObj.get(i) instanceof SolrInputField) {
      assertTrue(assertSolrInputFieldEquals(unmarshaledObj.get(i), matchObj.get(i)));
    } else {
      assertEquals(unmarshaledObj.get(i), matchObj.get(i));
    }

  }
}
 
Example #6
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Returns the indexed ID for this document.  The returned BytesRef is retained across multiple calls, and should not be modified. */
public BytesRef getIndexedId() {
  if (indexedId == null) {
    IndexSchema schema = req.getSchema();
    SchemaField sf = schema.getUniqueKeyField();
    if (sf != null) {
      if (solrDoc != null) {
        SolrInputField field = solrDoc.getField(sf.getName());

        int count = field==null ? 0 : field.getValueCount();
        if (count == 0) {
          if (overwrite) {
            throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document is missing mandatory uniqueKey field: " + sf.getName());
          }
        } else if (count  > 1) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Document contains multiple values for uniqueKey field: " + field);
        } else {
          BytesRefBuilder b = new BytesRefBuilder();
          sf.getType().readableToIndexed(field.getFirstValue().toString(), b);
          indexedId = b.get();
        }
      }
    }
  }
  return indexedId;
}
 
Example #7
Source File: AddSchemaFieldsUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Recursively find unknown fields in the given doc and its child documents, if any.
 */
private void getUnknownFields
(FieldNameSelector selector, SolrInputDocument doc, Map<String,List<SolrInputField>> unknownFields) {
  for (final String fieldName : doc.getFieldNames()) {
    //We do a assert and a null check because even after SOLR-12710 is addressed
    //older SolrJ versions can send null values causing an NPE
    assert fieldName != null;
    if (fieldName != null) {
      if (selector.shouldMutate(fieldName)) { // returns false if the field already exists in the current schema
        List<SolrInputField> solrInputFields = unknownFields.get(fieldName);
        if (null == solrInputFields) {
          solrInputFields = new ArrayList<>();
          unknownFields.put(fieldName, solrInputFields);
        }
        solrInputFields.add(doc.getField(fieldName));
      }
    }
  }
  List<SolrInputDocument> childDocs = doc.getChildDocuments();
  if (null != childDocs) {
    for (SolrInputDocument childDoc : childDocs) {
      getUnknownFields(selector, childDoc, unknownFields);
    }
  }
}
 
Example #8
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * @return String id to hash
 */
public String getHashableId() {
  IndexSchema schema = req.getSchema();
  SchemaField sf = schema.getUniqueKeyField();
  if (sf != null) {
    if (solrDoc != null) {
      SolrInputField field = solrDoc.getField(sf.getName());

      int count = field == null ? 0 : field.getValueCount();
      if (count == 0) {
        if (overwrite) {
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
              "Document is missing mandatory uniqueKey field: "
                  + sf.getName());
        }
      } else if (count > 1) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Document contains multiple values for uniqueKey field: " + field);
      } else {
        return field.getFirstValue().toString();
      }
    }
  }
  return null;
}
 
Example #9
Source File: SolrLogPostToolTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommit() throws Exception{
  String record = "2019-12-16T14:20:19.708 INFO  (qtp812143047-22671) [c:production_201912 s:shard128 r:core_node7 x:production_201912_shard128_replica] o.a.s.u.DirectUpdateHandler2 start commit{_version_=1653086376121335808,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false}\n";
  List<SolrInputDocument> docs = readDocs(record);
  assertEquals(docs.size(), 1);
  SolrInputDocument doc = docs.get(0);

  SolrInputField date = doc.getField("date_dt");
  SolrInputField type = doc.getField("type_s");
  SolrInputField shard = doc.getField("shard_s");
  SolrInputField replica = doc.getField("replica_s");
  SolrInputField core = doc.getField("core_s");
  SolrInputField openSearcher = doc.getField("open_searcher_s");
  SolrInputField softCommit = doc.getField("soft_commit_s");
  SolrInputField collection = doc.getField("collection_s");

  assertEquals(date.getValue(), "2019-12-16T14:20:19.708");
  assertEquals(type.getValue(), "commit");
  assertEquals(shard.getValue(), "shard128");
  assertEquals(replica.getValue(), "core_node7");
  assertEquals(core.getValue(), "production_201912_shard128_replica");
  assertEquals(openSearcher.getValue(), "true");
  assertEquals(softCommit.getValue(), "false");
  assertEquals(collection.getValue(), "production_201912");
}
 
Example #10
Source File: AddUpdateCommand.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Extract all child documents from parent that are saved in fields */
private void flattenLabelled(List<SolrInputDocument> unwrappedDocs, SolrInputDocument currentDoc, boolean isRoot) {
  for (SolrInputField field: currentDoc.values()) {
    Object value = field.getFirstValue();
    // check if value is a childDocument
    if (value instanceof SolrInputDocument) {
      Object val = field.getValue();
      if (!(val instanceof Collection)) {
        flattenLabelled(unwrappedDocs, ((SolrInputDocument) val));
        continue;
      }
      @SuppressWarnings({"unchecked"})
      Collection<SolrInputDocument> childrenList = ((Collection) val);
      for (SolrInputDocument child : childrenList) {
        flattenLabelled(unwrappedDocs, child);
      }
    }
  }

  if (!isRoot) unwrappedDocs.add(currentDoc);
}
 
Example #11
Source File: SolrInputFieldEquivalence.java    From kafka-connect-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean doEquivalent(SolrInputField o1, SolrInputField o2) {
  if (o1.getValue() instanceof SolrInputDocument) {
    if (!(o2.getValue() instanceof SolrInputDocument)) {
      return false;
    }
    final MapDifference<String, SolrInputField> difference = Maps.difference(
        (SolrInputDocument) o1.getValue(),
        (SolrInputDocument) o2.getValue(),
        this
    );
    if (!difference.areEqual()) {
      return false;
    }
  } else {
    if (o1.getValue() != o2.getValue()) {
      return false;
    }
  }
  return true;
}
 
Example #12
Source File: MapDifferenceSupplier.java    From kafka-connect-solr with Apache License 2.0 6 votes vote down vote up
@Override
public String get() {
  StringBuilder builder = new StringBuilder();
  if (!difference.entriesDiffering().isEmpty()) {
    builder.append("Differing:\n");
    for (Map.Entry<String, MapDifference.ValueDifference<SolrInputField>> diff : difference.entriesDiffering().entrySet()) {
      builder.append("  ");
      builder.append(diff.getKey());
      builder.append('\n');
      builder.append("  left  : ");
      builder.append(diff.getValue().leftValue());
      builder.append('\n');
      builder.append("  right : ");
      builder.append(diff.getValue().rightValue());
      builder.append('\n');
    }
  }

  return builder.toString();
}
 
Example #13
Source File: FieldValueMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
protected final SolrInputField mutate(final SolrInputField src) {
  Collection<Object> values = src.getValues();
  if(values == null) return src;//don't mutate
  SolrInputField result = new SolrInputField(src.getName());
  for (final Object srcVal : values) {
    final Object destVal = mutateValue(srcVal);
    if (DELETE_VALUE_SINGLETON == destVal) { 
      /* NOOP */
      if (log.isDebugEnabled()) {
        log.debug("removing value from field '{}': {}",
            src.getName(), srcVal);
      }
    } else {
      if (destVal != srcVal) {
        if (log.isDebugEnabled()) {
          log.debug("replace value from field '{}': {} with {}",
              new Object[]{src.getName(), srcVal, destVal});
        }
      }
      result.addValue(destVal);
    }
  }
  return 0 == result.getValueCount() ? null : result;
}
 
Example #14
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void doRemoveRegex(SolrInputDocument toDoc, SolrInputField sif, Object valuePatterns) {
  final String name = sif.getName();
  final SolrInputField existingField = toDoc.get(name);
  if (existingField != null) {
    final Collection<Object> valueToRemove = new HashSet<>();
    final Collection<Object> original = existingField.getValues();
    final Collection<Pattern> patterns = preparePatterns(valuePatterns);
    for (Object value : original) {
      for(Pattern pattern : patterns) {
        final Matcher m = pattern.matcher(value.toString());
        if (m.matches()) {
          valueToRemove.add(value);
        }
      }
    }
    original.removeAll(valueToRemove);
    toDoc.setField(name, original);
  }
}
 
Example #15
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testBooleanValuesInAdd() throws Exception {
  String str = "{'add':[{'id':'1','b1':true,'b2':false,'b3':[false,true]}]}".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(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("b1");
  assertEquals(Boolean.TRUE, f.getValue());
  f = d.getField("b2");
  assertEquals(Boolean.FALSE, f.getValue());
  f = d.getField("b3");
  assertEquals(2, ((List)f.getValue()).size());
  assertEquals(Boolean.FALSE, ((List)f.getValue()).get(0));
  assertEquals(Boolean.TRUE, ((List)f.getValue()).get(1));

  req.close();
}
 
Example #16
Source File: AutocompleteUpdateRequestProcessor.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
private void addField(SolrInputDocument doc, String name, String value) {
  // find if such field already exists
  if (doc.get(name) == null) {
    doc.addField(name, value);
  } else {
    // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check
    SolrInputField f = doc.get(name);
    
    boolean valueExists = false;
    
    for (Object existingValue : f.getValues()) {
      if (existingValue == null && value == null) {
        valueExists = true;
        break;
      }
      if (existingValue != null && value != null && existingValue.equals(value)) {
        valueExists = true;
        break;
      }
    }
      
    if (!valueExists) {
      f.addValue(value);
    }
  }
}
 
Example #17
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param fullDoc the full doc to  be compared against
 * @param partialDoc the sub document to be tested
 * @return whether partialDoc is derived from fullDoc
 */
public static boolean isDerivedFromDoc(SolrInputDocument fullDoc, SolrInputDocument partialDoc) {
  for(SolrInputField subSif: partialDoc) {
    Collection<Object> fieldValues = fullDoc.getFieldValues(subSif.getName());
    if (fieldValues == null) return false;
    if (fieldValues.size() < subSif.getValueCount()) return false;
    Collection<Object> partialFieldValues = subSif.getValues();
    // filter all derived child docs from partial field values since they fail List#containsAll check (uses SolrInputDocument#equals which fails).
    // If a child doc exists in partialDoc but not in full doc, it will not be filtered, and therefore List#containsAll will return false
    Stream<Object> nonChildDocElements = partialFieldValues.stream().filter(x -> !(isChildDoc(x) &&
        (fieldValues.stream().anyMatch(y ->
            (isChildDoc(x) &&
                isDerivedFromDoc((SolrInputDocument) y, (SolrInputDocument) x)
            )
        )
        )));
    if (!nonChildDocElements.allMatch(fieldValues::contains)) return false;
  }
  return true;
}
 
Example #18
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalValuesInAdd() throws Exception {
  String str = "{'add':[{'id':'1','d1':256.78,'d2':-5123456789.0,'d3':0.0,'d3':1.0,'d4':1.7E-10}]}".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(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("d1");
  assertEquals(256.78, f.getValue());
  f = d.getField("d2");
  assertEquals(-5123456789.0, f.getValue());
  f = d.getField("d3");
  assertEquals(2, ((List)f.getValue()).size());
  assertTrue(((List)f.getValue()).contains(0.0));
  assertTrue(((List) f.getValue()).contains(1.0));
  f = d.getField("d4");
  assertEquals(1.7E-10, f.getValue());

  req.close();
}
 
Example #19
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected void doRemove(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  final String name = sif.getName();
  SolrInputField existingField = toDoc.get(name);
  if (existingField == null) return;
  @SuppressWarnings({"rawtypes"})
  final Collection original = existingField.getValues();
  if (fieldVal instanceof Collection) {
    for (Object object : (Collection) fieldVal) {
      removeObj(original, object, name);
    }
  } else {
    removeObj(original, fieldVal, name);
  }

  toDoc.setField(name, original);
}
 
Example #20
Source File: MappingSolrConverter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private Collection<SolrInputField> writeRegularPropertyToTarget(final Map<? super Object, ? super Object> target,
		SolrPersistentProperty persistentProperty, Object fieldValue) {

	SolrInputField field = new SolrInputField(persistentProperty.getFieldName());

	if (persistentProperty.isCollectionLike()) {
		Collection<?> collection = asCollection(fieldValue);
		for (Object o : collection) {
			if (o != null) {
				field.addValue(convertToSolrType(persistentProperty.getType(), o), 1f);
			}
		}
	} else {
		field.setValue(convertToSolrType(persistentProperty.getType(), fieldValue), 1f);
	}

	target.put(persistentProperty.getFieldName(), field);

	return Collections.singleton(field);

}
 
Example #21
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param updateSif the SolrInputField to update its values
 * @param cmdDocWChildren the doc to insert/set inside updateSif
 * @param updateDoc the document that was sent as part of the Add Update Command
 * @return updated SolrInputDocument
 */
@SuppressWarnings({"unchecked"})
public SolrInputDocument updateDocInSif(SolrInputField updateSif, SolrInputDocument cmdDocWChildren, SolrInputDocument updateDoc) {
  @SuppressWarnings({"rawtypes"})
  List sifToReplaceValues = (List) updateSif.getValues();
  final boolean wasList = updateSif.getValue() instanceof Collection;
  int index = getDocIndexFromCollection(cmdDocWChildren, sifToReplaceValues);
  SolrInputDocument updatedDoc = merge(updateDoc, cmdDocWChildren);
  if(index == -1) {
    sifToReplaceValues.add(updatedDoc);
  } else {
    sifToReplaceValues.set(index, updatedDoc);
  }
  // in the case where value was a List prior to the update and post update there is no more then one value
  // it should be kept as a List.
  final boolean singleVal = !wasList && sifToReplaceValues.size() <= 1;
  updateSif.setValue(singleVal? sifToReplaceValues.get(0): sifToReplaceValues);
  return cmdDocWChildren;
}
 
Example #22
Source File: DbToSolrMigrationUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
private void toSolrDocument(XXAccessAuditBase xXAccessAudit,  SolrInputDocument document) {
	// add v4 fields
	document.addField("id", xXAccessAudit.getId());
	document.addField("access", xXAccessAudit.getAccessType());
	document.addField("enforcer", xXAccessAudit.getAclEnforcer());
	document.addField("agent", xXAccessAudit.getAgentId());
	document.addField("repo", xXAccessAudit.getRepoName());
	document.addField("sess", xXAccessAudit.getSessionId());
	document.addField("reqUser", xXAccessAudit.getRequestUser());
	document.addField("reqData", xXAccessAudit.getRequestData());
	document.addField("resource", xXAccessAudit.getResourcePath());
	document.addField("cliIP", xXAccessAudit.getClientIP());
	document.addField("logType", "RangerAudit");
	document.addField("result", xXAccessAudit.getAccessResult());
	document.addField("policy", xXAccessAudit.getPolicyId());
	document.addField("repoType", xXAccessAudit.getRepoType());
	document.addField("resType", xXAccessAudit.getResourceType());
	document.addField("reason", xXAccessAudit.getResultReason());
	document.addField("action", xXAccessAudit.getAction());
	document.addField("evtTime", DateUtil.getLocalDateForUTCDate(xXAccessAudit.getEventTime()));
	SolrInputField idField = document.getField("id");
	boolean uidIsString = true;
	if( idField == null) {
		Object uid = null;
		if(uidIsString) {
			uid = UUID.randomUUID().toString();
		}
		document.setField("id", uid);
	}
}
 
Example #23
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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);
  assertTrue(add.overwrite);

  add = p.addCommands.get(1);
  d = add.solrDoc;
  f = d.getField( "id" );
  assertEquals("2", f.getValue());
  assertEquals(add.commitWithin, -1);
  assertTrue(add.overwrite);

  req.close();
}
 
Example #24
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSimpleFormat() throws Exception
{
  String str = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
  SolrQueryRequest req = req("commitWithin","100", "overwrite","false");
  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, 100);
  assertFalse(add.overwrite);

  add = p.addCommands.get(1);
  d = add.solrDoc;
  f = d.getField( "id" );
  assertEquals("2", f.getValue());
  assertEquals(add.commitWithin, 100);
  assertFalse(add.overwrite);

  req.close();
}
 
Example #25
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void doInc(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  SolrInputField numericField = toDoc.get(sif.getName());
  SchemaField sf = schema.getField(sif.getName());
  if (numericField != null || sf.getDefaultValue() != null) {
    // TODO: fieldtype needs externalToObject?
    String oldValS = (numericField != null) ?
        numericField.getFirstValue().toString(): sf.getDefaultValue().toString();
    BytesRefBuilder term = new BytesRefBuilder();
    sf.getType().readableToIndexed(oldValS, term);
    Object oldVal = sf.getType().toObject(sf, term.get());

    String fieldValS = fieldVal.toString();
    Number result;
    if (oldVal instanceof Long) {
      result = ((Long) oldVal).longValue() + Long.parseLong(fieldValS);
    } else if (oldVal instanceof Float) {
      result = ((Float) oldVal).floatValue() + Float.parseFloat(fieldValS);
    } else if (oldVal instanceof Double) {
      result = ((Double) oldVal).doubleValue() + Double.parseDouble(fieldValS);
    } else {
      // int, short, byte
      result = ((Integer) oldVal).intValue() + Integer.parseInt(fieldValS);
    }

    toDoc.setField(sif.getName(),  result);
  } else {
    toDoc.setField(sif.getName(), fieldVal);
  }
}
 
Example #26
Source File: CountFieldValuesUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req,
                                          SolrQueryResponse rsp,
                                          UpdateRequestProcessor next) {
  return mutator(getSelector(), next, src -> {
    SolrInputField result = new SolrInputField(src.getName());
    result.setValue(src.getValueCount());
    return result;
  });
}
 
Example #27
Source File: AddSchemaFieldsUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Maps all given field values' classes to a typeMapping object
 * 
 * @param fields one or more (same-named) field values from one or more documents
 */
private TypeMapping mapValueClassesToFieldType(List<SolrInputField> fields) {
  NEXT_TYPE_MAPPING: for (TypeMapping typeMapping : typeMappings) {
    for (SolrInputField field : fields) {
      //We do a assert and a null check because even after SOLR-12710 is addressed
      //older SolrJ versions can send null values causing an NPE
      assert field.getValues() != null;
      if (field.getValues() != null) {
        NEXT_FIELD_VALUE: for (Object fieldValue : field.getValues()) {
          for (Class<?> valueClass : typeMapping.valueClasses) {
            if (valueClass.isInstance(fieldValue)) {
              continue NEXT_FIELD_VALUE;
            }
          }
          // This fieldValue is not an instance of any of the mapped valueClass-s,
          // so mapping fails - go try the next type mapping.
          continue NEXT_TYPE_MAPPING;
        }
      }
    }
    // Success! Each of this field's values is an instance of a mapped valueClass
    return typeMapping;
  }
  // At least one of this field's values is not an instance of any of the mapped valueClass-s
  // Return the typeMapping marked as default, if we have one, else return null to use fallback type 
  List<TypeMapping> defaultMappings = typeMappings.stream().filter(TypeMapping::isDefault).collect(Collectors.toList());
  if (defaultMappings.size() > 1) {
    throw new SolrException(SERVER_ERROR, "Only one typeMapping can be default");
  } else if (defaultMappings.size() == 1) {
    return defaultMappings.get(0);
  } else {
    return null;
  }
}
 
Example #28
Source File: NestedUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean processDocChildren(SolrInputDocument doc, String fullPath) {
  boolean isNested = false;
  for(SolrInputField field: doc.values()) {
    int childNum = 0;
    boolean isSingleVal = !(field.getValue() instanceof Collection);
    for(Object val: field) {
      if (!(val instanceof SolrInputDocument)) {
        // either all collection items are child docs or none are.
        break;
      }
      final String fieldName = field.getName();

      if (fieldName.contains(PATH_SEP_CHAR)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Field name: '" + fieldName
            + "' contains: '" + PATH_SEP_CHAR + "' , which is reserved for the nested URP");
      }
      final String sChildNum = isSingleVal ? SINGULAR_VALUE_CHAR : String.valueOf(childNum);
      SolrInputDocument cDoc = (SolrInputDocument) val;
      if (!cDoc.containsKey(uniqueKeyFieldName)) {
        String parentDocId = doc.getField(uniqueKeyFieldName).getFirstValue().toString();
        cDoc.setField(uniqueKeyFieldName, generateChildUniqueId(parentDocId, fieldName, sChildNum));
      }
      if (!isNested) {
        isNested = true;
      }
      final String lastKeyPath = PATH_SEP_CHAR + fieldName + NUM_SEP_CHAR + sChildNum;
      // concat of all paths children.grandChild => /children#1/grandChild#
      final String childDocPath = fullPath == null ? lastKeyPath : fullPath + lastKeyPath;
      processChildDoc(cDoc, doc, childDocPath);
      ++childNum;
    }
  }
  return isNested;
}
 
Example #29
Source File: ConcatFieldUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public UpdateRequestProcessor getInstance(SolrQueryRequest req,
                                          SolrQueryResponse rsp,
                                          UpdateRequestProcessor next) {
  return mutator(getSelector(), next, src -> {
    if (src.getValueCount() <= 1) return src;

    SolrInputField result = new SolrInputField(src.getName());
    result.setValue(StringUtils.join(src.getValues(), delimiter));
    return result;
  });
}
 
Example #30
Source File: MorphlineResultToSolrMapperTest.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
private Record toRecord(SolrInputDocument doc) {
  Record record = new Record();
  for (Entry<String, SolrInputField> entry : doc.entrySet()) {
      record.getFields().putAll(entry.getKey(), entry.getValue().getValues());
  }
  return record;
}