Skip to content

Commit

Permalink
Add DocValues to support sorting of ticket index fields.
Browse files Browse the repository at this point in the history
In order to support sorting, Lucene 5 needs DocValue fields in an index.
So in order to make the ticket index work, i.e. show any tickets on the
tickets page, the ticket index needs to be changed, adding a DocValues
field.

The DocValuesFields are implemented for the current index, which does not
use multiple values for a field. Should at any time in the future an
existing numeric field get multiple values stored in a document, then
the index needs to know that and use SortedNumeric DocValues and SortFields
instead.
  • Loading branch information
fzs committed Mar 5, 2017
1 parent 71e24e2 commit bf1b35a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/gitblit/tickets/TicketIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
Expand All @@ -49,6 +51,7 @@
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -549,21 +552,25 @@ private void toDocField(Document doc, Lucene lucene, Date value) {
return;
}
doc.add(new LongField(lucene.name(), value.getTime(), Store.YES));
doc.add(new NumericDocValuesField(lucene.name(), value.getTime()));
}

private void toDocField(Document doc, Lucene lucene, long value) {
doc.add(new LongField(lucene.name(), value, Store.YES));
doc.add(new NumericDocValuesField(lucene.name(), value));
}

private void toDocField(Document doc, Lucene lucene, int value) {
doc.add(new IntField(lucene.name(), value, Store.YES));
doc.add(new NumericDocValuesField(lucene.name(), value));
}

private void toDocField(Document doc, Lucene lucene, String value) {
if (StringUtils.isEmpty(value)) {
return;
}
doc.add(new org.apache.lucene.document.Field(lucene.name(), value, TextField.TYPE_STORED));
doc.add(new SortedDocValuesField(lucene.name(), new BytesRef(value)));
}

/**
Expand Down

0 comments on commit bf1b35a

Please sign in to comment.