This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » symfony projects » jobeet » [SOLVED] Zend lucene & pagination
[SOLVED] Zend lucene & pagination [message #98951] Wed, 12 May 2010 18:26 Go to next message
NeoX  is currently offline NeoX
Messages: 58
Registered: March 2010
Location: Spain
Member

Hi all,
I've followed Jobbet day 17: Search and It works.
Now I'm trying to include pagination in zend lucene search but It is impossible!!
If someone have a full example (actions & template code) and he/she could share it, I'll be very gratefull.

Thanks a lot.

[Updated on: Tue, 25 May 2010 17:00]

Re: Zend lucene & pagination [message #98952 is a reply to message #98951 ] Wed, 12 May 2010 18:27 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
What problems are you running into, specifically?


Remember Palestine
Re: Zend lucene & pagination [message #98960 is a reply to message #98951 ] Wed, 12 May 2010 19:53 Go to previous messageGo to next message
NeoX  is currently offline NeoX
Messages: 58
Registered: March 2010
Location: Spain
Member

The problem is I don't know how to implement it.
Do I need a DoctrinePaginator and a ZendPaginator or only a ZendPaginator??

I'm following this example without any succesfully results: http://framework.zend.com/manual/en/zend.paginator.usage.htm l

Thanks.

[Updated on: Thu, 13 May 2010 11:50]

Re: Zend lucene & pagination [message #99093 is a reply to message #98951 ] Fri, 14 May 2010 19:32 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
There are two Lucene plugins for Symfony, so I suggest you start there. I am not sure if they are up to date for 1.3 or 1.4, but they should help you make a start.

I don't know ZF, but the link you've supplied looks like a generic database pager, and has nothing to do with Lucene specifically.


Remember Palestine
Re: Zend lucene & pagination [message #99094 is a reply to message #98951 ] Fri, 14 May 2010 19:35 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Ah right, it's pagination you're trying to do - I misread your post. Well, my advice still applies Smile - I think this is in sfLucenePlugin, although that is quite old now. It should still give an indication of how to proceed, if you look at the code.


Remember Palestine
Re: Zend lucene & pagination [message #99772 is a reply to message #98951 ] Tue, 25 May 2010 16:54 Go to previous messageGo to next message
NeoX  is currently offline NeoX
Messages: 58
Registered: March 2010
Location: Spain
Member

Halfer, thanks.
I've got a solution. I share it with the community:

JobeetJob.class.php
  static public function getLuceneIndex()
  {
    ProjectConfiguration::registerZend();

    if (file_exists($index = self::getLuceneIndexFile()))
    {
      return Zend_Search_Lucene::open($index);
    }
    else
    {
      return Zend_Search_Lucene::create($index);
    }
  }

  static public function getLuceneIndexFile()
  {
    return sfConfig::get('sf_data_dir').'/job.'.sfConfig::get('sf_environment').'.index';
  }

  static public function getForLuceneQuery($query, $request)
  {
      $hits = self::getLuceneIndex()->find($query);

      $pks = array();

      foreach ($hits as $hit)
      {
        $pks[] = $hit->pk;
      }

      if (empty($pks))
      {
        return array();
      }

      $q = Doctrine_Query::create()
        ->from('JobeetJob j')
        ->whereIn('j.id', $pks);

      $pager = Zend_Paginator::factory($q->execute()->getData());
      $pager->setItemCountPerPage(3);
      $pager->setCurrentPageNumber($request->getParameter('page', 1));
      return $pager;
  }


Action code to get the pager:
  $this->pager = Doctrine_core::getTable('JobeetJob')->getForLuceneQuery($query, $request);


Hope this helps,

Cheers.
Re: Zend lucene & pagination [message #100869 is a reply to message #99772 ] Mon, 14 June 2010 00:27 Go to previous messageGo to next message
triplecero  is currently offline triplecero
Messages: 13
Registered: January 2009
Location: Venezuela
Junior Member

thanks for your code... very handy indeed... but for the sake of code reutilization, you could refactor all that into this:

  static public function getForLuceneQuery($query, $request)
  {
      $hits = self::getLuceneIndex()->find($query);

      $pks = array();

      foreach ($hits as $hit)
      {
        $pks[] = $hit->pk;
      }

      if (empty($pks))
      {
        return array();
      }

      $q = Doctrine_Query::create()
        ->from('JobeetJob j')
        ->whereIn('j.id', $pks);

      return $q;
  }


In the search action:

  public function executeSearch(sfWebRequest $request)
  {
    $this->forwardUnless($query = $request->getParameter('query'), 'job', 'index');

    $q = Doctrine_core::getTable('JobeetJob')->getForLuceneQuery($query);

    $pager = Zend_Paginator::factory($q);
    $pager->setItemCountPerPage(3);
    $pager->setCurrentPageNumber($request->getParameter('page', 1));

    $this->pager = $pager;
  }


and finally, in the template:
<?php include_partial('jobs/list', array('jobs => $pager)) ?>


This structure match with the same I use with sfDoctrinePager (when I don't use Zend Search Lucene, of course).

Anyway, I don't get why you used $q->execute()->getData(), use that method give me an error.

[Updated on: Mon, 14 June 2010 01:42]

Re: Zend lucene & pagination [message #101550 is a reply to message #100869 ] Thu, 24 June 2010 20:57 Go to previous messageGo to next message
zuby  is currently offline zuby
Messages: 9
Registered: May 2010
Location: Lagos, Nigeria
Junior Member
Hi guys,

I did exactly as above and i get the following error:

500 | Internal Server Error | Zend_Paginator_Exception

No adapter for type Doctrine_Collection

at ()
in C:\xampp\php\PEAR\Zend\Paginator.php line 286 ...
*/

require_once 'Zend/Paginator/Exception.php';


throw new Zend_Paginator_Exception('No adapter for type ' . $type);

}

}


Any ideas?

thanks
zuby
Re: Zend lucene & pagination [message #101577 is a reply to message #101550 ] Fri, 25 June 2010 05:41 Go to previous messageGo to next message
triplecero  is currently offline triplecero
Messages: 13
Registered: January 2009
Location: Venezuela
Junior Member

According to the source code of factory (in the Paginator class), the adapter parameter only could be an INTERNAL_ADAPTER, an array, a instance of Zend_Db_Table_Select, Zend_Db_Select, Iterator or just a null value... but if you are working with another ORM (like Propel or Doctrine) your obviously must pass to the Zend factory method, an array dataset as parameter... so you must tell to Doctrine that change the returned result:

In getForLuceneQuery(), put this before "return $q" line (the last one, of course):
$q->setHydrationMode(Doctrine::HYDRATE_ARRAY);


(Probably I miss that when I did refactor the code that I sent to this thread, my code look a little bit different)

Let me know if that works...
Re: [SOLVED] Zend lucene & pagination [message #103440 is a reply to message #98951 ] Mon, 02 August 2010 21:38 Go to previous messageGo to next message
petersaints  is currently offline petersaints
Messages: 10
Registered: March 2010
Location: Portugal
Junior Member
Would this paginator work just like the sfDoctrinePager? Or could even that be used instead of Zend's Paginator?
Re: [SOLVED] Zend lucene & pagination [message #103445 is a reply to message #103440 ] Tue, 03 August 2010 02:51 Go to previous message
triplecero  is currently offline triplecero
Messages: 13
Registered: January 2009
Location: Venezuela
Junior Member

I'm not sure if Zend Paginator is better or equal to sfDoctrinePager, cause I've used only the latter... but IMHO there's not need to use another one.
Previous Topic:doctrine not using prepared statements why?
Next Topic:Snapshots for symfony 1.4 and Propel
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software