function getResults($userQuery) { $core = 'techknowledgy_core'; $options = array('hostname' => 'localhost', 'port' => 8983, 'timeout' => 10, 'path' => '/solr/' . $core); $client = new SolrClient($options); #if (!$client->ping()) { # exit('Solr service not responding.'); #} #else{ # print "Worked!"; #} $query = new SolrQuery(); $query->setQuery($userQuery); $query->setStart(0); $query->setRows(1000); $query->addField('url')->addField('title')->addField('host')->addField('content'); $query_response = $client->query($query); $response = $query_response->getResponse(); #print_r($response); return $response; }
/** * Simple Search interface * * @param string|array $query The raw query string * @param array $params Params can be specified like this: * 'offset' - The starting offset for result documents * 'limit - The maximum number of result documents to return * 'sort_by' - Sort field, can be just sort field name (and asceding order will be used by default), * or can be an array of arrays like this: array('sort_field_name' => 'asc|desc') * to define sort order and sorting fields. * If sort order not asc|desc - asceding will used by default * 'fields' - Fields names which are need to be retrieved from found documents * 'solr_params' - Key / value pairs for other query parameters (see Solr documentation), * use arrays for parameter keys used more than once (e.g. facet.field) * 'locale_code' - Locale code, it used to define what suffix is needed for text fields, * by which will be performed search request and sorting * 'ignore_handler' - Flag that allows to ignore handler (qt) and make multifield search * * @return array */ protected function _search($query, $params = array()) { /** * Hard code to prevent Solr bug: * Bug #17009 Creating two SolrQuery objects leads to wrong query value * @link http://pecl.php.net/bugs/bug.php?id=17009&edit=1 * @link http://svn.php.net/viewvc?view=revision&revision=293379 */ if ((int) ('1' . str_replace('.', '', solr_get_version())) < 1099) { $this->_connect(); } $searchConditions = $this->prepareSearchConditions($query); if (!$searchConditions) { return array(); } $_params = $this->_defaultQueryParams; if (is_array($params) && !empty($params)) { $_params = array_intersect_key($params, $_params) + array_diff_key($_params, $params); } $offset = isset($_params['offset']) ? (int) $_params['offset'] : 0; $limit = isset($_params['limit']) ? (int) $_params['limit'] : Enterprise_Search_Model_Adapter_Solr_Abstract::DEFAULT_ROWS_LIMIT; /** * Now supported search only in fulltext field * By default in Solr set <defaultSearchField> is "fulltext" * When language fields need to be used, then perform search in appropriate field */ $languageSuffix = $this->_getLanguageSuffix($params['locale_code']); $solrQuery = new SolrQuery(); $solrQuery->setStart($offset)->setRows($limit); $solrQuery->setQuery($searchConditions); if (!is_array($_params['fields'])) { $_params['fields'] = array($_params['fields']); } if (!is_array($_params['solr_params'])) { $_params['solr_params'] = array($_params['solr_params']); } /** * Add sort fields */ if ($limit > 1) { $sortFields = $this->_prepareSortFields($_params['sort_by']); foreach ($sortFields as $sortField) { $sortField['sortType'] = $sortField['sortType'] == 'desc' ? SolrQuery::ORDER_DESC : SolrQuery::ORDER_ASC; $solrQuery->addSortField($sortField['sortField'], $sortField['sortType']); } } /** * Fields to retrieve */ if ($limit && !empty($_params['fields'])) { foreach ($_params['fields'] as $field) { $solrQuery->addField($field); } } /** * Now supported search only in fulltext and name fields based on dismax requestHandler (named as magento_lng). * Using dismax requestHandler for each language make matches in name field * are much more significant than matches in fulltext field. */ if ($_params['ignore_handler'] !== true) { $_params['solr_params']['qt'] = 'magento' . $languageSuffix; } /** * Facets search */ $useFacetSearch = isset($params['solr_params']['facet']) && $params['solr_params']['facet'] == 'on'; if ($useFacetSearch) { $_params['solr_params'] += $this->_prepareFacetConditions($params['facet']); } /** * Suggestions search */ $useSpellcheckSearch = isset($params['solr_params']['spellcheck']) && $params['solr_params']['spellcheck'] == 'true'; if ($useSpellcheckSearch) { if (isset($params['solr_params']['spellcheck.count']) && (int) $params['solr_params']['spellcheck.count'] > 0) { $spellcheckCount = (int) $params['solr_params']['spellcheck.count']; } else { $spellcheckCount = self::DEFAULT_SPELLCHECK_COUNT; } $_params['solr_params'] += array('spellcheck.collate' => 'true', 'spellcheck.dictionary' => 'magento_spell' . $languageSuffix, 'spellcheck.extendedResults' => 'true', 'spellcheck.count' => $spellcheckCount); } /** * Specific Solr params */ if (!empty($_params['solr_params'])) { foreach ($_params['solr_params'] as $name => $value) { if (is_array($value)) { foreach ($value as $val) { $solrQuery->addParam($name, $val); } } else { $solrQuery->addParam($name, $value); } } } $filtersConditions = $this->_prepareFilters($_params['filters']); foreach ($filtersConditions as $condition) { $solrQuery->addFilterQuery($condition); } $this->_client->setServlet(SolrClient::SEARCH_SERVLET_TYPE, 'select'); /** * Store filtering */ if ($_params['store_id'] > 0) { $solrQuery->addFilterQuery('store_id:' . $_params['store_id']); } if (!Mage::helper('cataloginventory')->isShowOutOfStock()) { $solrQuery->addFilterQuery('in_stock:true'); } try { $this->ping(); $response = $this->_client->query($solrQuery); $data = $response->getResponse(); if (!isset($params['solr_params']['stats']) || $params['solr_params']['stats'] != 'true') { if ($limit > 0) { $result = array('ids' => $this->_prepareQueryResponse($data)); } /** * Extract facet search results */ if ($useFacetSearch) { $result['faceted_data'] = $this->_prepareFacetsQueryResponse($data); } /** * Extract suggestions search results */ if ($useSpellcheckSearch) { $resultSuggestions = $this->_prepareSuggestionsQueryResponse($data); /* Calc results count for each suggestion */ if (isset($params['spellcheck_result_counts']) && $params['spellcheck_result_counts'] == true && count($resultSuggestions) && $spellcheckCount > 0) { /* Temporary store value for main search query */ $tmpLastNumFound = $this->_lastNumFound; unset($params['solr_params']['spellcheck']); unset($params['solr_params']['spellcheck.count']); unset($params['spellcheck_result_counts']); $suggestions = array(); foreach ($resultSuggestions as $key => $item) { $this->_lastNumFound = 0; $this->search($item['word'], $params); if ($this->_lastNumFound) { $resultSuggestions[$key]['num_results'] = $this->_lastNumFound; $suggestions[] = $resultSuggestions[$key]; $spellcheckCount--; } if ($spellcheckCount <= 0) { break; } } /* Return store value for main search query */ $this->_lastNumFound = $tmpLastNumFound; } else { $suggestions = array_slice($resultSuggestions, 0, $spellcheckCount); } $result['suggestions_data'] = $suggestions; } } else { $result = $this->_prepateStatsQueryResponce($data); } return $result; } catch (Exception $e) { Mage::logException($e); } }
<?php include "bootstrap.php"; $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH); $client = new SolrClient($options); $query = new SolrQuery(); $query->setQuery('manu:"Apple Computer Inc." OR text:apple'); $query->setStart(0); $query->setRows(50); $query->addField('cat')->addField('features')->addField('id')->addField('timestamp'); $query_response = $client->query($query); $response = $query_response->getResponse(); print_r($response);
/** * Get the currently indexed files for a particular document, returns the total count, and a subset of files. * * @param document $document * @param int $start The row to start the results on. Zero indexed. * @param int $rows The number of rows to fetch * @return array A two element array, the first is the total number of availble results, the second is an array * of documents for the current request. */ protected function get_indexed_files($document, $start = 0, $rows = 500) { // Build a custom query that will get any document files that are in our solr_filegroupingid. $query = new \SolrQuery(); // We want to get all file records tied to a document. // For efficiency, we are building our own, stripped down, query. $query->setQuery('*'); $query->setRows($rows); $query->setStart($start); // We want a consistent sorting. $query->addSortField('id'); // We only want the bare minimum of fields. $query->addField('id'); $query->addField('modified'); $query->addField('title'); $query->addField('solr_fileid'); $query->addField('solr_filecontenthash'); $query->addField('solr_fileindexstatus'); $query->addFilterQuery('{!cache=false}solr_filegroupingid:(' . $document->get('id') . ')'); $query->addFilterQuery('type:' . \core_search\manager::TYPE_FILE); $response = $this->get_query_response($query); if (empty($response->response->numFound)) { return array(0, array()); } return array($response->response->numFound, $this->convert_file_results($response)); }
/** * Sets fields to be returned in the result. * * @param SolrQuery $query object. */ public function add_fields($query) { $documentclass = $this->get_document_classname(); $fields = array_keys($documentclass::get_default_fields_definition()); foreach ($fields as $field) { $query->addField($field); } }
(or <i>fink selfupdate-cvs</i> if you can't use rsync for some reason). </p> <p>Help is also needed to find new maintainers for the <a href="<?php echo $pdbroot; ?> browse.php?maintainer=None&nochildren=on">packages without maintainers</a>.</p> <? $pkgcount = memcache_get_key('pdb-package-count'); if (!$pkgcount) { $names = array(); $q = new SolrQuery(); $q->addQuery("dist_visible:true", true); $q->addField("name_e"); $q->setUnique(true); $r = $q->fetch(); if ($r != null) { $pkgcount = count($r); if ($pkgcount > 0) { memcache_set_key('pdb-package-count', $pkgcount, false, $cache_timeout); } } } $dyndate = date_create($update_date)->format('U'); $seccount = count($sections); ?>
<?php $options = array('hostname' => 'localhost', 'port' => '8983', 'path' => 'solr/design_template', 'wt' => 'json'); $client = new SolrClient($options); //$var=$client->getOptions(); //print_r($var); //die; $query = new SolrQuery(); $keyword = $_GET['keyword']; $keyword = $keyword . "*"; //print "gettype($keyword)"; $keyword1 = "first_product_name:(" . $keyword . ")"; $query->setQuery($keyword1); $query->setStart(0); $query->setRows(1000); $query->addField('first_product_name'); $query_response = $client->query($query); $response = $query_response->getResponse(); $arr = array(); /*for($i =1; $i<sizeof($response["response"]["docs"]) ;$i++){ if($response["response"]["docs"][$i]["first_product_name"]==$response["response"]["docs"][$i-1]["first_product_name"]){ echo "dfasdfasdf\n"; } else $arr[$i]=$response["response"]["docs"][$i]["first_product_name"]; }*/ function search($arr, $string) { for ($i = 0; $i < sizeof($arr); $i++) {
/** * Run the search against a sanitized query string via Solr. * * @param string $string * @param array $scriptProperties The scriptProperties array from the SimpleSearch snippet * @return array */ public function search($string, array $scriptProperties = array()) { /** @var SolrQuery $query */ $query = new SolrQuery(); $query->setQuery($string); /* set limit */ $perPage = $this->modx->getOption('perPage', $scriptProperties, 10); if (!empty($perPage)) { $offset = $this->modx->getOption('start', $scriptProperties, 0); $offsetIndex = $this->modx->getOption('offsetIndex', $scriptProperties, 'sisea_offset'); if (isset($_REQUEST[$offsetIndex])) { $offset = (int) $_REQUEST[$offsetIndex]; } $query->setStart($offset); $query->setRows($perPage); } /* add fields to search */ $fields = $this->modx->getFields('modResource'); foreach ($fields as $fieldName => $default) { $query->addField($fieldName); } $includeTVs = $this->modx->getOption('includeTVs', $scriptProperties, false); $includeTVList = $this->modx->getOption('includeTVList', $scriptProperties, ''); if ($includeTVs) { $sql = $this->modx->newQuery('modTemplateVar'); $sql->select($this->modx->getSelectColumns('modTemplateVar', '', '', array('id', 'name'))); if (!empty($includeTVList)) { $includeTVList = explode(',', $includeTVList); $includeTVList = array_map('trim', $includeTVList); $sql->where(array('name:IN' => $includeTVList)); } $sql->sortby($this->modx->escape('name'), 'ASC'); $sql->prepare(); $sql = $sql->toSql(); $tvs = $this->modx->query($sql); if ($tvs && $tvs instanceof PDOStatement) { while ($tv = $tvs->fetch(PDO::FETCH_ASSOC)) { $query->addField($tv['name']); } } } /* handle hidemenu option */ $hideMenu = $this->modx->getOption('hideMenu', $scriptProperties, 2); if ($hideMenu != 2) { $query->addFilterQuery('hidemenu:' . ($hideMenu ? 1 : 0)); } /* handle contexts */ $contexts = $this->modx->getOption('contexts', $scriptProperties, ''); $contexts = !empty($contexts) ? $contexts : $this->modx->context->get('key'); $contexts = implode(' ', explode(',', $contexts)); $query->addFilterQuery('context_key:(' . $contexts . ')'); /* handle restrict search to these IDs */ $ids = $this->modx->getOption('ids', $scriptProperties, ''); if (!empty($ids)) { $idType = $this->modx->getOption('idType', $this->config, 'parents'); $depth = $this->modx->getOption('depth', $this->config, 10); $ids = $this->processIds($ids, $idType, $depth); $query->addFilterQuery('id:(' . implode(' ', $ids) . ')'); } /* handle exclude IDs from search */ $exclude = $this->modx->getOption('exclude', $scriptProperties, ''); if (!empty($exclude)) { $exclude = $this->cleanIds($exclude); $exclude = implode(' ', explode(',', $exclude)); $query->addFilterQuery('-id:(' . $exclude . ')'); } /* basic always-on conditions */ $query->addFilterQuery('published:1'); $query->addFilterQuery('searchable:1'); $query->addFilterQuery('deleted:0'); /* sorting */ if (!empty($scriptProperties['sortBy'])) { $sortDir = $this->modx->getOption('sortDir', $scriptProperties, 'desc'); $sortDirs = explode(',', $sortDir); $sortBys = explode(',', $scriptProperties['sortBy']); $dir = 'desc'; for ($i = 0; $i < count($sortBys); $i++) { if (isset($sortDirs[$i])) { $dir = $sortDirs[$i]; } $dir = strtolower($dir) == 'asc' ? SolrQuery::ORDER_ASC : SolrQuery::ORDER_DESC; $query->addSortField($sortBys[$i], $dir); } } /* prepare response array */ $response = array('total' => 0, 'start' => !empty($offset) ? $offset : 0, 'limit' => $perPage, 'status' => 0, 'query_time' => 0, 'results' => array()); /* query Solr */ 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) { $d[$k] = $v; } /** @var modResource $resource */ $resource = $this->modx->newObject($d['class_key']); if ($resource->checkPolicy('list')) { $response['results'][] = $d; } } } } } catch (Exception $e) { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error running query on Solr server: ' . $e->getMessage()); } return $response; }
public function findQuery($y_query, $y_options = array('settings' => array(), 'sort' => array(), 'filters' => array(), 'facets' => array(), 'fields' => array())) { //-- $connect = $this->solr_connect(); //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { //-- SmartFrameworkRegistry::setDebugMsg('db', 'solr|total-queries', 1, '+'); //-- $time_start = microtime(true); //-- } //end if //-- $query = new SolrQuery(); $query->setQuery(SolrUtils::escapeQueryChars("'" . $y_query) . "'"); if (Smart::array_size($y_options['settings']) > 0) { foreach ($y_options['settings'] as $key => $val) { $method = ucfirst(strtolower($key)); $query->{'set' . $method}($val); } //end for } //end if if (Smart::array_size($y_options['sort']) > 0) { foreach ($y_options['sort'] as $key => $val) { //echo 'Sort by: '.$key.' / '.$val.'<br>'; $query->addSortField($key, $val); } //end for } //end if if (Smart::array_size($y_options['filters']) > 0) { foreach ($y_options['filters'] as $key => $val) { //echo 'Filter Query: '.$key.' / '.$val.'<br>'; $query->addFilterQuery($key . ':"' . SolrUtils::escapeQueryChars($val) . '"'); } //end for } //end if $have_facets = false; if (Smart::array_size($y_options['facets']) > 0) { $have_facets = true; $query->setFacet(true); $query->setFacetMinCount(1); for ($i = 0; $i < Smart::array_size($y_options['facets']); $i++) { $query->addFacetField((string) $y_options['facets'][$i]); } //end for } //end if if (Smart::array_size($y_options['fields']) > 0) { for ($i = 0; $i < Smart::array_size($y_options['fields']); $i++) { $query->addField((string) $y_options['fields'][$i]); } //end for } //end if try { //-- $response = $this->instance->query($query); //-- } catch (Exception $e) { //-- Smart::log_warning('Solr ERROR # Query # EXCEPTION: ' . $e->getMessage() . "\n" . 'Query=' . print_r($query, 1)); return array(); // not connected //-- } //end try catch $response->setParseMode(SolrResponse::PARSE_SOLR_DOC); $data = $response->getResponse(); //print_r($data); //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { //-- $time_end = (double) (microtime(true) - (double) $time_start); //-- SmartFrameworkRegistry::setDebugMsg('db', 'solr|total-time', $time_end, '+'); //-- SmartFrameworkRegistry::setDebugMsg('db', 'solr|log', ['type' => 'nosql', 'data' => 'FIND-QUERY: ' . $y_query, 'command' => $y_options, 'time' => Smart::format_number_dec($time_end, 9, '.', '')]); //-- } //end if //-- if (!is_object($data)) { Smart::log_warning('Solr Query: Invalid Object Result'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if if ($data instanceof SolrObject !== true) { Smart::log_warning('Solr Query: Invalid Object Instance Result'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if //-- if (!is_object($data['responseHeader'])) { Smart::log_warning('Solr Query: Invalid Object ResponseHeader'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if if ($data['responseHeader'] instanceof SolrObject !== true) { Smart::log_warning('Solr Query: Invalid Object Instance ResponseHeader'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if //-- if ((string) $data['responseHeader']->status != '0') { Smart::log_warning('Solr Query: Invalid Status Result'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if //-- if (!is_object($data['response'])) { Smart::log_warning('Solr Query: Invalid Object Response'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if if ($data['response'] instanceof SolrObject !== true) { Smart::log_warning('Solr Query: Invalid Object Instance Response'); return array(); // connection errors will be threated silently as Solr is a remote service } //end if //-- if (!is_array($data['response']->docs)) { $this->error('Solr Query', 'Invalid Response Document Format', $y_query); return array(); } //end if //-- if ($have_facets and is_object($data['facet_counts'])) { return array('header' => $data['responseHeader'], 'total' => (int) $data['response']->numFound, 'docs' => (array) $data['response']->docs, 'facets' => (array) $data['facet_counts']->facet_fields); } else { return array('header' => $data['responseHeader'], 'total' => (int) $data['response']->numFound, 'docs' => (array) $data['response']->docs); } //end if else //-- }
private function find_response() { if (!isset($this->query)) { throw new SolrException('SolrAccessor Exception: query undefined', '7700'); } $query = new SolrQuery(); $query->setQuery($this->query); $query->addField($this->mapping['key']); if (is_array($this->fields)) { foreach ($this->fields as $field) { $query->addField($field); } } if (is_array($this->filters)) { foreach ($this->filters as $field => $value) { $query->addFilterQuery("{$field}:{$value}"); } } if (is_array($this->sorts)) { foreach ($this->sorts as $field => $type) { $query->addSortField($field, $type); } } if (is_array($this->query_field) && !empty($this->query_field)) { $arrQueryFields = array(); foreach ($this->query_field as $field => $boost) { $arrQueryFields[] = $field . '^' . $boost; } $query->setParam('qf', implode(' ', $arrQueryFields)); $query->setParam('qt', 'dismax'); } if (isset($this->start)) { $query->setStart($this->start); } if (isset($this->rows)) { $query->setRows($this->rows); } if (is_array($this->facets)) { $query->setFacet(true); $query->setFacetMinCount($this->facet_min_count); foreach ($this->facets as $facet) { $query->addFacetField($facet['filed']); if (isset($facet['limit'])) { $limit = intval($facet['limit']); $query->setFacetLimit($limit); } if (isset($facet['sort'])) { $sort = $facet['sort'] == 'index' ? $query::FACET_SORT_INDEX : $query::FACET_SORT_COUNT; $query->setFacetSort($sort, $facet['filed']); } } } if (!empty($this->facet_query_array) && is_array($this->facet_query_array)) { $query->setFacet(true); $query->setFacetMinCount($this->facet_min_count); foreach ($this->facet_query_array as $valFacetQuery) { $query->addFacetQuery($valFacetQuery); } } $client = MPF_Solr_Factory::get_instance()->get_client($this->mapping['instance']); $response = $client->query($query); $debug = explode("\n", $client->getDebug()); //TODO use getRawRequest instead of debug $raw_request = @$debug[13]; if (substr($raw_request, 0, 2) != 'q=') { $raw_request = @$debug[12]; } MPF::get_instance()->debug(__CLASS__ . '::' . __FUNCTION__ . ':' . @$response->getRequestUrl() . '&' . $raw_request); return $response->getResponse(); }
public static function buildStringCondition(&$conditions, &$values, $field, $param, $model, $join = array(null, null)) { $has_result = false; $has_id = false; $nb_result = 0; $join_result = null; $param = urldecode($param); if (strlen($param) > 2) { if (sfConfig::get('app_solr_enable') == true) { /* init solr */ $max_row = sfConfig::get('app_solr_maxrow'); $options = array('hostname' => sfConfig::get('app_solr_host'), 'port' => sfConfig::get('app_solr_port'), 'path' => sfConfig::get('app_solr_path'), 'timeout' => sfConfig::get('app_solr_timeout')); $client = new SolrClient($options); try { // 1st search : exact search $query_solr_exact = new SolrQuery(); $query_solr_exact->setQuery('*' . $param . '*'); $query_solr_exact->setRows($max_row); if ($model == 'User' || $model == 'UserPrivateData') { if (!sfContext::getInstance()->getUser()->isConnected()) { $query_solr_exact->addFilterQuery('user_private_public:true'); } } $query_solr_exact->addFilterQuery('module:' . strtolower($model) . 's'); $query_solr_exact->addField('name')->addField('module')->addField('id_doc'); $res_exact = $client->query($query_solr_exact)->getResponse(); if ($res_exact['response']['numFound'] > 0) { for ($i = 0; $i < $res_exact['response']['numFound']; $i++) { $ids_tmp[]['id'] = $res_exact['response']['docs'][$i]['id_doc']; } } else { // No exact serach ... so try fuzzy search $query_solr = new SolrQuery(); // Fuzzy search word > 3 letters $query_words = explode(" ", $param); foreach ($query_words as &$word) { switch (true) { case in_array(strlen($word), range(0, 3)): $word = $word; break; case in_array(strlen($word), range(4, 5)): $word = $word . '~1'; break; case in_array(strlen($word), range(6, 7)): $word = $word . '~2'; break; case in_array(strlen($word), range(8, 9)): $word = $word . '~3'; break; default: $word = $word . '~4'; break; } } $query_search_fuzzy = implode(' ', $query_words); $query_search = "({$param})^20 OR ({$query_search_fuzzy})^5"; c2cTools::log(" solr request : " . $query_search); $query_solr->setQuery($query_search); $query_solr->setRows($max_row); if ($model == 'User' || $model == 'UserPrivateData') { if (!sfContext::getInstance()->getUser()->isConnected()) { $query_solr->addFilterQuery('user_private_public:true'); } } $query_solr->addFilterQuery('module:' . strtolower($model) . 's'); $query_solr->addField('name')->addField('module')->addField('id_doc'); $res = $client->query($query_solr)->getResponse(); for ($i = 0; $i < $res['response']['numFound']; $i++) { $ids_tmp[]['id'] = $res['response']['docs'][$i]['id_doc']; } } } catch (Exception $e) { c2cTools::log(" exception solr : " . $e); $ids_tmp = self::idSearchByName($param, $model); } } else { $ids_tmp = self::idSearchByName($param, $model); } if (count($ids_tmp)) { $ids = array(); foreach ($ids_tmp as $id) { $ids[] = $id['id']; } $conditions[] = $field[0] . ' IN (' . implode(',', $ids) . ')'; $has_result = true; $nb_result = count($ids); $join_result = $join[0]; if (!$join[0]) { $has_id = true; } } } else { $conditions[] = $field[1] . ' LIKE make_search_name(?)||\'%\''; $values[] = $param; $has_result = true; if ($join[1]) { $join_result = $join[1]; } } return array('has_result' => $has_result, 'has_id' => $has_id, 'nb_result' => $nb_result, 'join' => $join_result); }
/** * 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; }
<?php $query = new SolrQuery(); $query->setParam('a', 1); $query->setParam('b', 2); $query->setParam('c', 3); $query->setStart(4)->setQuery('solr')->setTimeAllowed(500)->setRows(17); $query->addField('israel')->addField('joshua')->addField('june'); $query->addSortField('cat', 0); $query->addFilterQuery('solr')->addFilterQuery('solr1')->addFilterQuery('solr2'); echo $query; echo "\n";