How to get first 20 documents in Zend lucene search when no search term is provided?

606 views Asked by At

I have a fairly new grasp of Zend_Search_Lucene, and how to build and search through documents added to indicies, but I want to know if it is possible to return documents if no search term is provided at all.

I know that sounds strange, and probably goes against the purpose of the lucene search, but I've run into a scenario where returning the first 20 documents to the user is preferred, rather than nothing, if they just click on 'Search' without typing in any search term.

So therein lies my question: what search term could I provide Zend_Search_Lucene that would return the first 20 documents it encounters when no search term is provided to rather see some results vs. seeing nothing at all.

I've already got this working great:

<?php

    Zend_Search_Lucene::setResultSetLimit(20);

    $index = Zend_Search_Lucene::open("some/path/to/index");

    $search_term = trim($_POST["search_term"]);

    if ($search_term == "")
    {
        // adjust the search term to return any documents...
        // will obviously be limited to the first 20...
    }

    $hits = $index->find($search_term);

    // display the results...
    // ...

?>

Thank you so much for your time, and any help / suggestions!

1

There are 1 answers

1
Mouna Cheikhna On BEST ANSWER

when no search term is provided search for the 20 records by searching range of ids 1 to 20 (but your document has to have an id field)

$from = new Zend_Search_Lucene_Index_Term(1, 'id');
$to   = new Zend_Search_Lucene_Index_Term(20, 'id');
$query = new Zend_Search_Lucene_Search_Query_Range(
             $from, $to, true // inclusive
         );
$hits  = $index->find($query);