Lucene: null when adding a field with DocValues

I changed the document field to sort it by search, but now it addDocument()throws an exception saying that the field value is null, although I confirmed that when I add the field I get a non-empty string. Before an exception, Lucene gets the code binaryValue()from the field. It is suspicious that the constructor StringFielddoes not accept the custom FieldType type. Can I use string fields to sort? How to fix it?

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 to search:

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);
+4
source share
1 answer

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.

+5

Source: https://habr.com/ru/post/1621461/


All Articles