我通过搜索更改了一个文档字段以进行排序,但现在addDocument()抛出一个异常,说明字段值为null,尽管我在添加字段时验证了该电子邮件是非空字符串。 在异常之前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);I changed a document field to become sortable by search, but now addDocument() throws an exception saying that the field value is null, although I verified that email is a non-null string when I add the field. Before the exception Lucene code is getting the binaryValue() from the field. It is suspicious that the StringField constructor does not accept a custom FieldType. Can I use String fields for sorting? How to fix this?
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)EDIT:
this code is used for searching:
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);最满意答案
您需要单独添加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 。
You need to separately add a SortedDocValuesField to take advantage of the new faster sorting in Lucene 5. When indexing, add the email field to your document as follows:
doc.add(new StringField("email", email, Field.Store.YES)); doc.add(new SortedDocValuesField("email", new BytesRef(email)));Also, if you're trying to find all of the documents with emails in your search, it would be better to use a new FieldValueQuery("email") instead of a WildcardQuery.
更多推荐
发布评论