0

我正在使用以下代码来索引一个整数值

String key = hmap.get("key");
System.out.println("key == "+Integer.parseInt(key));
if(key!=null && key.trim().length()>0)
        doc.add(new IntField("kv", Integer.parseInt(key),IndexFieldTypes.getFieldType(INDEX_STORE_FIELD)));

问题是如果 'key' 是 '50' 行 'key== 50' 打印得很好但是当它到达 'doc.add' 行时它会抛出以下异常:

java.lang.IllegalArgumentException: type.numericType() must be INT but got null
at org.apache.lucene.document.IntField.<init>(IntField.java:171)

有人能弄清楚。

4

1 回答 1

1

An IntField must have a NumericFieldType of FieldType.NumericType.INT. Of course, I don't have intimate knowledge of your IndexFieldTypes class, but I would guess it's default INDEX_STORE_FIELD has no numeric type (rightly so, if it is non-null lucene will try to index as a number).

You may not necessarily need to pass a field type to IntField though, you could just do something like:

doc.add(new IntField("kv", Integer.parseInt(key), Field.Store.YES));

If you do need to define a FieldType, either use a different type from existing functionality in IndexFieldTypes, or implement logic to create an IntField from it. Or just set the NumericFieldType after it is retreived, like:

FieldType type = IndexFieldTypes.getFieldType(INDEX_STORE_FIELD);
type.setNumericFieldType(FieldType.NumericType.INT);
doc.add(new IntField("kv", Integer.parseInt(key), type));
于 2013-09-27T23:37:54.050 回答