Beispiel #1
6
 /**
  * @param QueryInterface $query
  * @param ModelInterface $model
  * @return ModelListInterface
  * @throws NotSupportedFilterException
  */
 public function find(QueryInterface $query, ModelInterface $model)
 {
     $solrQuery = $this->solrClient->createSelect();
     $helper = $solrQuery->getHelper();
     foreach ($query->getFilters() as $filter) {
         if (!$filter instanceof KeyValueFilter) {
             throw new NotSupportedFilterException(sprintf('%s filter is not supported or unknown.', get_class($filter)));
         }
         $solrQuery->createFilterQuery($filter->getFieldName())->setQuery($filter->getFieldName() . ': ' . $helper->escapePhrase($filter->getValue()));
     }
     if ($query->getLimit() !== null) {
         $solrQuery->setRows($query->getLimit());
     }
     if ($query->getOffset() !== null) {
         $solrQuery->setStart($query->getOffset());
     }
     $result = $this->solrClient->select($solrQuery);
     $list = new ModelList();
     foreach ($result as $doc) {
         /** @var ModelInterface $item */
         $item = new $model();
         $item->loadData($doc->getFields());
         if ($item instanceof SavableModelInterface) {
             $item->markAsStored();
         }
         $list->addListItem($item);
     }
     return $list;
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public function query(AbstractQuery $query)
 {
     $entity = $query->getEntity();
     $queryString = $query->getQuery();
     $runQueryInIndex = $query->getIndex();
     $query = $this->solrClientCore->createSelect($query->getOptions());
     $query->setQuery($queryString);
     try {
         $response = $this->solrClientCore->select($query, $runQueryInIndex);
     } catch (\Exception $e) {
         $errorEvent = new ErrorEvent(null, null, 'query solr');
         $errorEvent->setException($e);
         $this->eventManager->dispatch(Events::ERROR, $errorEvent);
         return array();
     }
     $this->numberOfFoundDocuments = $response->getNumFound();
     if ($this->numberOfFoundDocuments == 0) {
         return array();
     }
     $targetEntity = $entity;
     $mappedEntities = array();
     foreach ($response as $document) {
         $mappedEntities[] = $this->entityMapper->toEntity($document, $targetEntity);
     }
     return $mappedEntities;
 }
 /**
  * Returns url's that are expired.
  *
  * @param string $core
  *
  * @return \Solarium\QueryType\Select\Result\Result
  */
 public function findExpiredUrls($core)
 {
     $this->setCoreNameFromMetadata(['core' => $core]);
     $now = new DateTime();
     $queryPhrase = sprintf("revisit_expiration:[* TO %s]", $now->format('Y-m-d\\TH:i:s\\Z'));
     $query = $this->client->createSelect()->setQuery($queryPhrase)->setRows(1000);
     return $this->client->select($query);
 }
 /**
  */
 public function render()
 {
     switch (gettype($this->arguments['query'])) {
         case 'string':
             $query = $this->createQuery($this->arguments['query']);
             break;
         case 'array':
             $query = $this->createQuery(implode(' ' . $this->arguments['operator'] . ' ', array_map(function ($k, $v) {
                 return $k . ':' . $v;
             }, array_keys($this->arguments['query']), array_values($this->arguments['query']))));
             break;
         default:
             $query = $this->createQuery('*:*');
     }
     if (!is_null($this->arguments['sortField'])) {
         $query->addSort($this->arguments['sortField'], $this->arguments['sortOrder']);
     }
     if (!is_null($this->arguments['rows'])) {
         $query->setRows($this->arguments['rows']);
     }
     if (!is_null($this->arguments['fields'])) {
         $query->clearFields();
         $query->addFields($this->arguments['fields']);
     }
     /** @var Result $resultSet */
     $resultSet = $this->solr->select($query);
     /** @var DocumentInterface $result */
     $results = $resultSet->getDocuments();
     $out = '';
     if ($results) {
         foreach ($results as $result) {
             if ($this->templateVariableContainer->exists('solr')) {
                 $this->templateVariableContainer->remove('solr');
             }
             $this->templateVariableContainer->add('solr', $result);
             $out .= $this->renderThenChild();
         }
     } else {
         $out .= $this->renderElseChild();
     }
     return $out;
 }
 /**
  * Returns url's that are not indexed or indexed but expired.
  *
  * @param string $uri
  * @param array  $metadata
  *
  * @return boolean
  */
 public function isUrlNotIndexedOrIndexedAndExpired($uri, array $metadata = [])
 {
     $this->setCoreNameFromMetadata($metadata);
     $uriHash = sha1(strtolower($uri));
     $queryPhrase = sprintf("id:%s", $uriHash);
     $query = $this->client->createSelect();
     $query->setQuery($queryPhrase);
     $result = $this->client->select($query);
     if ($result->getNumFound() < 1) {
         return true;
     }
     $now = new DateTime();
     $queryPhrase = sprintf("id:%s AND revisit_expiration:[* TO %s]", $uriHash, $now->format('Y-m-d\\TH:i:s\\Z'));
     $query->setQuery($queryPhrase);
     $result = $this->client->select($query);
     return $result->getNumFound() > 0;
 }
 /**
  * @param integer $storeId - Store View Id
  * @param string $queryString - What the user is typing
  * @return array              - key = suggested term,  value = result count
  */
 public function getAutoSuggestions($storeId, $queryString)
 {
     $result = null;
     // Create basic query with wildcard
     $query = $this->_client->createSelect();
     $queryHelper = $query->getHelper();
     $escapedQueryString = $queryHelper->escapeTerm(strtolower($queryString));
     $query->setQueryDefaultField('text');
     $query->setQuery($escapedQueryString . '*');
     $query->setRows(0);
     if (!empty($storeId)) {
         $query->createFilterQuery('store_id')->setQuery('store_id:' . intval($storeId));
     }
     $groupComponent = $query->getGrouping();
     $groupComponent->addField('product_id');
     $groupComponent->setFacet(true);
     $groupComponent->setLimit(1);
     // Add facet for completion
     $facetSet = $query->getFacetSet();
     $facetField = $facetSet->createFacetField('auto_complete');
     $facetField->setField('text');
     $facetField->setMincount(1);
     $facetField->setLimit($this->getConf('results/autocomplete_suggestions'));
     $facetField->setPrefix($escapedQueryString);
     try {
         $solariumResult = $this->_client->select($query);
         $this->debugQuery($query);
         if ($solariumResult) {
             $result = array();
             foreach ($solariumResult->getFacetSet()->getFacet('auto_complete') as $term => $matches) {
                 if ($matches) {
                     $result[$term] = $matches;
                 }
             }
         }
     } catch (Exception $e) {
         Mage::log(sprintf('%s->%s: %s', __CLASS__, __FUNCTION__, $e->getMessage()), Zend_Log::ERR);
         $this->debugQuery($query);
     }
     return $result;
 }
}
// this very simple plugin that modifies the default querytype mapping
class QueryCustomizer extends Plugin
{
    public function initPlugin($client, $options)
    {
        $client->registerQueryType(Client::QUERY_SELECT, 'MyQuery', 'Solarium\\QueryType\\Select\\RequestBuilder\\RequestBuilder', 'Solarium\\QueryType\\Select\\ResponseParser\\ResponseParser');
    }
}
htmlHeader();
// create a client instance and register the plugin
$client = new Client($config);
$client->registerPlugin('querycustomizer', 'QueryCustomizer');
// create a select query instance
$query = $client->createSelect();
// check the query class, it should be our custom query class
echo 'Query class: ' . get_class($query) . '<br/>';
// execute the query and display the results
$resultset = $client->select($query);
echo 'NumFound: ' . $resultset->getNumFound();
foreach ($resultset as $document) {
    echo '<hr/><table>';
    foreach ($document as $field => $value) {
        if (is_array($value)) {
            $value = implode(', ', $value);
        }
        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
    }
    echo '</table>';
}
htmlFooter();
 public function get($search_term = NULL, $core = FALSE)
 {
     if ($search_term !== NULL) {
         $this->search($search_term, $core);
     }
     if (empty($this->_core)) {
         return FALSE;
     }
     $config = Config::get('laravel-solarium::solr');
     $config['endpoint']['localhost']['path'] = '/solr/' . $this->_core . '/';
     $client = new Client($config);
     // get a select query instance
     $query = $client->createSelect();
     if (empty($this->_search_term)) {
         return FALSE;
     }
     $query->setQuery($this->_search_term);
     // set start and rows param (comparable to SQL limit) using fluent interface
     $query->setStart($this->_start_index)->setRows($this->_count_index);
     if (is_array($this->_fields) && !empty($this->_fields)) {
         $query->setFields($this->_fields);
     }
     if ($this->_order_by_field !== FALSE && $this->_order_by_direction !== FALSE) {
         // sort the results by price ascending
         $query->addSort($this->_order_by_field, $this->_order_by_direction);
     }
     if (is_array($this->_filters) && !empty($this->_filters)) {
         foreach ($this->_filters as $filter_name => $filter) {
             $query->createFilterQuery($filter_name)->setQuery($filter);
         }
     }
     if (is_array($this->_highlight_fields) && !empty($this->_highlight_fields)) {
         $hl = $query->getHighlighting();
         $hl->setSnippets(5);
         $hl->setFields(trim(implode(',', $this->_highlight_fields), ','));
         $hl->setSimplePrefix('<' . $this->_highlight_tag . '>');
         $hl->setSimplePostfix('</' . $this->_highlight_tag . '>');
     }
     $result = $client->select($query);
     $this->_reset();
     // this executes the query and returns the result
     return $result;
 }
Beispiel #9
0
 /**
  * Execute query and return the resultset
  * 
  * @return \Solarium\Core\Query\Result\ResultInterface
  */
 public function run()
 {
     return $this->client->select($this->query);
 }
Beispiel #10
0
 /**
  * Implements Search::Framework::SearchEngineAbstract::search().
  *
  * @return \Solarium\QueryType\Select\Result\Result
  */
 public function search($keywords, array $options = array())
 {
     $query = $this->_client->createSelect();
     $query->setQuery($keywords);
     return $this->_client->select($query);
 }