<?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 //-- }
public function facet_date() { $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->setFacet(TRUE); $query->setFacetSort(SolrQuery::FACET_SORT_INDEX); $query->setFacetLimit(20000); $query->addFacetDateField('input_datetime'); //$query->setFacetDateStart('2010-06-01T00:00:00Z'); $query->setFacetDateStart('2008-06-01T00:00:00Z'); $query->setFacetDateEnd('2010-12-01T00:00:00Z'); $query->setFacetDateGap('+1MONTH'); $query->setFacetDateHardEnd(TRUE); //$query->setFacetMinCount(2); $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($response_array->facet_counts); }
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(); }
<?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";
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); } } }