I want to write a ComparatorSource for Lucene 3.6 with which I can execute the Round Robin sorting style:
Say we have something like this:
A, A, A, B, C, D, D
And I want them to be sorted in that order (not necessarily in alphabetical order)
A, B, C, D, A, D, A
How can I perform this behavior?
This is what I came up with, but it does not work, because temporary renters are updated in the right place. Where can i do this?
public class RoundRobinFieldComparatorSource<T> extends FieldComparatorSource { private static long seed = new Random().nextInt(); public static void setNewSeed(long seed) { RoundRobinFieldComparatorSource.seed = seed; } private static final long serialVersionUID = -8959374194451783596L; private final Set<T> roundRobinValues; private final boolean excluded; private final TwoWayStringBridge stringBridge; private final Set<String> preferredValues; public RoundRobinFieldComparatorSource(TwoWayStringBridge stringBridge, Set<T> roundRobinValues) { this(stringBridge, roundRobinValues, new HashSet<T>()); } public RoundRobinFieldComparatorSource(TwoWayStringBridge stringBridge, Set<T> roundRobinValues, Set<T> preferredValues) { this(stringBridge, roundRobinValues, preferredValues, false); } public RoundRobinFieldComparatorSource(TwoWayStringBridge stringBridge, Set<T> roundRobinValues, Set<T> preferredValues, boolean excluded) { this.stringBridge = stringBridge; this.roundRobinValues = roundRobinValues; this.excluded = excluded; this.preferredValues = new HashSet<>(); if(preferredValues != null) { for(T val : preferredValues) { this.preferredValues.add(this.stringBridge.objectToString(val)); } } } public static <T> Set<T> toSet(Collection<T> iterable) { Set<T> ret = new HashSet<>(); ret.addAll(iterable); return ret; } @Override public FieldComparator<String> newComparator(final String fieldName, int numHits, int sortPos, final boolean reversed) throws IOException { return new FieldComparator<String>() { private String[] values; private String bottom; private String[] currentReaderValues; private Map<String, Integer> passedTimeShards; private Random random; @Override public int compare(int slot1, int slot2) { return this.compare(this.values[slot1], this.values[slot2]); } @Override public int compareBottom(int doc) throws IOException { return this.compare(this.bottom, this.currentReaderValues[doc]); } @Override public void copy(int slot, int doc) throws IOException { this.values[slot] = this.currentReaderValues[doc]; } @Override public void setBottom(int slot) { this.bottom = this.values[slot]; } @Override public void setNextReader(IndexReader reader, int docBase) throws IOException {
Thanks in advance.
source share