Skip to content

bluedaniel/Zend-Service-Solr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Zend Service Solr


Example usage - Solr model (Plus zend pagination)

class Model_Solr {

    public $_pagination = null;

    /*
     * Pass in params like 'q', 'page', 'count'
     *
     * /
    public function search($op = array()) {	

        $pagination_array = array();

        $solr = $this->get_solr_connection();
        if ($solr->ping ()) {

            $params = array(
                'facet' => 'on',
                'facet.field' => 'cat', // cat - Short for category
                'hl' => 'on',
                'hl.fl' => 'name',
                'spellcheck' => 'true',
            );

            $offset = ($op['page'] - 1) * $op['count']   0;

            $query = 'manufacturer:'.$op['q'];

            $results = $solr->search($query, $offset, $op['count'], $params);

            $output['response'] = $this->object_to_array($results->response);
            $output['responseHeader'] = $this->object_to_array($results->responseHeader);
            $output['facet_counts'] = $this->object_to_array($results->facet_counts);

            $pagination_array = array();
            if(isset($output['response']['numFound'])) {
                $i=1;
                while($i <= $output['response']['numFound']) {
                    $pagination_array[] = $i;
                    $i  ;
                }
            }
        }
        $pagination = Zend_Paginator::factory ( $pagination_array );
        $pagination->setCurrentPageNumber ( $op['page'] );
        $pagination->setItemCountPerPage ( 10 );
        $pagination->setPageRange ( 7 );

        $this->_pagination = $pagination;

        return $output;

    }


    public function returnPagination() {
        return $this->_pagination;
    }

    /**
     * 
     * Get spell
     * @param arr $op
     */
    public function spell($op = array()) {		
        $solr = $this->get_solr_connection();	
        if ($solr->ping ()) {

            $results = $solr->spell($op['q']);
            $results = json_decode($results, true);

            return $results['spellcheck'];
        }
    }

    private function get_solr_connection() {
        return new Zend_Service_Solr ('HOST', 'PORT', 'PATH'); // Configure
    }

    private function object_to_array($object) {
        if(is_array($object) || is_object($object)) {
            $result = array();
            foreach($object as $key => $value) {
                $result[$key] = $this->object_to_array($value);
            }
            return $result;
        }
        return $object;
    }

}

About

Zend_Service_Solr is a port of Solr PHP Client (http://code.google.com/p/solr-php-client) to integrate with Zend Framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages