1

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!

Chris Kempen
  • 9,491
  • 5
  • 40
  • 52
  • 1
    How about generating a random search term and using that? Maybe something within the context of your site? – vascowhite Feb 05 '12 at 17:43
  • That seems obvious now :/ Thanks for the help vascowhite, I'll give it a try..! :) – Chris Kempen Feb 05 '12 at 20:26
  • So there's nothing I could set the search term to, like `*` or `?` or some combination of that which would yield the first few results? Thanks again for any help! – Chris Kempen Feb 06 '12 at 08:36
  • Have you tried *? According to the manual, it should work. http://framework.zend.com/manual/en/zend.search.lucene.query-language.html#zend.search.lucene.query-language.wildcard – vascowhite Feb 06 '12 at 13:22
  • Yep, got an error along the lines of "the first 3 characters need to be non-wildcard characters"...having lots of fun with Zend lucene! :) – Chris Kempen Feb 06 '12 at 13:58

1 Answers1

2

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);
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • Another solution that seems obvious...so there's nothing I could set the search term to, like `*` or `?` or some combination of that which would yield the first few results? Thanks for the help! – Chris Kempen Feb 06 '12 at 08:35