简体   繁体   English

在Apache Lucene中获取字段类型

[英]Get field type in Apache Lucene

What I'm trying to achieve is a way to detect if the stored value is numeric( IntPoint or FloatPoint ) or string( StringField or TextField ). 我想要实现的是一种检测存储的值是数字( IntPointFloatPoint )还是字符串( StringFieldTextField )的方法。

For example : I'm storing this field 例如:我正在存储此字段

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

after I execute a query that return some documents I want to detect if Value field type is IntPoint 执行返回一些文档的查询后,我想检测“ Value字段类型是否为IntPoint

I'm using something like this 我正在使用这样的东西

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

which returns stored 返回stored

If I use field.fieldType().docValuesType() it will return NONE 如果我使用field.fieldType().docValuesType() ,它将返回NONE

Is there a way to achieve this? 有没有办法做到这一点?

This seems to be what Luke does, hope it helps 这似乎是卢克所做的,希望能有所帮助

 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 链接到卢克代码

First of all, here is remark about IntPoint field 首先,这里是有关IntPoint字段的说明

An indexed field for fast range filters. 用于快速范围过滤器的索引字段。 If you also need to store the value, you should add a separate StoredField instance. 如果还需要存储值,则应添加一个单独的StoredField实例。

which basically means, I'm not sure, how you get this working: 从根本上讲,我不确定您是如何工作的:

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

it should return you null , instead of a field. 它应该返回null ,而不是一个字段。 Also, applicable to all fields - if you're operating on Document returned to your by Lucene - it will only contains fields, which are stored once again. 同样,适用于所有字段-如果您正在处理Lucene返回给您的Document-它仅包含字段,这些字段将再次存储。

For stored fields, you could easily get their type by doing something like this: 对于存储的字段,您可以通过执行以下操作轻松获取其类型:

((StoredField) text).fieldsData

which would return exactly what's stored there - Float or String or Double. 它将返回确切存储在此处的内容-Float或String或Double。

Another possibility is to call set of methods stringValue, binaryValue, readerValue, numericValue , which would return you String , ByteRef , Reader or Number or null, if it's not a number, for example. 另一种可能性是调用方法集stringValue, binaryValue, readerValue, numericValue ,这将返回StringByteRefReaderNumber ;如果不是数字,则返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM