代码之家  ›  专栏  ›  技术社区  ›  Doua Beri

获取Apache Lucene中的字段类型

  •  1
  • Doua Beri  · 技术社区  · 8 年前

    我试图实现的是一种检测存储值是否为数字的方法( IntPoint 或 FloatPoint )还是绳子( StringField 或 TextField ).

    例如: 我在存储这个字段

    doc.add(new IntPoint("Value", 4));
    

    在我执行一个返回一些文档的查询之后,我想检测一下 Value 字段类型为 IntPoint

    我用的是这样的东西

    IndexableField field = doc.getField("Value");
    if (field != null)
        System.out.println(field.fieldType());
    

    它回来了 stored

    如果我使用 field.fieldType().docValuesType() 它会回来的 NONE

    有没有办法做到这一点?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Yossi Vainshtein    8 年前

    卢克就是这么做的,希望能有所帮助

     public static String fieldFlags(Field fld, FieldInfo info) {
        ...
        Number numeric = null;
        if (fld == null) {
          ...
        } else {
          ...
          numeric = fld.numericValue();
        }
        ...
        if (numeric != null) {
          flags.append("#");
          // try faking it
          if (numeric instanceof Integer) {
            flags.append("i32");
          } else if (numeric instanceof Long) {
            flags.append("i64");
          } else if (numeric instanceof Float) {
            flags.append("f32");
          } else if (numeric instanceof Double) {
            flags.append("f64");
          } else if (numeric instanceof Short) {
            flags.append("i16");
          } else if (numeric instanceof Byte) {
            flags.append("i08");
          } else if (numeric instanceof BigDecimal) {
            flags.append("b^d");
          } else if (numeric instanceof BigInteger) {
            flags.append("b^i");
          } else {
            flags.append("???");
          }
          ...
        }
    

    Link to luke code

        2
  •  1
  •   Mysterion    8 年前

    首先,这里是关于 IntPoint 领域

    用于快速范围过滤器的索引字段。如果你还需要存储 值时,应添加一个单独的StoredField实例。

    这基本上意味着,我不确定,你是如何做到这一点的:

    IndexableField field = doc.getField("Value");
    

    它应该还给你 null ,而不是一个字段。 此外,适用于所有字段——如果您对Lucene返回给您的文档进行操作——它将只包含字段,这些字段将再次存储。

    对于存储字段,您可以通过以下操作轻松获取其类型:

    ((StoredField) text).fieldsData
    

    它将返回存储在那里的内容——Float、String或Double。

    另一种可能性是调用一组方法 stringValue, binaryValue, readerValue, numericValue ,这会回报你 String , ByteRef , Reader 或 Number 或者null,如果它不是一个数字,例如。

    推荐文章