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

The following examples show how to use org.apache.solr.common.SolrInputDocument#get() . 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: 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 2
Source File: CustomIndexLoader.java    From solr-autocomplete with Apache License 2.0 6 votes vote down vote up
private static void addField(SolrInputDocument doc, String name, String value) {
  // find if such field already exists
  if (doc.get(name) == null) {
    // System.out.println("Adding field " + name + " without previous values");
    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);
    for (Object val : f.getValues()) {
      // fix for boolean values
      if ((value.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) ||
          (value.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) {
            return;
      }
      if (value.equals(val.toString())) {
        // if we find such value in the doc, we will not add it again
        // System.out.println("Field " + name + " already contains value " + value);
        return;
      }
    }
    // System.out.println("Adding field " + name + " without new value " + value);
    f.addValue(value);
  }
}
 
Example 3
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 4
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 5
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 6
Source File: CustomIndexLoader.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private static void addField(SolrInputDocument doc, String name, String [] values) {
  // find if such field already exists
  if (doc.get(name) == null) {
    doc.addField(name, values);
  } 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);
    for (String v : values) {
      boolean valueAlreadyIn = false;
      for (Object val : f.getValues()) {
        if (v.equals(val.toString())) {
          // if we find such value in the doc, we will not add it again
          valueAlreadyIn = true;
          break;
        }
        // fix for boolean values
        if ((v.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) ||
            (v.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) {
          valueAlreadyIn = true;
          break;
        }
      }
      
      if (!valueAlreadyIn) {
        f.addValue(v);
      }
    }
  }
}
 
Example 7
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 8
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 9
Source File: AtomicUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void processAdd(AddUpdateCommand cmd)
    throws IOException {

  SolrInputDocument orgdoc = cmd.getSolrInputDocument();
  boolean isAtomicUpdateAddedByMe = false;

  Iterator<String> paramsIterator = req.getParams().getParameterNamesIterator();

  while (paramsIterator.hasNext()) {

    String param = paramsIterator.next();

    if (!param.startsWith(ATOMIC_FIELD_PREFIX)) continue;

    String field = param.substring(ATOMIC_FIELD_PREFIX.length(), param.length());
    String operation = req.getParams().get(param);

    if (!VALID_OPS.contains(operation)) {
      throw new SolrException(SERVER_ERROR,
          "Unexpected param(s) for AtomicUpdateProcessor, invalid atomic op passed: '" +
              req.getParams().get(param) + "'");
    }
    if (orgdoc.get(field) == null || orgdoc.get(field).getValue() instanceof Map) {
      // no value for the field or it's already an atomic update operation
      //continue processing other fields
      continue;
    }

    orgdoc.setField(field, singletonMap(operation, orgdoc.get(field).getValue()));
    isAtomicUpdateAddedByMe = true;
  }

  // if atomic, put _version_ for optimistic concurrency if doc present in index
  if (isAtomicUpdateAddedByMe) {
    Long lastVersion = vinfo.lookupVersion(cmd.getIndexedId());
    // if lastVersion is null then we put -1 to assert that document must not exist
    lastVersion = lastVersion == null ? -1 : lastVersion;
    orgdoc.setField(VERSION, lastVersion);
    processAddWithRetry(cmd, 1, cmd.getSolrInputDocument().deepCopy());
  } else {
    super.processAdd(cmd);
  }
  // else send it for doc to get inserted for the first time
}