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 //-- }
<?php include "bootstrap.php"; $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT); $client = new SolrClient($options); $query = new SolrQuery(); $userInput = 'USB/2'; $escapedUserInput = SolrUtils::escapeQueryChars($userInput); $query->setQuery('text:' . $escapedUserInput); echo $query . PHP_EOL;
/** * Escape a string and remove solr special characters * @param string $string the string to escape * @return string the escaped string */ public function escape($string) { return SolrUtils::escapeQueryChars($string); }