Java Code Examples for org.apache.lucene.index.IndexableField#binaryValue()

The following examples show how to use org.apache.lucene.index.IndexableField#binaryValue() . 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: dremio-oss   File: LuceneSearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private List<Doc> toDocs(ScoreDoc[] hits, Searcher searcher) throws IOException{
  List<Doc> documentList = new ArrayList<>();
  for (int i = 0; i < hits.length; ++i) {
    ScoreDoc scoreDoc = hits[i];
    Document doc = searcher.doc(scoreDoc.doc);
    IndexableField idField = doc.getField("_id");
    if(idField == null){
      // deleted between index hit and retrieval.
      continue;
    }
    final BytesRef ref = idField.binaryValue();
    final byte[] bytes = new byte[ref.length];
    System.arraycopy(ref.bytes, ref.offset, bytes, 0, ref.length);
    Doc outputDoc = new Doc(scoreDoc, bytes, 0 /*version*/);
    documentList.add(outputDoc);
  }
  return documentList;
}
 
Example 2
private static SolrInputDocument toSolrInputDocument(Document doc, IndexSchema schema) {
  SolrInputDocument out = new SolrInputDocument();
  for( IndexableField f : doc.getFields() ) {
    String fname = f.name();
    SchemaField sf = schema.getFieldOrNull(f.name());
    Object val = null;
    if (sf != null) {
      if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue;
      val = sf.getType().toObject(f);   // object or external string?
    } else {
      val = f.stringValue();
      if (val == null) val = f.numericValue();
      if (val == null) val = f.binaryValue();
      if (val == null) val = f;
    }

    // todo: how to handle targets of copy fields (including polyfield sub-fields)?
    out.addField(fname, val);
  }
  return out;
}
 
Example 3
Source Project: Elasticsearch   File: ParseContext.java    License: Apache License 2.0 5 votes vote down vote up
public BytesRef getBinaryValue(String name) {
    for (IndexableField f : fields) {
        if (f.name().equals(name) && f.binaryValue() != null) {
            return f.binaryValue();
        }
    }
    return null;
}
 
Example 4
private static Object getValueFromField(Object expectedValue, IndexableField field) {
  if (expectedValue instanceof String) {
    return field.stringValue();
  } else if (expectedValue instanceof byte[]) {
    return field.binaryValue().bytes;
  }

  return field.numericValue();
}
 
Example 5
Source Project: lucene-solr   File: DocumentField.java    License: Apache License 2.0 5 votes vote down vote up
static DocumentField of(FieldInfo finfo, IndexableField field, IndexReader reader, int docId)
    throws IOException {

  Objects.requireNonNull(finfo);
  Objects.requireNonNull(reader);

  DocumentField dfield = new DocumentField();

  dfield.name = finfo.name;
  dfield.idxOptions = finfo.getIndexOptions();
  dfield.hasTermVectors = finfo.hasVectors();
  dfield.hasPayloads = finfo.hasPayloads();
  dfield.hasNorms = finfo.hasNorms();

  if (finfo.hasNorms()) {
    NumericDocValues norms = MultiDocValues.getNormValues(reader, finfo.name);
    if (norms.advanceExact(docId)) {
      dfield.norm = norms.longValue();
    }
  }

  dfield.dvType = finfo.getDocValuesType();

  dfield.pointDimensionCount = finfo.getPointDimensionCount();
  dfield.pointNumBytes = finfo.getPointNumBytes();

  if (field != null) {
    dfield.isStored = field.fieldType().stored();
    dfield.stringValue = field.stringValue();
    if (field.binaryValue() != null) {
      dfield.binaryValue = BytesRef.deepCopyOf(field.binaryValue());
    }
    dfield.numericValue = field.numericValue();
  }

  return dfield;
}
 
Example 6
Source Project: lucene-solr   File: Document.java    License: Apache License 2.0 5 votes vote down vote up
/**
* Returns an array of byte arrays for of the fields that have the name specified
* as the method parameter.  This method returns an empty
* array when there are no matching fields.  It never
* returns null.
*
* @param name the name of the field
* @return a <code>BytesRef[]</code> of binary field values
*/
public final BytesRef[] getBinaryValues(String name) {
  final List<BytesRef> result = new ArrayList<>();
  for (IndexableField field : fields) {
    if (field.name().equals(name)) {
      final BytesRef bytes = field.binaryValue();
      if (bytes != null) {
        result.add(bytes);
      }
    }
  }

  return result.toArray(new BytesRef[result.size()]);
}
 
Example 7
Source Project: lucene-solr   File: Document.java    License: Apache License 2.0 5 votes vote down vote up
/**
* Returns an array of bytes for the first (or only) field that has the name
* specified as the method parameter. This method will return <code>null</code>
* if no binary fields with the specified name are available.
* There may be non-binary fields with the same name.
*
* @param name the name of the field.
* @return a <code>BytesRef</code> containing the binary field value or <code>null</code>
*/
public final BytesRef getBinaryValue(String name) {
  for (IndexableField field : fields) {
    if (field.name().equals(name)) {
      final BytesRef bytes = field.binaryValue();
      if (bytes != null) {
        return bytes;
      }
    }
  }
  return null;
}
 
Example 8
Source Project: lucene-solr   File: SolrDocumentFetcher.java    License: Apache License 2.0 5 votes vote down vote up
/** Executes a stored field visitor against a hit from the document cache */
private void visitFromCached(Document document, StoredFieldVisitor visitor) throws IOException {
  for (IndexableField f : document) {
    final FieldInfo info = searcher.getFieldInfos().fieldInfo(f.name());
    final StoredFieldVisitor.Status needsField = visitor.needsField(info);
    if (needsField == StoredFieldVisitor.Status.STOP) return;
    if (needsField == StoredFieldVisitor.Status.NO) continue;
    BytesRef binaryValue = f.binaryValue();
    if (binaryValue != null) {
      visitor.binaryField(info, toByteArrayUnwrapIfPossible(binaryValue));
      continue;
    }
    Number numericValue = f.numericValue();
    if (numericValue != null) {
      if (numericValue instanceof Double) {
        visitor.doubleField(info, numericValue.doubleValue());
      } else if (numericValue instanceof Integer) {
        visitor.intField(info, numericValue.intValue());
      } else if (numericValue instanceof Float) {
        visitor.floatField(info, numericValue.floatValue());
      } else if (numericValue instanceof Long) {
        visitor.longField(info, numericValue.longValue());
      } else {
        throw new AssertionError();
      }
      continue;
    }
    // must be String
    if (f instanceof LargeLazyField) { // optimization to avoid premature string conversion
      visitor.stringField(info, toStringUnwrapIfPossible(((LargeLazyField) f).readBytes()));
    } else {
      visitor.stringField(info, f.stringValue());
    }
  }
}
 
Example 9
Source Project: lucene-solr   File: BoolField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toExternal(IndexableField f) {
  if (null != f.binaryValue()) {
    return indexedToReadable(f.binaryValue().utf8ToString());
  }
  if (null != f.stringValue()) {
    return indexedToReadable(f.stringValue());
  }
  return null;
}
 
Example 10
@Override
public ByteBuffer toObject(IndexableField f) {
    BytesRef bytes = f.binaryValue();
    if (bytes != null) {
        return  ByteBuffer.wrap(bytes.bytes, bytes.offset, bytes.length);
    }
    return ByteBuffer.allocate(0);
}
 
Example 11
/**
 * Adds user defined attributes to the binary time series builder.
 * Checks if the attribute is of type byte[], String, Number or Collection.
 * Otherwise the attribute is ignored.
 *
 * @param field the attribute field
 */
private Object convert(IndexableField field) {
    LOGGER.debug("Reading field {} ", field);

    if (field.numericValue() != null) {
        return field.numericValue();
    } else if (field.stringValue() != null) {
        return field.stringValue();
    } else if (field.binaryValue() != null) {
        return field.binaryValue().bytes;
    } else {
        LOGGER.debug("Field {} could not be handled. Type is not supported", field);
        return null;
    }
}
 
Example 12
@Override
public void writeField(FieldInfo info, IndexableField field) throws IOException {
  write(FIELD);
  write(Integer.toString(info.number));
  newLine();
  
  write(NAME);
  write(field.name());
  newLine();
  
  write(TYPE);
  final Number n = field.numericValue();

  if (n != null) {
    if (n instanceof Byte || n instanceof Short || n instanceof Integer) {
      write(TYPE_INT);
      newLine();
        
      write(VALUE);
      write(Integer.toString(n.intValue()));
      newLine();
    } else if (n instanceof Long) {
      write(TYPE_LONG);
      newLine();

      write(VALUE);
      write(Long.toString(n.longValue()));
      newLine();
    } else if (n instanceof Float) {
      write(TYPE_FLOAT);
      newLine();
        
      write(VALUE);
      write(Float.toString(n.floatValue()));
      newLine();
    } else if (n instanceof Double) {
      write(TYPE_DOUBLE);
      newLine();
        
      write(VALUE);
      write(Double.toString(n.doubleValue()));
      newLine();
    } else {
      throw new IllegalArgumentException("cannot store numeric type " + n.getClass());
    }
  } else { 
    BytesRef bytes = field.binaryValue();
    if (bytes != null) {
      write(TYPE_BINARY);
      newLine();
      
      write(VALUE);
      write(bytes);
      newLine();
    } else if (field.stringValue() == null) {
      throw new IllegalArgumentException("field " + field.name() + " is stored but does not have binaryValue, stringValue nor numericValue");
    } else {
      write(TYPE_STRING);
      newLine();
      write(VALUE);
      write(field.stringValue());
      newLine();
    }
  }
}
 
Example 13
@Override
public void writeField(FieldInfo info, IndexableField field)
    throws IOException {

  ++numStoredFieldsInDoc;

  int bits = 0;
  final BytesRef bytes;
  final String string;

  Number number = field.numericValue();
  if (number != null) {
    if (number instanceof Byte || number instanceof Short || number instanceof Integer) {
      bits = NUMERIC_INT;
    } else if (number instanceof Long) {
      bits = NUMERIC_LONG;
    } else if (number instanceof Float) {
      bits = NUMERIC_FLOAT;
    } else if (number instanceof Double) {
      bits = NUMERIC_DOUBLE;
    } else {
      throw new IllegalArgumentException("cannot store numeric type " + number.getClass());
    }
    string = null;
    bytes = null;
  } else {
    bytes = field.binaryValue();
    if (bytes != null) {
      bits = BYTE_ARR;
      string = null;
    } else {
      bits = STRING;
      string = field.stringValue();
      if (string == null) {
        throw new IllegalArgumentException("field " + field.name() + " is stored but does not have binaryValue, stringValue nor numericValue");
      }
    }
  }

  final long infoAndBits = (((long) info.number) << TYPE_BITS) | bits;
  bufferedDocs.writeVLong(infoAndBits);

  if (bytes != null) {
    bufferedDocs.writeVInt(bytes.length);
    bufferedDocs.writeBytes(bytes.bytes, bytes.offset, bytes.length);
  } else if (string != null) {
    bufferedDocs.writeString(string);
  } else {
    if (number instanceof Byte || number instanceof Short || number instanceof Integer) {
      bufferedDocs.writeZInt(number.intValue());
    } else if (number instanceof Long) {
      writeTLong(bufferedDocs, number.longValue());
    } else if (number instanceof Float) {
      writeZFloat(bufferedDocs, number.floatValue());
    } else if (number instanceof Double) {
      writeZDouble(bufferedDocs, number.doubleValue());
    } else {
      throw new AssertionError("Cannot get here");
    }
  }
}
 
Example 14
Source Project: lucene-solr   File: BinaryField.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuffer toObject(IndexableField f) {
  BytesRef bytes = f.binaryValue();
  return  ByteBuffer.wrap(bytes.bytes, bytes.offset, bytes.length);
}
 
Example 15
Source Project: lucene-solr   File: TrieField.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {

    if (f.fieldType().stored() == false && f.fieldType().docValuesType() == DocValuesType.NUMERIC ) {
      long bits = val.longValue();
      switch (type) {
        case INTEGER:
          return (int)bits;
        case FLOAT:
          return Float.intBitsToFloat((int)bits);
        case LONG:
          return bits;
        case DOUBLE:
          return Double.longBitsToDouble(bits);
        case DATE:
          return new Date(bits);
        default:
          throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
      }
    }

    // normal stored case
    return (type == NumberType.DATE) ? new Date(val.longValue()) : val;
  } else {
    // multi-valued numeric docValues currently use SortedSet on the indexed terms.
    BytesRef term = f.binaryValue();
    switch (type) {
      case INTEGER:
        return LegacyNumericUtils.prefixCodedToInt(term);
      case FLOAT:
        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(term));
      case LONG:
        return LegacyNumericUtils.prefixCodedToLong(term);
      case DOUBLE:
        return NumericUtils.sortableLongToDouble(LegacyNumericUtils.prefixCodedToLong(term));
      case DATE:
        return new Date(LegacyNumericUtils.prefixCodedToLong(term));
      default:
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
    }
  }

}