<?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->setFacet(true); $query->addFacetField('cat')->addFacetField('name')->setFacetMinCount(2); $query->setFacetMinCount(4, 'name'); $updateResponse = $client->query($query); $response_array = $updateResponse->getResponse(); $facet_data = $response_array->facet_counts->facet_fields; print_r($facet_data);
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 function facet_test() { $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'port' => SOLR_SERVER_PORT); $client = new SolrClient($options); $query = new SolrQuery('*'); //$query->setQuery('input_datetime:[2010-01-01T00:00:00Z TO 2010-06-01T00:00:00Z]'); $query->setQuery('input_datetime:[* TO 2010-06-01T00:00:00Z]'); $query->setQuery('order_status:9'); $query->setFacetSort(SolrQuery::FACET_SORT_COUNT); $query->setFacet(TRUE); $query->setFacetLimit(200); $query->addFacetField('country'); //$query->setFacetMinCount(2); $query->setFacetMinCount(1, 'country'); $query->setFacetOffset(0); //$query->setFacetDateStart('2012-01-01T00:00:00:Z', 'input_date'); //$query->setFacetPrefix('c'); $updateResponse = $client->query($query); $response_array = $updateResponse->getResponse(); $facet_datas = $response_array->facet_counts->facet_fields; echo '<pre>'; print_r($facet_datas); }
<?php $query = new SolrQuery(); $query->setFacet(0); $query->addFacetField('israel')->addFacetField('israel2'); $query->addFacetQuery('silk')->addFacetQuery('name:june'); $query->setFacetPrefix('A')->setFacetPrefix('C', 'june'); $query->setFacetSort(1)->setFacetSort(0, 'june'); // $query->setFacetLimit(98)->setFacetLimit(44, 'june'); $query->setFacetOffset(110)->setFacetOffset(12, 'june'); $query->setFacetMinCount(4)->setFacetMinCount(30, 'june'); $query->setFacetMissing(1)->setFacetMissing(0, 'june'); $query->setFacetMethod('enum')->setFacetMethod('fc', 'june'); echo $query; echo "\n";