/** * Create a new date filter. * * Only one of the filter bounds may be 0, indicating a no limit in that * direction. * * @param string $field The name of the field that should be filtered. * @param string $start Start of filter range (or 0 for no start filter) * @param string $end End of filter range (or 0 for no end filter) */ public function __construct($field, $start, $end) { parent::__construct($field); if ($start == 0 && $end == 0) { throw new midcom_error('Both start and end of a datefilter must not be 0.'); } $this->_start = $start; $this->_end = $end; $this->type = 'datefilter'; }
/** * Query the index and, if set, restrict the query by a given filter. * * @param string $query The query, which must suite the backends query syntax. * @param midcom_services_indexer_filter $filter An optional filter used to restrict the query. This may be null indicating no filter. * @return Array An array of documents matching the query, or false on a failure. */ public function query($query, midcom_services_indexer_filter $filter = null) { if ($filter !== null) { $query .= ' AND ' . $filter->get_query_string(); } $url = "http://{$GLOBALS['midcom_config']['indexer_xmltcp_host']}:{$GLOBALS['midcom_config']['indexer_xmltcp_port']}/solr/select"; $request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET); $url = $request->getUrl(); // FIXME: Make this configurable, even better: adapt the whole indexer system to fetching enable querying for counts and slices $maxrows = 1000; $url->setQueryVariables(array('q' => $query, 'fl' => '*,score', 'rows' => $maxrows)); if (!empty($this->_index_name)) { $url->setQueryVariable('fq', '__INDEX_NAME:"' . rawurlencode($this->_index_name) . '"'); } $request->setHeader('Accept-Charset', 'UTF-8'); $request->setHeader('Content-type', 'text/xml; charset=utf-8'); try { $response = $request->send(); } catch (Exception $e) { debug_add("Failed to execute request " . $url . ": " . $e->getMessage(), MIDCOM_LOG_WARN); return false; } $this->code = $response->getStatus(); if ($this->code != 200) { debug_print_r($url . " returned response code {$this->code}, body:", $response->getBody()); return false; } $body = $response->getBody(); $response = DomDocument::loadXML($body); $xquery = new DomXPath($response); $result = array(); $num = $xquery->query('/response/result')->item(0); if ($num->getAttribute('numFound') == 0) { return array(); } foreach ($xquery->query('/response/result/doc') as $res) { $doc = new midcom_services_indexer_document(); foreach ($res->childNodes as $str) { $name = $str->getAttribute('name'); $doc->add_result($name, $str->tagName == 'float' ? (double) $str->nodeValue : (string) $str->nodeValue); if ($name == 'RI') { $doc->add_result('__RI', $str->nodeValue); } if ($name == 'score' && $filter == null) { $doc->score = (double) $str->nodeValue; } } /* FIXME: before result slicing is properly supported this can be too heavy if ( isset($doc->source) && mgd_is_guid($doc->source)) { midcom::get('cache')->content->register($doc->source); } */ $result[] = $doc; } debug_add(sprintf('Returning %d results', count($result)), MIDCOM_LOG_INFO); return $result; }