/** * Returns a QueryContext with sorting added to it. * * @param key The key to sort on. * @param additionalKeys Any additional keys to sort on. * @return A QueryContext with sorting added to it. */ public QueryContext sort( String key, String... additionalKeys ) { SortField firstSortField = new SortedSetSortField( key, false ); if ( additionalKeys.length == 0 ) { return sort( new Sort( firstSortField ) ); } SortField[] sortFields = new SortField[1+additionalKeys.length]; sortFields[0] = firstSortField; for ( int i = 0; i < additionalKeys.length; i++ ) { sortFields[1+i] = new SortedSetSortField( additionalKeys[i], false ); } return sort( new Sort( sortFields ) ); }
private static int runQuery(IndexReader indexReader, int count, Query q) throws IOException { long start = System.currentTimeMillis(); IndexSearcher searcher = new IndexSearcher(indexReader); Sort sort = new Sort(); sort.setSort(new SortedSetSortField("category", false)); TopFieldCollector collector = TopFieldCollector.create(sort, count, null, true, true, true); searcher.search(q, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; int totalHits = collector.getTotalHits(); @SuppressWarnings("unused") long searchTime = System.currentTimeMillis() - start; start = System.currentTimeMillis(); List<String> ids = new ArrayList<>(); for (ScoreDoc hit : hits) { int docId = hit.doc; Document d = searcher.doc(docId); ids.add(d.get("uid")); } @SuppressWarnings("unused") long fetchTime = System.currentTimeMillis() - start; return totalHits; }