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

The following examples show how to use org.apache.lucene.index.IndexableField#numericValue() . 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: BaseEditorialTransformer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected BytesRef getKey(SolrDocument doc) {
  Object obj = doc.get(idFieldName);
  if (obj instanceof IndexableField) {
    IndexableField f = (IndexableField) obj;
    BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
    Number n = f.numericValue();
    if (n != null) {
      ft.readableToIndexed(n.toString(), bytesRefBuilder);
    } else {
      ft.readableToIndexed(f.stringValue(), bytesRefBuilder);
    }
    return bytesRefBuilder.get();
  } else if (obj instanceof String) { // Allows the idField to be stored=false, docValues=true
    return new BytesRef(((String)obj));
  }
  throw new AssertionError("Expected an IndexableField but got: " + obj.getClass());
}
 
Example 2
Source File: InstantStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    Number number = indexable.numericValue();
    if (Instant.class.isAssignableFrom(clazz)) {
        return Instant.ofEpochMilli(number.longValue());
    }
    if (Date.class.isAssignableFrom(clazz)) {
        return new Date(number.longValue());
    }
    if (LocalDate.class.isAssignableFrom(clazz)) {

    }
    if (LocalTime.class.isAssignableFrom(clazz)) {

    }
    if (LocalDateTime.class.isAssignableFrom(clazz)) {

    }
    if (ZonedDateTime.class.isAssignableFrom(clazz)) {

    }
    if (ZoneOffset.class.isAssignableFrom(clazz)) {

    }
    throw new StorageException();
}
 
Example 3
Source File: LongPointField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {
    return val;
  } else {
    throw new AssertionError("Unexpected state. Field: '" + f + "'");
  }
}
 
Example 4
Source File: AbstractEnumField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String toExternal(IndexableField f) {
  final Number val = f.numericValue();
  if (val == null)
    return null;

  return enumMapping.intValueToStringValue(val.intValue());
}
 
Example 5
Source File: AbstractEnumField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void write(TextResponseWriter writer, String name, IndexableField f) throws IOException {
  final Number val = f.numericValue();
  if (val == null) {
    writer.writeNull(name);
    return;
  }

  final String readableValue = enumMapping.intValueToStringValue(val.intValue());
  writer.writeStr(name, readableValue, true);
}
 
Example 6
Source File: AbstractEnumField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public EnumFieldValue toObject(IndexableField f) {
  Integer intValue = null;
  String stringValue = null;
  final Number val = f.numericValue();
  if (val != null) {
    intValue = val.intValue();
    stringValue = enumMapping.intValueToStringValue(intValue);
  }
  return new EnumFieldValue(intValue, stringValue);
}
 
Example 7
Source File: IntPointField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {
    return val.intValue();
  } else {
    throw new AssertionError("Unexpected state. Field: '" + f + "'");
  }
}
 
Example 8
Source File: FloatPointField.java    From lucene-solr with Apache License 2.0 5 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) {
      return Float.intBitsToFloat(val.intValue());
    } else if (f.fieldType().stored() == false && f.fieldType().docValuesType() == DocValuesType.SORTED_NUMERIC) {
      return NumericUtils.sortableIntToFloat(val.intValue());
    } else  {
      return val;
    }
  } else {
    throw new AssertionError("Unexpected state. Field: '" + f + "'");
  }
}
 
Example 9
Source File: DatePointField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Object toObject(IndexableField f) {
  final Number val = f.numericValue();
  if (val != null) {
    return new Date(val.longValue());
  } else {
    throw new AssertionError("Unexpected state. Field: '" + f + "'");
  }
}
 
Example 10
Source File: EnumFieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public String storedToIndexed(IndexableField f) {
  final Number val = f.numericValue();
  if (val == null)
    return null;
  final BytesRefBuilder bytes = new BytesRefBuilder();
  bytes.grow(Integer.BYTES);
  bytes.setLength(Integer.BYTES);
  NumericUtils.intToSortableBytes(val.intValue(), bytes.bytes(), 0);
  return bytes.get().utf8ToString();
}
 
Example 11
Source File: SolrDocumentFetcher.java    From lucene-solr with 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 12
Source File: FieldValueFeature.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public float score() throws IOException {

  try {
    final Document document = context.reader().document(itr.docID(),
        fieldAsSet);
    final IndexableField indexableField = document.getField(field);
    if (indexableField == null) {
      return getDefaultValue();
    }
    final Number number = indexableField.numericValue();
    if (number != null) {
      return number.floatValue();
    } else {
      final String string = indexableField.stringValue();
      if (string.length() == 1) {
        // boolean values in the index are encoded with the
        // a single char contained in TRUE_TOKEN or FALSE_TOKEN
        // (see BoolField)
        if (string.charAt(0) == BoolField.TRUE_TOKEN[0]) {
          return 1;
        }
        if (string.charAt(0) == BoolField.FALSE_TOKEN[0]) {
          return 0;
        }
      }
    }
  } catch (final IOException e) {
    throw new FeatureException(
        e.toString() + ": " +
            "Unable to extract feature for "
            + name, e);
  }
  return getDefaultValue();
}
 
Example 13
Source File: SpatialClusteringComponent.java    From solr-spatial-clustering with Apache License 2.0 5 votes vote down vote up
private static Number getFieldNumber(Document document, String name) {
    IndexableField field = document.getField(name);
    if (field == null) {
        return null;
    }

    return field.numericValue();
}
 
Example 14
Source File: DocumentField.java    From lucene-solr with 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 15
Source File: TestSimpleDocumentWriter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
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 16
Source File: TimeSeriesConverterCaller.java    From chronix.storage with Apache License 2.0 5 votes vote down vote up
/**
 * 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 17
Source File: HighlightHelper.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
/**
 * NOTE: This method will not preserve the correct field types.
 * 
 * @param preTag
 * @param postTag
 */
public static Document highlight(int docId, Document document, Query query, FieldManager fieldManager,
    IndexReader reader, String preTag, String postTag) throws IOException, InvalidTokenOffsetsException {

  String fieldLessFieldName = fieldManager.getFieldLessFieldName();

  Query fixedQuery = fixSuperQuery(query, null, fieldLessFieldName);

  Analyzer analyzer = fieldManager.getAnalyzerForQuery();

  SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(preTag, postTag);
  Document result = new Document();
  for (IndexableField f : document) {
    String name = f.name();
    if (fieldLessFieldName.equals(name) || FIELDS_NOT_TO_HIGHLIGHT.contains(name)) {
      result.add(f);
      continue;
    }
    String text = f.stringValue();
    Number numericValue = f.numericValue();

    Query fieldFixedQuery;
    if (fieldManager.isFieldLessIndexed(name)) {
      fieldFixedQuery = fixSuperQuery(query, name, fieldLessFieldName);
    } else {
      fieldFixedQuery = fixedQuery;
    }

    if (numericValue != null) {
      if (shouldNumberBeHighlighted(name, numericValue, fieldFixedQuery)) {
        String numberHighlight = preTag + text + postTag;
        result.add(new StringField(name, numberHighlight, Store.YES));
      }
    } else {
      Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(fieldFixedQuery, name));
      TokenStream tokenStream = TokenSources.getAnyTokenStream(reader, docId, name, analyzer);
      TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);
      for (int j = 0; j < frag.length; j++) {
        if ((frag[j] != null) && (frag[j].getScore() > 0)) {
          result.add(new StringField(name, frag[j].toString(), Store.YES));
        }
      }
    }
  }
  return result;
}
 
Example 18
Source File: CompressingStoredFieldsWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@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 19
Source File: SimpleTextStoredFieldsWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@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 20
Source File: NumberValue.java    From HongsCORE with MIT License 4 votes vote down vote up
@Override
public Object get(IndexableField f) {
    return f.numericValue();
}