Example usage for org.apache.lucene.search FieldComparator compare

List of usage examples for org.apache.lucene.search FieldComparator compare

Introduction

In this page you can find the example usage for org.apache.lucene.search FieldComparator compare.

Prototype

public abstract int compare(int slot1, int slot2);

Source Link

Document

Compare hit at slot1 with hit at slot2.

Usage

From source file:org.apache.solr.search.MultiCollector.java

License:Apache License

@Override
public void collect(int doc) throws IOException {
    matches++;/*w w  w  . j a v a  2  s  .  c o  m*/
    filler.fillValue(doc);
    SearchGroup group = groupMap.get(mval);
    if (group == null) {
        int num = groupMap.size();
        if (groupMap.size() < nGroups) {
            SearchGroup sg = new SearchGroup();
            sg.groupValue = mval.duplicate();
            sg.comparatorSlot = num++;
            sg.matches = 1;
            sg.topDoc = docBase + doc;
            // sg.topDocScore = scorer.score();
            for (FieldComparator fc : comparators)
                fc.copy(sg.comparatorSlot, doc);
            groupMap.put(sg.groupValue, sg);
            return;
        }

        if (orderedGroups == null) {
            buildSet();
        }

        for (int i = 0;; i++) {
            final int c = reversed[i] * comparators[i].compareBottom(doc);
            if (c < 0) {
                // Definitely not competitive.
                return;
            } else if (c > 0) {
                // Definitely competitive.
                break;
            } else if (i == comparators.length - 1) {
                // Here c=0. If we're at the last comparator, this doc is not
                // competitive, since docs are visited in doc Id order, which means
                // this doc cannot compete with any other document in the queue.
                return;
            }
        }

        // remove current smallest group
        SearchGroup smallest = orderedGroups.pollLast();
        groupMap.remove(smallest.groupValue);

        // reuse the removed SearchGroup
        smallest.groupValue.copy(mval);
        smallest.matches = 1;
        smallest.topDoc = docBase + doc;
        // smallest.topDocScore = scorer.score();
        for (FieldComparator fc : comparators)
            fc.copy(smallest.comparatorSlot, doc);

        groupMap.put(smallest.groupValue, smallest);
        orderedGroups.add(smallest);

        for (FieldComparator fc : comparators)
            fc.setBottom(orderedGroups.last().comparatorSlot);

        return;
    }

    //
    // update existing group
    //

    group.matches++; // TODO: these aren't valid if the group is every discarded then re-added.  keep track if there have been discards?

    for (int i = 0;; i++) {
        FieldComparator fc = comparators[i];
        fc.copy(spareSlot, doc);

        final int c = reversed[i] * fc.compare(group.comparatorSlot, spareSlot);
        if (c < 0) {
            // Definitely not competitive.
            return;
        } else if (c > 0) {
            // Definitely competitive.
            // Set remaining comparators
            for (int j = i + 1; j < comparators.length; j++)
                comparators[j].copy(spareSlot, doc);
            break;
        } else if (i == comparators.length - 1) {
            // Here c=0. If we're at the last comparator, this doc is not
            // competitive, since docs are visited in doc Id order, which means
            // this doc cannot compete with any other document in the queue.
            return;
        }
    }

    // remove before updating the group since lookup is done via comparators
    // TODO: optimize this
    if (orderedGroups != null)
        orderedGroups.remove(group);

    group.topDoc = docBase + doc;
    // group.topDocScore = scorer.score();
    int tmp = spareSlot;
    spareSlot = group.comparatorSlot;
    group.comparatorSlot = tmp; // swap slots

    // re-add the changed group
    if (orderedGroups != null)
        orderedGroups.add(group);
}

From source file:org.apache.solr.search.MultiCollector.java

License:Apache License

void buildSet() {
    Comparator<SearchGroup> comparator = new Comparator<SearchGroup>() {
        public int compare(SearchGroup o1, SearchGroup o2) {
            for (int i = 0;; i++) {
                FieldComparator fc = comparators[i];
                int c = reversed[i] * fc.compare(o1.comparatorSlot, o2.comparatorSlot);
                if (c != 0) {
                    return c;
                } else if (i == comparators.length - 1) {
                    return o1.topDoc - o2.topDoc;
                }//w  ww  .  ja  va 2 s. co m
            }
        }
    };

    orderedGroups = new TreeSet<SearchGroup>(comparator);
    orderedGroups.addAll(groupMap.values());
    if (orderedGroups.size() == 0)
        return;
    for (FieldComparator fc : comparators)
        fc.setBottom(orderedGroups.last().comparatorSlot);
}