/** * 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(); } }
<?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";
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(); }
function query_direct($args) { $solr = $this->solr->get($this->core); switch ($this->method) { case 'add': isset($args['_documents']) or backend_error('bad_query', 'Solr _documents argument missing'); $docs = []; foreach ($args['_documents'] as $name => $value) { if (is_array($value)) { if ($value === array_values($value)) { foreach ($value as $document) { $docs[] = self::add($document); } } else { $docs[] = self::add($value); } } else { if (is_object($value)) { $docs[] = self::add(get_object_vars($value)); } else { backend_error('bad_args', 'Bad Solr document: ' . $name); } } } $solr->addDocuments($docs); $solr->request("<commit/>"); break; case 'delete': $solr->deleteByQuery(self::substitute($this->body, $args)); $solr->request("<commit/>"); break; case 'query': $query = new SolrQuery(self::substitute($this->body, $args)); foreach ($this->order_by as $name => $mode) { if ($mode->type == 'normal') { $query->addSortField($name, $mode->order == 'desc' ? SolrQuery::ORDER_DESC : SolrQuery::ORDER_ASC); } elseif ($mode->type == 'spatial') { $query->setParam('spatial', true); $query->setParam('sfield', $name); if (isset($mode->point)) { $query->setParam('pt', $args[$mode->point]); } $query->setParam('sort', self::substitute($mode->order, $args)); } } $offset = isset($args['_offset']) ? $args['_offset'] : $this->offset; $count = isset($args['_count']) ? $args['_count'] : $this->count; is_null($offset) or $query->setStart($offset); is_null($count) or $query->setRows($count); if (isset($args['_fields']) or isset($args['_queries'])) { $query->setFacet(true); if (isset($args['_fields'])) { foreach ($args['_fields'] as $field) { $query->addFacetField($field); } } if (isset($args['_queries'])) { foreach ($args['_queries'] as $fq) { $query->addFacetQuery($fq); } } } $result = new stdClass(); $result->matched = 0; $result->documents = []; $result->fields = new stdClass(); $result->queries = new stdClass(); $response = $solr->query($query); $object = $response->getResponse(); if (is_array($object['response']['docs'])) { $result->matched = $object['response']['numFound']; foreach ($object['response']['docs'] as $doc) { $document = new stdClass(); foreach ($doc as $name => $value) { if ($name != '_version_') { if (is_array($value)) { $items = []; foreach ($value as $item) { $items[] = $item; } $document->{$name} = $items; } else { $document->{$name} = $value; } } } $result->documents[] = $document; } } else { !$this->required or backend_error('bad_input', 'Empty response from Solr procedure'); } if (isset($args['_fields'])) { foreach ($object['facet_counts']['facet_fields'] as $name => $counts) { $array = []; foreach ($counts as $value => $count) { $array[$value] = $count; } $result->fields->{$name} = (object) $array; } } if (isset($args['_queries'])) { foreach ($object['facet_counts']['facet_queries'] as $fq => $count) { $result->queries->{$fq} = $count; } } switch ($this->result) { case 'array': return empty($result) ? (object) ['matched' => 0, 'documents' => [], 'fields' => (object) null, 'queries' => (object) null] : $result; case 'object': if ($result->matched == 1 and count($result->documents) == 1) { return $result->documents[0]; } elseif ($result->matched == 0 and count($result->documents) == 0) { return null; } else { backend_error('bad_query', 'Solr query result is not an object'); } default: backend_error('bad_query', 'Unsupported Solr query result type: ' . $this->result); } } }