/** * Run the search based on the specified search string. * * @param string $string The string to run the search on. * @param int $limit The number of results to limit to. * @param int $start The starting result index to search from. * @param array $conditions An array of conditions to add to the search filter. * @return array An array of search results. */ public function run($string, $limit = 10, $start = 0, array $conditions = array()) { /* sanitize string */ $string = str_replace(array('!'), '', $string); /* @var SolrQuery $query */ $query = new SolrQuery(); $query->setQuery($string); $query->setStart($start); $query->setRows($limit); // turn board array into solr-compatible OR argument if (isset($conditions['board']) && is_array($conditions['board'])) { $c = array(); foreach ($conditions['board'] as $board) { $c[] = $board['id']; } $conditions['board'] = '(' . implode(' OR ', $c) . ')'; } // @todo rectify this workaround // convert author (id) lookup to username (name) lookup if (isset($conditions['author']) && isset($_REQUEST['user'])) { unset($conditions['author']); $conditions['username'] = trim($_REQUEST['user']); } // allow for non-default Solr requestHandler if (isset($this->_searchOptions['requestHandler']) && !empty($this->_searchOptions['requestHandler'])) { $this->client->setServlet(SolrClient::SEARCH_SERVLET_TYPE, $this->_searchOptions['requestHandler']); } else { $query->addField('id')->addField('title')->addField('message')->addField('thread')->addField('board')->addField('category')->addField('author')->addField('username')->addField('replies')->addField('createdon')->addField('board_name')->addField('url')->addField('private'); } foreach ($conditions as $k => $v) { $query->addFilterQuery($k . ':' . $v); } $response = array('total' => 0, 'start' => $start, 'limit' => $limit, 'status' => 0, 'query_time' => 0, 'results' => array()); try { $queryResponse = $this->client->query($query); $responseObject = $queryResponse->getResponse(); if ($responseObject) { $response['total'] = $responseObject->response->numFound; $response['query_time'] = $responseObject->responseHeader->QTime; $response['status'] = $responseObject->responseHeader->status; $response['results'] = array(); if (!empty($responseObject->response->docs)) { foreach ($responseObject->response->docs as $doc) { $d = array(); foreach ($doc as $k => $v) { if ($k == 'createdon') { $v = strftime($this->discuss->dateFormat, strtotime($v)); } $d[$k] = $v; } $response['results'][] = $d; } } } } catch (Exception $e) { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error running query on Solr server: ' . $e->getMessage()); } return $response; }