Lucene:使用DocValues添加字段时为null值

时间:2015-12-23 13:28:25

标签: java lucene

我通过搜索更改了文档字段以使其可排序,但是现在addDocument()会抛出一个异常,说明字段值为空,尽管我在添加字段时验证了该电子邮件是非空字符串。在异常之前Lucene代码从字段中获取binaryValue()。怀疑StringField构造函数不接受自定义FieldType。我可以使用String字段进行排序吗?如何解决这个问题?

Lucene 5.3.1

    private static final FieldType EMAIL_FIELD_TYPE = new FieldType(StringField.TYPE_STORED);
    static
    {
        EMAIL_FIELD_TYPE.setDocValuesType(DocValuesType.SORTED);
        EMAIL_FIELD_TYPE.freeze();
    }

    ...
        doc.add(new Field("email", email, EMAIL_FIELD_TYPE));
    ...
        writer.addDocument(doc);
        writer.commit();

java.lang.IllegalArgumentException: field "email": null value not allowed
    at org.apache.lucene.index.SortedDocValuesWriter.addValue(SortedDocValuesWriter.java:65)
    at org.apache.lucene.index.DefaultIndexingChain.indexDocValue(DefaultIndexingChain.java:435)
    at org.apache.lucene.index.DefaultIndexingChain.processField(DefaultIndexingChain.java:376)
    at org.apache.lucene.index.DefaultIndexingChain.processDocument(DefaultIndexingChain.java:300)
    at org.apache.lucene.index.DocumentsWriterPerThread.updateDocument(DocumentsWriterPerThread.java:234)
    at org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:450)
    at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1475)
    at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1254)

编辑:

此代码用于搜索:

Query q = new WildcardQuery(new Term("email", "*"));
Sort sort = new Sort(new SortField("email", SortField.Type.STRING));
TopDocs res = searcher.search(q, Integer.MAX_VALUE, sort);

1 个答案:

答案 0 :(得分:5)

您需要单独添加SortedDocValuesField以利用Lucene 5中新的更快排序。编制索引时,将email字段添加到文档中,如下所示:

doc.add(new StringField("email", email, Field.Store.YES));
doc.add(new SortedDocValuesField("email", new BytesRef(email)));

此外,如果您尝试在搜索中找到包含电子邮件的所有文档,最好使用new FieldValueQuery("email")代替WildcardQuery