Question

I have site which is managed using CMS entirely developed on zend. Now I have to Implement a search feature too. I'vent done anything related to search in zend. Some suggestions that I received is to implement a spider. The site will have plenty of links(and it will keep on adding). I'm totally confused and I don't know where to start from. Will zend_search_lucene do the trick?

Was it helpful?

Solution

You probably aren't going to find something completely turnkey for this. If your content is all public, and you are fine with just using a crawler, the easiest thing to implement could be Google Site Search.

http://www.google.com/enterprise/search/products_gss.html

If you need to get different functionality out of the search that this wouldn't offer, you'll likely be stuck doing some code. The Zend Lucene link that Alvar posted is good. One of the ugly things about Zend_Lucene, if I am not mistaken, is that it's relying on the text based lucene indexes without any Java. It's just slower and more cumbersome to manage.

A more robust Lucene based approach is Solr. It's Java based, and runs on it's own service with an API. It scales well, and there's a PHP Pecl out now that will help you communicate with it.

See http://php.net/manual/en/book.solr.php

Another option is Sphinx. This search engine bolts directly to your database, so indexing might be a little more intuitive.

http://sphinxsearch.com/

Good luck to you!

OTHER TIPS

Lucene is strange, i never got it to work properly and developed my own search logic, but maybe this helps:

http://devzone.zend.com/397/roll-your-own-search-engine-with-zend_search_lucene/

Because you are using a home grown product you'll likely be better served by keeping things as simple as possible, at least in the beginning. Also because you're product is home grown you should have a pretty good handle on the data structure.

Building a simple query based search may be something appropriate for starters.

I started with a simple search form:

<?php

    class Application_Form_Search extends Zend_Form
    {

        public function init() {

            $this->setMethod('POST');
            $this->setDecorators(array(
                array('ViewScript', array(
                        'viewScript' => '_searchForm.phtml'
                ))
            ));

            // create new element
            $query = $this->createElement('text', 'query');
            // element options
            $query->setLabel('Search Keywords');
            $query->setAttribs(array('placeholder' => 'Title',
                'size' => 27,
            ));
            // add the element to the form
            $this->addElement($query);

            $submit = $this->createElement('submit', 'search');
            $submit->setLabel('Search Site');
            $submit->setDecorators(array('ViewHelper'));
            $this->addElement($submit);
        }
    }

then I built a simple action helper to display and route the form:

<?php

class Library_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{

    public function direct($action, $label = null, $placeHolder = null)
    {
        $form = new Application_Form_Search();
        $form->setAction($action);
        $form->search->setLabel($label);
        $form->query->setAttribs(array('placeholder' => $placeHolder,
            'size' => 27,
        ));

        return $form;
    }
}

then I added a placeholder for the search form in my layout.phtml

<?php echo $this->layout()->search ?>

then in the controllers that need to use the search function I add the helper to predispatch():

public function preDispatch()
    {
        //assign search action helper to view placeholder
        $this->_helper->layout()->search = $this->_helper->search(
                'url_for_action', 'Submit button label', 'placeholder text'
        );
    }

then I use a simple mapper method to perform the appropriate query and I usually return a paginator adapter:

 public function fetchPagedMoviesByTitle($title)
    {

        $select = $this->getGateway()->select();
        $select->where(new Zend_Db_Expr("title LIKE '%$title%'"));
        $select->order('title', 'ASC');

        //create a new instance of the paginator adapter and return it
        $adapter = new Video_Model_Paginator_Video($select);

        return $adapter;
    }

This is simple way to implement a search function and is adaptable to most types of queries. I find that a switch statment and a couple of simple database queries and almost any information I need is available.

Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top