/** * 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)); }
/** * Simple Search suggestions interface * * @deprecated after 1.9.0.0 - integrated into $this->_search() * * @param string $query The raw query string * @param array $params * @param int|bool $limit * @param bool $withResultsCounts * @return array */ public function _searchSuggestions($query, $params = array(), $limit = false, $withResultsCounts = false) { /** * @see self::_search() */ if ((int) ('1' . str_replace('.', '', solr_get_version())) < 1099) { $this->_connect(); } $query = $this->_escapePhrase($query); if (!$query) { return false; } $_params = array(); $languageSuffix = $this->_getLanguageSuffix($params['locale_code']); $solrQuery = new SolrQuery($query); $_params['solr_params'] = array('spellcheck' => 'true', 'spellcheck.collate' => 'true', 'spellcheck.dictionary' => 'magento_spell' . $languageSuffix, 'spellcheck.extendedResults' => 'true', 'spellcheck.count' => $limit ? $limit : 1); /** * Specific Solr params */ if (!empty($_params['solr_params'])) { foreach ($_params['solr_params'] as $name => $value) { $solrQuery->setParam($name, $value); } } $this->_client->setServlet(SolrClient::SEARCH_SERVLET_TYPE, 'spell'); /** * Store filtering */ if (!empty($params['store_id'])) { $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); $result = $this->_prepareSuggestionsQueryResponse($response->getResponse()); $resultLimit = array(); // Calc results count for each suggestion if ($withResultsCounts && $limit) { $tmp = $this->_lastNumFound; //Temporary store value for main search query $this->_lastNumFound = 0; foreach ($result as $key => $item) { $this->search($item['word'], $params); if ($this->_lastNumFound) { $result[$key]['num_results'] = $this->_lastNumFound; $resultLimit[] = $result[$key]; $limit--; } if ($limit <= 0) { break; } } $this->_lastNumFound = $tmp; //Revert store value for main search query } else { $resultLimit = array_slice($result, 0, $limit); } return $resultLimit; } catch (Exception $e) { Mage::logException($e); return array(); } }
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 //-- }
/** * 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; }
/** * Выполнить поиск * * @param $query * @param array $params * @return CSolrQueryResults */ public static function search($query, $params = array()) { $solrQuery = new SolrQuery(); $solrQuery->setQuery("doc_body:*" . $query . "*"); if (mb_strpos($query, " ") !== false) { $solrQuery->setQuery('doc_body:"*' . $query . '*"'); } foreach ($params as $key => $value) { if ($key == "_highlight_") { $solrQuery->addHighlightField($value); $solrQuery->setHighlight(true); $solrQuery->setHighlightSimplePre("<em>"); $solrQuery->setHighlightSimplePost("</em>"); } elseif (is_array($value)) { $solrQuery->addFilterQuery($key . ":" . implode(",", $value)); } else { $solrQuery->addFilterQuery($key . ":" . $value); } } try { $query_response = self::getClient()->query($solrQuery); } catch (Exception $e) { echo $e->getMessage(); var_dump(self::getClient()->getDebug()); break; } $response = $query_response->getResponse(); $result = new CSolrQueryResults($response); return $result; }
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";