我通过搜索更改了文档字段以使其可排序,但现在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);
heenenee.. 5
您需要单独添加一个SortedDocValuesField
以利用Lucene 5中新的更快排序.在建立索引时,将email
字段添加到文档中,如下所示:
doc.add(new StringField("email", email, Field.Store.YES)); doc.add(new SortedDocValuesField("email", new BytesRef(email)));
此外,如果您在搜索中尝试使用电子邮件查找所有文档,最好使用a new FieldValueQuery("email")
而不是WildcardQuery
.
您需要单独添加一个SortedDocValuesField
以利用Lucene 5中新的更快排序.在建立索引时,将email
字段添加到文档中,如下所示:
doc.add(new StringField("email", email, Field.Store.YES)); doc.add(new SortedDocValuesField("email", new BytesRef(email)));
此外,如果您在搜索中尝试使用电子邮件查找所有文档,最好使用a new FieldValueQuery("email")
而不是WildcardQuery
.