Changeset 12980


Ignore:
Timestamp:
2006-10-03T15:35:04+13:00 (18 years ago)
Author:
mdewsnip
Message:

Now passes the endresults value (if defined) into the Searcher.search() call so only the required number of documents are returned.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/LuceneWrap/GS2LuceneQuery.java

    r12976 r12980  
    3131import org.apache.lucene.search.RangeFilter;
    3232import org.apache.lucene.search.Searcher;
     33import org.apache.lucene.search.ScoreDoc;
    3334import org.apache.lucene.search.Sort;
     35import org.apache.lucene.search.TopFieldDocs;
    3436
    3537
     
    5759        QueryParser query_parser_no_stop_words = new QueryParser(TEXTFIELD, new StandardAnalyzer(new String[] { }));
    5860
    59         Sort sorter = null;
     61        Sort sorter = new Sort();
    6062        Filter filter = null;
    6163        String fuzziness = null;
     
    6365        // Paging
    6466        int start_results = 1;
    65         int end_results = -1;
     67        int end_results = Integer.MAX_VALUE;
    6668
    6769            // New code to allow the default conjunction operator to be
     
    157159            }
    158160
    159             // Perform the query (filter and sorter may be null)
    160             Hits hits = searcher.search(query, filter, sorter);
    161 
    162             // Do we need to use a hit iterator to get sorted results?
    163             System.out.println("  <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
    164             System.out.println("  <StartResults num=\"" + start_results + "\" />");
    165             System.out.println("  <EndsResults num=\"" + end_results + "\" />");
    166 
    167             int counter = 1;
    168             Iterator hit_iter = hits.iterator();
    169             while (hit_iter.hasNext()) {
    170             Hit hit = (Hit) hit_iter.next();
    171             Document doc = hit.getDocument();
    172            
    173             // May not be paging results
    174             if (start_results == 1 && end_results == -1) {
    175                 String node_id = doc.get("nodeID");
    176                 System.out.println("  <Match id=\"" + node_id + "\" />");
     161            // Simple case for getting all the matching documents
     162            if (end_results == Integer.MAX_VALUE) {
     163            // Perform the query (filter and sorter may be null)
     164            Hits hits = searcher.search(query, filter, sorter);
     165            System.out.println("  <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
     166
     167            // Output the matching documents
     168            System.out.println("  <StartResults num=\"" + start_results + "\" />");
     169            System.out.println("  <EndsResults num=\"" + hits.length() + "\" />");
     170            for (int i = start_results; i <= hits.length(); i++) {
     171                Document doc = hits.doc(i - 1);
     172                System.out.println("  <Match id=\"" + doc.get("nodeID") + "\" />");
    177173            }
    178             // Otherwise skip up until page offset
    179             else if (start_results <= counter && counter <= end_results) {
    180                 String node_id = doc.get("nodeID");
    181                 System.out.println("  <Match id=\"" + node_id + "\" />");
     174            }
     175
     176            // Slightly more complicated case for returning a subset of the matching documents
     177            else {
     178            // Perform the query (filter may be null)
     179            TopFieldDocs hits = searcher.search(query, filter, end_results, sorter);
     180            System.out.println("  <MatchingDocsInfo num=\"" + hits.totalHits + "\"/>");
     181
     182            // Output the matching documents
     183            System.out.println("  <StartResults num=\"" + start_results + "\" />");
     184            System.out.println("  <EndsResults num=\"" + end_results + "\" />");
     185            for (int i = start_results; (i <= hits.scoreDocs.length && i <= end_results); i++) {
     186                Document doc = reader.document(hits.scoreDocs[i - 1].doc);
     187                System.out.println("  <Match id=\"" + doc.get("nodeID") + "\" />");
    182188            }
    183             // And skip all the rest
    184 
    185             ++counter;
    186189            }
    187190        }
  • trunk/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneQuery.java

    r12976 r12980  
    3131import org.apache.lucene.search.RangeFilter;
    3232import org.apache.lucene.search.Searcher;
     33import org.apache.lucene.search.ScoreDoc;
    3334import org.apache.lucene.search.Sort;
     35import org.apache.lucene.search.TopFieldDocs;
    3436
    3537
     
    5759        QueryParser query_parser_no_stop_words = new QueryParser(TEXTFIELD, new StandardAnalyzer(new String[] { }));
    5860
    59         Sort sorter = null;
     61        Sort sorter = new Sort();
    6062        Filter filter = null;
    6163        String fuzziness = null;
     
    6365        // Paging
    6466        int start_results = 1;
    65         int end_results = -1;
     67        int end_results = Integer.MAX_VALUE;
    6668
    6769            // New code to allow the default conjunction operator to be
     
    157159            }
    158160
    159             // Perform the query (filter and sorter may be null)
    160             Hits hits = searcher.search(query, filter, sorter);
    161 
    162             // Do we need to use a hit iterator to get sorted results?
    163             System.out.println("  <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
    164             System.out.println("  <StartResults num=\"" + start_results + "\" />");
    165             System.out.println("  <EndsResults num=\"" + end_results + "\" />");
    166 
    167             int counter = 1;
    168             Iterator hit_iter = hits.iterator();
    169             while (hit_iter.hasNext()) {
    170             Hit hit = (Hit) hit_iter.next();
    171             Document doc = hit.getDocument();
    172            
    173             // May not be paging results
    174             if (start_results == 1 && end_results == -1) {
    175                 String node_id = doc.get("nodeID");
    176                 System.out.println("  <Match id=\"" + node_id + "\" />");
     161            // Simple case for getting all the matching documents
     162            if (end_results == Integer.MAX_VALUE) {
     163            // Perform the query (filter and sorter may be null)
     164            Hits hits = searcher.search(query, filter, sorter);
     165            System.out.println("  <MatchingDocsInfo num=\"" + hits.length() + "\"/>");
     166
     167            // Output the matching documents
     168            System.out.println("  <StartResults num=\"" + start_results + "\" />");
     169            System.out.println("  <EndsResults num=\"" + hits.length() + "\" />");
     170            for (int i = start_results; i <= hits.length(); i++) {
     171                Document doc = hits.doc(i - 1);
     172                System.out.println("  <Match id=\"" + doc.get("nodeID") + "\" />");
    177173            }
    178             // Otherwise skip up until page offset
    179             else if (start_results <= counter && counter <= end_results) {
    180                 String node_id = doc.get("nodeID");
    181                 System.out.println("  <Match id=\"" + node_id + "\" />");
     174            }
     175
     176            // Slightly more complicated case for returning a subset of the matching documents
     177            else {
     178            // Perform the query (filter may be null)
     179            TopFieldDocs hits = searcher.search(query, filter, end_results, sorter);
     180            System.out.println("  <MatchingDocsInfo num=\"" + hits.totalHits + "\"/>");
     181
     182            // Output the matching documents
     183            System.out.println("  <StartResults num=\"" + start_results + "\" />");
     184            System.out.println("  <EndsResults num=\"" + end_results + "\" />");
     185            for (int i = start_results; (i <= hits.scoreDocs.length && i <= end_results); i++) {
     186                Document doc = reader.document(hits.scoreDocs[i - 1].doc);
     187                System.out.println("  <Match id=\"" + doc.get("nodeID") + "\" />");
    182188            }
    183             // And skip all the rest
    184 
    185             ++counter;
    186189            }
    187190        }
Note: See TracChangeset for help on using the changeset viewer.