~jamesh/mediascanner/ca-certs-dependency

« back to all changes in this revision

Viewing changes to src/mediascanner/mediaindex.cpp

  • Committer: Tarmac
  • Author(s): James Henstridge
  • Date: 2013-10-04 14:25:39 UTC
  • mfrom: (388.1.4 sort-results)
  • Revision ID: tarmac-20131004142539-ngejl3y6fdkqzz56
If a limit on the number of results for a search is given, sort the results. Fixes: https://bugs.launchpad.net/bugs/1234145.

Approved by Jussi Pakkanen, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <Term.h>
40
40
#include <TermDocs.h>
41
41
#include <TopDocs.h>
 
42
#include <TopScoreDocCollector.h>
42
43
 
43
44
// Boost C++
44
45
#include <boost/filesystem.hpp>
831
832
        }
832
833
    }
833
834
 
834
 
    const Lucene::CollectorPtr media_collector =
835
 
            newLucene<Collector>(visit_item, d->searcher(),
836
 
                                 query, offset, limit);
837
 
    d->searcher()->search(query, media_collector);
 
835
    const auto searcher = d->searcher();
 
836
 
 
837
    if (limit < 0) {
 
838
        /* If no limit is specified, return results unsorted */
 
839
        const Lucene::CollectorPtr media_collector =
 
840
            newLucene<Collector>(visit_item, searcher, query, offset, limit);
 
841
        searcher->search(query, media_collector);
 
842
    } else {
 
843
        const auto collector = Lucene::TopScoreDocCollector::create(
 
844
            offset + limit, false);
 
845
        searcher->search(query, collector);
 
846
 
 
847
        const auto docs = collector->topDocs(offset, limit)->scoreDocs;
 
848
        int32_t remaining = docs.size();
 
849
        for (const auto &score_doc: docs) {
 
850
            const auto doc = searcher->doc(score_doc->doc);
 
851
            visit_item(ExtractProperties(doc), remaining);
 
852
            if (remaining > 0)
 
853
                remaining--;
 
854
        }
 
855
    }
838
856
 
839
857
    return true;
840
858
}