Lucene:使用DocValues添加字段时为null值(Lucene: null value when adding a field with DocValues)

编程入门 行业动态 更新时间:2024-10-07 22:22:16
Lucene:使用DocValues添加字段时为null值(Lucene: null value when adding a field with DocValues)

我通过搜索更改了一个文档字段以进行排序,但现在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.

更多推荐

本文发布于:2023-08-04 21:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   DocValues   Lucene   field   adding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!