Problems with sorting Lucene 5 (UnInvertedReader and DocValues)

I am working on a search engine created in Lucene 5.2.1, but I am having problems with the "Sort updates" option for the search. When searching with the Sort option, an error appears:

Exception in thread "main" java.lang.IllegalStateException: unexpected docvalues ​​NONE for the 'stars' fields (expected = NUMERIC). Use a UninvertingReader or index with docvalues.
    at org.apache.lucene.index.DocValues.checkField (DocValues.java:208)
    at org.apache.lucene.index.DocValues.getNumeric (DocValues.java:227)
    at org.apache.lucene.search.FieldComparator $ NumericCompapa .getNumericDocValues ​​(FieldComparator.java:167)
    at org.apache.lucene.search.FieldComparator $ NumericComparator.doSetNextReader (FieldComparator.java:153)
    at org.apache.lucene.search.SimpleFieldComparator.getLeafComparator Simple )
    on org.apache.lucene.search.FieldValueHitQueue.getComparators (FieldValueHitQueue.java:183)
    at org.apache.lucene.search.TopFieldCollector $ NonScoringCollector.getLeafCollector (TopFieldCollector.java:141)
    at org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:762)
    at org.apache.lucene.secse.secse.secse.secse.sec .search (IndexSearcher.java:485)
    on org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:694)
    on org.apache.lucene.search.IndexSearcher.searchAfter (IndexSearcher.java:679)
    on org. apache.lucene.search.IndexSearcher.searchAfter (IndexSearcher.java:621)
    at org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java∗27)
    at org.apache.lucene.search.IndexSearcher.search (IndexSearcher. java: 577)
    on SearchEngine.searchBusinessByCategory (SearchEngine.java:145)
    in Tests.searchEngineTest (Tests.java:137)
    on Main.main (Main.java:13)

I indexed a document that contains a double “star” field, which I need to use to sort the search results (the document with the highest “star” value should be on top).

Document doc = new Document();
doc.add(new DoubleField("stars", stars, Store.YES));

Then I applied the "Search" command with the "Sort" option in the "star" field as follows:

SortField sortfield = new SortField("stars", SortField.Type.DOUBLE, true);
Sort sort = new Sort(sortfield);
mySearcher.search(query, maxdocs, sort);

I found similar discussions on the Internet, but they all talk about SolR (or sometimes Elastic Search), but they don't provide either a Lucene snippet or a more general solution strategy for Lucene 5. For example:

Solr DocValues ​​ UninvertingReader, ( ). DocValues , . ( h ttp://grokbase.com/t/lucene/java-user/152q2jcgzz/lucene-4-x-5-illegalstateexception-while-sorting)

, - UninvertingReader DocValues. ?

+4
3

FieldType, DoubleField docvalues.

:

private static final FieldType DOUBLE_FIELD_TYPE_STORED_SORTED = new FieldType();
static {
    DOUBLE_FIELD_TYPE_STORED_SORTED.setTokenized(true);
    DOUBLE_FIELD_TYPE_STORED_SORTED.setOmitNorms(true);
    DOUBLE_FIELD_TYPE_STORED_SORTED.setIndexOptions(IndexOptions.DOCS);
    DOUBLE_FIELD_TYPE_STORED_SORTED
        .setNumericType(FieldType.NumericType.DOUBLE);
    DOUBLE_FIELD_TYPE_STORED_SORTED.setStored(true);
    DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
    DOUBLE_FIELD_TYPE_STORED_SORTED.freeze();
}

, :

Document doc = new Document();
doc.add(new DoubleField("stars", stars, DOUBLE_FIELD_TYPE_STORED_SORTED));

new DoubleField("stars", stars, Stored.YES);

FieldType:

public static final FieldType TYPE_STORED = new FieldType();
static {
    TYPE_STORED.setTokenized(true);
    TYPE_STORED.setOmitNorms(true);
    TYPE_STORED.setIndexOptions(IndexOptions.DOCS);
    TYPE_STORED.setNumericType(FieldType.NumericType.DOUBLE);
    TYPE_STORED.setStored(true);
    TYPE_STORED.freeze();
}

DocValues.

+5

, @user1071777:

private static final FieldType DOUBLE_FIELD_TYPE_STORED_SORTED = new FieldType(LongField.TYPE_STORED);

static {
    DOUBLE_FIELD_TYPE_STORED_SORTED.setDocValuesType(DocValuesType.NUMERIC);
    DOUBLE_FIELD_TYPE_STORED_SORTED.freeze();
}

, fieldType, , .

+3

I used DoubleField, but decided to solve the problem using DoubleDocValuesField () instead.

0
source

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


All Articles