Jump to content


Photo

Sphinx Search Issues with Cakephp

sphinx search cakephp

Best Answer n1concepts, 01 July 2013 - 05:46 PM

I found the answer: GET RID OF SPHINX and just defined standard 'find(all) search class for form.

Sphinx was overkill and found out buggy for what we wanted to do for this search.

Go to the full post


  • Please log in to reply
1 reply to this topic

#1 n1concepts

n1concepts

    Advanced Member

  • Members
  • PipPipPip
  • 167 posts

Posted 30 June 2013 - 10:34 AM

Hi,

 

Need some help to get Sphinx Search working - framework being used is CakePHP.

Here's the error when clicking the search form on page (I'll post the class code following the error info debugged from CakePHP

----------------------------

Notice (8): Undefined index: Job [APP/Controller/JobsController.php, line 79]
Code Context

$search = object(SphinxClient) {
	_host => 'localhost'
	_port => (int) 9312
	_offset => (int) 0
	_limit => (int) 20
	_mode => (int) 6
	_weights => array()
	_sort => (int) 0
	_sortby => ''
	_min_id => (int) 0
	_max_id => (int) 0
	_filters => array()
	_groupby => ''
	_groupfunc => (int) 0
	_groupsort => '@group desc'
	_groupdistinct => ''
	_maxmatches => (int) 1000
	_cutoff => (int) 0
	_retrycount => (int) 0
	_retrydelay => (int) 0
	_anchor => array()
	_indexweights => array()
	_ranker => (int) 0
	_rankexpr => ''
	_maxquerytime => (int) 0
	_fieldweights => array()
	_overrides => array()
	_select => '*'
	_error => ''
	_warning => ''
	_connerror => false
	_reqs => array()
	_mbenc => ''
	_arrayresult => false
	_timeout => (int) 0
	_path => false
	_socket => false
}

JobsController::search() - APP/Controller/JobsController.php, line 79
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 109

And here's the snippet of code from 'JobsController.php

Q: I see the error stating, 'ReflectionMethod::invokeArgs() - [internal], line ??' but need some guidance on how to isolate the fault and resolve.

To add, I have found some syntax issues as well - I'm fixing those but need some guidance on getting Sphinx query up and running based on stated criteria.

 

FYI: (Line 79 starts w/this line of code: $results = $search->query($this->request->data['Job']['keywords'], 'src2');

<?php

require('Component/SphinxComponent.php');
App::uses('AppController', 'Controller');

/**
 * Jobs Controller
 *
 * @property Job $Job
 */
class JobsController extends AppController {

/**
 * Uses
 *
 * @var array
 */
	// JobTypeType is *not* a typo below
	public $uses = array('User', 'Resume', 'JobType', 'FieldType', 'Job', 'JobTypeType', 'JobFieldType', 'UserField', 'UserFieldType', 'EducationLevel', 'CandidateView', 'EmployerProfile');
	
/**
 * Helpers
 *
 * @var array
 */
	public $helpers = array('Text', 'Js', 'Time', 'Number', 'Func');

/**
 * Components
 *
 * @var array
 */
	public $components = array('Email');

/**
 * beforeFilter callback
 *
 * @return void
 */
	public function beforeFilter() {
		parent::beforeFilter();
		//echo $this->request->here;
		$this->Auth->allow('recent', 'daily', 'industries', 'search', 'limited', 'view');
	}

/**
 * isAuthorized callback
 *
 * @return void
 */
	public function isAuthorized($user) {
		if ($user['Role']['role_name'] == 'Employer') {
			return true;
		} else {
			if ($user['Role']['role_name'] == 'Candidate') {
				if (in_array($this->action, array('search','togglefavorite'))) {
					return true;
				}
			} 
			if (in_array($this->action, array('view'))) {
				return true;
			}
		}
		return true;
	}

/**
 * search method
 * 
 * Takes full-text Sphinx search results and then filters & paginates
 *
 * @return void
 */
	public function search() {
		if ($this->request->is('post')) {
			$search = new SphinxClient();
			$search->setMatchMode(SPH_MATCH_EXTENDED2);
			$results = $search->query($this->request->data['Job']['keywords'], 'src2');
			if (isset($results['matches'])) {
				$conditions['Job.id'] = array_keys($results['matches']);
			}
			
			if (isset($this->request->data['Job']['field_types_id'])) {
				foreach ($this->request->data['Job']['field_types_id'] as $field_type_id) {
// 					$conditions[]['Job.job_field_types LIKE'] = '|%' . $field_type_id . '%|';
					$field_type_conditions[]['Job.job_field_types LIKE'] = '%|' . $field_type_id . '|%';
				}
			}
			if (isset($this->request->data['Job']['job_types_id'])) {
				foreach ($this->request->data['Job']['job_types_id'] as $job_type_id) {
// 					$conditions[]['Job.job_type_types LIKE'] = '|%' . $job_type_id . '%|';
					$job_type_conditions[]['Job.job_type_types LIKE'] = '%|' . $job_type_id . '|%';
				}
			}
			if (is_numeric($this->request->data['Job']['job_salary_type']) 
				&& 
			    is_numeric($this->request->data['Job']['minimum_salary']) 
				&& 
			    is_numeric($this->request->data['Job']['maximum_salary'])) 
			{
				$conditions['Job.job_salary_type'] = $this->request->data['Job']['job_salary_type'];
				$conditions['Job.job_salary BETWEEN ? AND ?'] = array(
					$this->request->data['Job']['minimum_salary'],
					$this->request->data['Job']['maximum_salary']
				);
			}
			if (is_numeric($this->request->data['Job']['minimum_experience'])) {
				$conditions['Job.minimum_experience >='] = $this->request->data['Job']['minimum_experience']; 
			}
			if (isset($this->request->data['Job']['education_levels_id'])) {	
				foreach ($this->request->data['Job']['education_levels_id'] as $education_levels_id) {
					$conditions['OR'][]['Job.education_levels_id'] = $education_levels_id;
				}	
			}
			if (isset($this->request->data['Job']['date_posted'])) {
				switch ($this->request->data['Job']['date_posted']) {
					// Posted Today
					case 'today':
						$conditions['Job.modified BETWEEN ? AND ?'] = array(
							date('Y-m-d').' 00:00:00',
							date('Y-m-d').' 23:59:59',
						);
					break;
					// Posted This Week
					case 'week':
						$monday = intval(date('d')) - intval(date('N')) + 1;
						$conditions['Job.modified BETWEEN ? AND ?'] = array(
							date('Y-m-').$monday.' 00:00:00',
							date('Y-m-d H:i:s')							
						);
					break;
					// Posted This Month 
					case 'month':
						$conditions['Job.modified BETWEEN ? AND ?'] = array(
							date('Y-m-1').' 00:00:00',
							date('Y-m-d H:i:s')							
						);
					break;
					// Posted Any
					case '':
						$conditions['Job.modified NOT'] = null;
					break;
					
				}
			}
			$conditions[]['Job.deleted'] = null;
		} else {
			$conditions[]['Job.deleted'] = null;
		}
		if (isset($field_type_conditions)) {
			if (isset($job_type_conditions)) {
				$final_conditions = array(
						'AND' => array(
								array('OR' => $field_type_conditions),
								array('OR' => $job_type_conditions),
								array('AND'=>$conditions)
						)
				);
			} else {
				$final_conditions = array(
						'AND' => array(
								array('OR' => $field_type_conditions),
								array('AND'=>$conditions)
						)
				);
			}
		} else {
			if (isset($job_type_conditions)) {
				$final_conditions = array(
						'AND' => array(
								array('OR' => $job_type_conditions),
								array('AND'=>$conditions)
						)
				);
			} else {
				$final_conditions = array(
						'AND' => array(
								array('AND'=>$conditions)
						)
				);
			}
		}
		
		$this->Job->recursive = 0;
		$this->paginate = array(
			'limit'=>10,
			'conditions' => $final_conditions,
			'order' => array('Job.modified DESC')
		);
		$jobs = $this->paginate('Job');
		$fieldTypes = $this->FieldType->find('list');
		$jobTypes = $this->JobType->find('list');
		$empRaw = $this->EmployerProfile->find('all', ['fields'=>['users_id','company_name']]);
		foreach ($empRaw as $emp) {
			$empList[$emp['EmployerProfile']['users_id']] = $emp['EmployerProfile']['company_name']; 
		}
		$educationLevels = $this->EducationLevel->find('list');
		foreach ($jobs as &$job) {
		}
		$this->set('jobs', $jobs);
		$this->set(compact('fieldTypes', 'jobTypes', 'educationLevels', 'empList', 'final_conditions'));
	}




#2 n1concepts

n1concepts

    Advanced Member

  • Members
  • PipPipPip
  • 167 posts

Posted 01 July 2013 - 05:46 PM   Best Answer

I found the answer: GET RID OF SPHINX and just defined standard 'find(all) search class for form.

Sphinx was overkill and found out buggy for what we wanted to do for this search.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com