public function getItems() { $params = JComponentHelper::getParams($this->get('option'), true); $list = array(); $facetParams = array(); $filters = array(); $array = array(); $facetFields = $this->getState('facet.fields'); if ($this->getState('facet.range')) { $facetFields[] = $this->getState('facet.range'); } if (!$this->getState('params')->get('override_schema', 0)) { $access = implode(' OR ', JFactory::getUser()->getAuthorisedViewLevels()); if ($access) { $access = 'access:' . '(' . $access . ') OR null'; $filters[] = $access; } $filters[] = $this->getLanguageFilter(); } // get context. if ($this->getState('query.o', null)) { foreach ($dispatcher->trigger('onJSolrSearchRegisterPlugin') as $result) { if (JArrayHelper::getValue($result, 'name') == $this->getState('query.o', null)) { $filters = array_merge($filters, array('context:' . JArrayHelper::getValue($result, 'context'))); } } } if ($prefix = $this->getState('facet.prefix')) { $facetParams['facet.prefix'] = $prefix; } JPluginHelper::importPlugin("jsolrsearch"); $dispatcher = JDispatcher::getInstance(); try { $query = \JSolr\Search\Factory::getQuery("*:*")->useQueryParser('edismax')->mergeParams($facetParams)->filters($filters)->facet(0, 'index', -1)->rows(0); if ($this->getState('facet.fields')) { $query->facetFields($this->getState('facet.fields')); } if ($this->getState('facet.range')) { $query->facetRange($this->getState('facet.range'), $this->getState('facet.start'), $this->getState('facet.end'), $this->getState('facet.gap')); } $results = $query->search(); foreach ($facetFields as $field) { $array[$field] = array(); if (isset($results->getFacets()->{$field})) { foreach ($results->getFacets()->{$field} as $key => $value) { $array[$field][$key] = $value; } } if (isset($results->getFacetRanges()->{$field})) { $counts = (array) $results->getFacetRanges()->{$field}->counts; foreach ($counts as $key => $value) { $array[$field][$key] = $value; } } } } catch (Exception $e) { JLog::add($e->getMessage(), JLog::ERROR, 'jsolrsearch'); } return $array; }
/** * @return array */ function getItems() { $q = JFactory::getApplication()->input->getString('q'); $fields = JFactory::getApplication()->input->getString('fields'); $suggest = JFactory::getApplication()->input->getString('suggest'); $query = \JSolr\Search\Factory::getQuery('*:*')->useQueryParser("edismax")->retrieveFields("*,score")->limit(10)->highlight(200, "<strong>", "</strong>", 1); $fields = explode(',', $fields); $items = array(); if (empty($q)) { return $items; } foreach ($fields as &$field) { $field = explode('^', $field); $field = $field[0]; $field = $field . ':' . $q . '*'; } $filters = implode(' OR ', $fields); $query->filters($filters); try { $results = $query->search(); print_r($results); $response = json_decode($results->getSuggestions()); foreach ($response->docs as $doc) { if (is_array($doc->{$suggest})) { $v = (array) $doc->{$suggest}; $items[] = $v[0]; } else { $items[] = $doc->{$suggest}; } } } catch (Exception $e) { print_r($e->getMessage()); } return $items; }
private function _loadDefaultRanges() { if (!$this->ranges) { $params = array(); $params["stats"] = "true"; $params["stats.field"] = $this->filter; $query = \JSolr\Search\Factory::getQuery("*:*")->mergeParams($params)->rows(0); $results = $query->search(); $stats = $results->getStats(); if (isset($stats->{$this->filter})) { $this->ranges = JArrayHelper::fromObject($stats->{$this->filter}); } } }
public function getItems($params) { $results = array(); if (class_exists('\\JSolr\\Search\\Factory')) { try { $client = \JSolr\Search\Factory::getService(); $query = \JSolr\Search\Factory::getQuery('*:*')->limit($params->get('count', 5))->sort($params->get('ordering', 'modified') . ' desc')->useQueryParser("edismax")->retrieveFields("*,score"); if ($limit = $params->get('o')) { $query->filters('extension:' . $limit); } if ($fq = $params->get('fq', null)) { $filters = array(); $parts = explode(',', $fq); foreach ($parts as $part) { $filters[] = trim($part); } $query->mergeFilters($filters); } $results = $query->search(); } catch (Exception $e) { } } return $results; }
/** * (non-PHPdoc) * @see \JSolr\Index\Crawler::clean() */ protected function clean() { $items = $this->getItems(); $service = \JSolr\Index\Factory::getService(); $query = \JSolr\Search\Factory::getQuery('*:*')->useQueryParser("edismax")->filters(array($this->get('itemContext')))->retrieveFields('id')->rows(0); $results = $query->search(); if ($results->get('numFound')) { $query->rows($results->get('numFound')); } $results = $query->search(); if ($results->get('numFound')) { $delete = array(); $prefix = $this->get('itemContext') . '.'; foreach ($results as $result) { $needle = new stdClass(); $needle->{'search.resourceid'} = $result->id; if (array_search($needle, $items) === false) { $delete[] = $prefix . $result->id; } } if (count($delete)) { foreach ($delete as $key) { $this->out('cleaning item ' . $key . ' and its bitstreams'); $query = 'type:' . $this->get('assetContext') . ' AND parent_id:' . str_replace($prefix, '', $key); $service->deleteByQuery($query); } $service->deleteByMultipleIds($delete); $response = $service->commit(); } } }
/** * Gets the Solr query for the list. * * @return JSolrSearchQuery An instance of the JSolrSearchQuery class. */ protected function _getListQuery() { $hl = array(); $filters = array(); $facets = array(); $qf = array(); $sort = array(); $filters = $this->getForm()->getFilters(); if (!$this->getState('params')->get('override_schema', 0)) { $access = implode(' OR ', JFactory::getUser()->getAuthorisedViewLevels()); if ($access) { $access = 'access:' . '(' . $access . ') OR null'; $filters[] = $access; } } if (!$this->getState('query.q')) { if (!$this->getAppliedFacetFilters()) { return null; // nothing passed. Get out of here. } } $sort = $this->getForm()->getSorts(); $facets = $this->getForm()->getFacets(); JPluginHelper::importPlugin("jsolrsearch"); $dispatcher = JEventDispatcher::getInstance(); // Get any additional filters which may be needed as part of the search query. foreach ($dispatcher->trigger("onJSolrSearchFQAdd") as $result) { $filters = array_merge($filters, $result); } // Get Highlight fields for results. foreach ($dispatcher->trigger('onJSolrSearchHLAdd') as $result) { $hl = array_merge($hl, $result); } // get query filter params and boosts from plugin. foreach ($dispatcher->trigger('onJSolrSearchQFAdd') as $result) { $qf = array_merge($qf, $result); } // get context. if ($this->getState('query.o', null)) { foreach ($dispatcher->trigger('onJSolrSearchRegisterPlugin') as $result) { if (JArrayHelper::getValue($result, 'name') == $this->getState('query.o', null)) { $filters = array_merge($filters, array('context:' . JArrayHelper::getValue($result, 'context'))); } } } $q = $this->getState('query.q', "*:*"); $query = \JSolr\Search\Factory::getQuery($q)->spellcheck(true)->useQueryParser("edismax")->retrieveFields("*,score")->filters($filters)->highlight(200, "<mark>", "</mark>", 3, implode(" ", $hl))->limit($this->getState("list.limit", JFactory::getApplication()->getCfg('list.limit', 10)))->offset($this->getState("list.start", 0))->mergeParams(array('mm' => $this->getState('params')->get('mm', self::MM_DEFAULT))); if (count($sort)) { $query->sort(implode(', ', $sort)); } if (count($qf)) { $query->queryFields($qf); } if (count($facets)) { foreach ($facets as $facet) { $query->mergeParams($facet); } $query->facet(1, true, 10); } return $query; }