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;
 }
 /**
  * Get an Iterator over all $type objects in the source/destination system, ordered by their ascending IDs.
  *
  * @param string $type       Type of Objects to return
  * @param string $indexQueue Whether all Objects or only new, updated or deleted Objects are returned for indexing
  *
  * @return \Iterator
  */
 public function getObjectsOrderedById($type)
 {
     $query = $this->solrClient->createSelect()->setQuery('type:' . $type)->setStart(0)->setRows(1000000)->setFields(array('*'))->addSort('id', 'asc');
     /** @var Result $resultset */
     $resultset = $this->solrClient->execute($query);
     $this->messages[] = "SolariumAdapter found " . $resultset->getNumFound() . " objects for type " . $type;
     return $resultset->getIterator();
 }
 /**
  * @param string $objectClass Fully qualified class name of the object
  * @return \ArrayIterator
  */
 public function getObjectsOrderedById($objectClass)
 {
     $normalizedObjectClass = $this->normalizeObjectClass($objectClass);
     $query = $this->solrClient->createSelect()->setQuery('objectclass:' . $normalizedObjectClass)->setStart(0)->setRows(1000000)->setFields(array('id', 'objectid', 'objectclass', 'hash'))->addSort('objectid', 'asc');
     /** @var SelectResult $resultset */
     $resultset = $this->solrClient->execute($query);
     $this->logger->info("SolariumDestinationAdapter found {number} objects for objectClass {objectClass}", array('number' => $resultset->getNumFound(), 'objectClass' => $objectClass));
     return $resultset->getIterator();
 }
 /**
  * Creates a blank query, sets up TypoScript filters and adds it to the view.
  *
  */
 protected function createQuery()
 {
     $this->query = $this->connection->createSelect();
     $this->addFeatures();
     $this->addTypoScriptFilters();
     $this->setConfigurationValue('solarium', $this->query);
 }
Beispiel #5
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);
 }
Beispiel #7
0
 /**
  * Get select query
  *
  * @param AbstractQuery $query
  *
  * @return \Solarium\QueryType\Select\Query\Query
  */
 public function getSelectQuery(AbstractQuery $query)
 {
     $selectQuery = $this->solrClientCore->createSelect($query->getOptions());
     $selectQuery->setQuery($query->getQuery());
     $selectQuery->setFilterQueries($query->getFilterQueries());
     $selectQuery->setSorts($query->getSorts());
     $selectQuery->setFields($query->getFields());
     return $selectQuery;
 }
 /**
  * Creates a query for a document
  *
  * @param string $id the document id
  * @param string $idfield the document id field
  * @return \Solarium\QueryType\Select\Query\Query
  */
 private function createQuery($query)
 {
     $queryObject = $this->solr->createSelect();
     $this->addTypoScriptFilters($queryObject);
     $queryObject->setQuery($query);
     $this->createQueryComponents($queryObject);
     $this->configuration['solarium'] = $queryObject;
     return $this->configuration['solarium'];
 }
 /**
  * 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;
 }
Beispiel #11
0
 /**
  * {@inheritDoc}
  */
 public function search($query)
 {
     $query = $result = preg_replace('@([\\+\\-\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\"\\~\\*\\?\\:])@is', '\\\\1', $query);
     $queryClass = $this->client->createSelect();
     $instance = $this->instanceManager->getInstanceFromRequest();
     $queryClass->createFilterQuery('ínstanceFilter')->setQuery('instance:(' . $instance->getId() . ')');
     $hl = $queryClass->getHighlighting();
     $hl->setFields('content');
     $hl->setSimplePrefix('<strong>');
     $hl->setSimplePostfix('</strong>');
     $queryClass->setFields(['*', 'score']);
     $disMax = $queryClass->getDisMax();
     $disMax->setQueryFields('title^4 content keywords^2 type^3');
     $queryClass->setQuery($query);
     $queryClass->addSort('score', $queryClass::SORT_DESC);
     $queryClass->setQueryDefaultOperator($queryClass::QUERY_OPERATOR_AND);
     $adapter = new SolrPaginator($this->client, $queryClass);
     $adapter->setTranslator($this->translator);
     $paginator = new Paginator($adapter);
     return $paginator;
 }
{
}
// 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>';
}
 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 #14
0
 /**
  * Constructor
  * 
  * @param \Solarium\Client $client
  * 
  * @return Irto\Solrio\Query\Builder
  */
 public function __construct(Solarium\Client $client, $query = null)
 {
     $this->client = $client;
     $this->query = $client->createSelect();
     $this->rawQuery = $query;
 }
Beispiel #15
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);
 }
 /**
  * {@inheritdoc}
  */
 public function search(QueryInterface $query)
 {
     // Reset request handler.
     $this->request_handler = NULL;
     // Get field information.
     /** @var \Drupal\search_api\Entity\Index $index */
     $index = $query->getIndex();
     $index_id = $this->getIndexId($index->id());
     $field_names = $this->getFieldNames($index);
     $field_names_single_value = $this->getFieldNames($index, TRUE);
     // Get Solr connection.
     $this->connect();
     // Instantiate a Solarium select query.
     $solarium_query = $this->solr->createSelect();
     // Extract keys.
     $keys = $query->getKeys();
     if (is_array($keys)) {
         $keys = $this->getSolrHelper()->flattenKeys($keys);
     }
     // Set them
     $solarium_query->setQuery($keys);
     unset($keys);
     $returned_fields = array('item_id', 'score');
     if (!$this->configuration['site_hash']) {
         $returned_fields[] = 'hash';
     }
     $solarium_query->setFields($returned_fields);
     // Set searched fields.
     $options = $query->getOptions();
     $search_fields = $this->getQueryFulltextFields($query);
     // Get the index fields to be able to retrieve boosts.
     $index_fields = $index->getFields();
     $query_fields = array();
     foreach ($search_fields as $search_field) {
         /** @var \Solarium\QueryType\Update\Query\Document\Document $document */
         $document = $index_fields[$search_field];
         $boost = $document->getBoost() ? '^' . $document->getBoost() : '';
         $query_fields[] = $field_names[$search_field] . $boost;
     }
     $solarium_query->getEDisMax()->setQueryFields(implode(' ', $query_fields));
     // Handle More Like This requests
     $mlt_options = $query->getOption('search_api_mlt');
     if ($mlt_options) {
         $index_fields = $index->getFields();
         $this->getSolrHelper()->setMoreLikeThis($solarium_query, $query, $mlt_options, $index_fields, $field_names);
         // Override the search key by setting it to the solr document id
         // we want to compare it with
         // @todo. Figure out how we can set MLT earlier in the process
         // so we do not do unnecessary function calls
         $id = $this->createId($index_id, $mlt_options['id']);
         $id = static::getQueryHelper()->escapePhrase($id);
         $solarium_query->setQuery('id:' . $id);
     }
     // Set basic filters.
     $conditions_queries = $this->createFilterQueries($query->getConditionGroup(), $field_names, $index->getFields());
     foreach ($conditions_queries as $id => $conditions_query) {
         $solarium_query->createFilterQuery('filters_' . $id)->setQuery($conditions_query);
     }
     // Set the Index filter
     $solarium_query->createFilterQuery('index_id')->setQuery('index_id:' . static::getQueryHelper($solarium_query)->escapePhrase($index_id));
     // Set the site hash filter, if enabled.
     if ($this->configuration['site_hash']) {
         $site_hash = $this->getQueryHelper()->escapePhrase(SearchApiSolrUtility::getSiteHash());
         $solarium_query->createFilterQuery('site_hash')->setQuery('hash:' . $site_hash);
     }
     // Set sorts.
     $this->solrHelper->setSorts($solarium_query, $query, $field_names_single_value);
     // Set facet fields.
     $facets = $query->getOption('search_api_facets', array());
     $this->setFacets($facets, $field_names, $solarium_query);
     // Set highlighting.
     $excerpt = !empty($this->configuration['excerpt']) ? true : false;
     $highlight_data = !empty($this->configuration['highlight_data']) ? true : false;
     $this->getSolrHelper()->setHighlighting($solarium_query, $query, $excerpt, $highlight_data);
     // Handle spatial filters.
     $spatial_options = $query->getOption('search_api_location');
     if ($spatial_options) {
         $this->solrHelper->setSpatial($solarium_query, $query, $spatial_options, $field_names);
     }
     // Handle field collapsing / grouping.
     $grouping_options = $query->getOption('search_api_grouping');
     if (!empty($grouping_options['use_grouping'])) {
         $this->solrHelper->setGrouping($solarium_query, $query, $grouping_options, $index_fields, $field_names);
     }
     if (isset($options['offset'])) {
         $solarium_query->setStart($options['offset']);
     }
     $rows = isset($options['limit']) ? $options['limit'] : 1000000;
     $solarium_query->setRows($rows);
     if (!empty($options['search_api_spellcheck'])) {
         $solarium_query->getSpellcheck();
     }
     /**
      * @todo Make this more configurable so that views can choose which fields
      * it wants to fetch
      */
     if (!empty($this->configuration['retrieve_data'])) {
         $solarium_query->setFields(array('*', 'score'));
     }
     // Allow modules to alter the query
     try {
         $this->moduleHandler->alter('search_api_solr_query', $solarium_query, $query);
         $this->preQuery($solarium_query, $query);
         // Use the 'postbigrequest' plugin if no specific http method is
         // configured. The plugin needs to be loaded before the request is
         // created.
         if ($this->configuration['http_method'] == 'AUTO') {
             $this->solr->getPlugin('postbigrequest');
         }
         // Use the manual method of creating a Solarium request so we can control
         // the HTTP method.
         $request = $this->solr->createRequest($solarium_query);
         // Set the configured HTTP method.
         if ($this->configuration['http_method'] == 'POST') {
             $request->setMethod(Request::METHOD_POST);
         } elseif ($this->configuration['http_method'] == 'GET') {
             $request->setMethod(Request::METHOD_GET);
         }
         // Set HTTP Basic Authentication parameter, if login data was set.
         if (strlen($this->configuration['http_user']) && strlen($this->configuration['http_pass'])) {
             $request->setAuthentication($this->configuration['http_user'], $this->configuration['http_pass']);
         }
         // Send search request.
         $response = $this->solr->executeRequest($request);
         $resultset = $this->solr->createResult($solarium_query, $response);
         // Extract results.
         $results = $this->extractResults($query, $resultset);
         // Add warnings, if present.
         if (!empty($warnings)) {
             foreach ($warnings as $warning) {
                 $results->addWarning($warning);
             }
         }
         // Extract facets.
         if ($resultset instanceof Result) {
             if ($facets = $this->extractFacets($query, $resultset)) {
                 $results->setExtraData('search_api_facets', $facets);
             }
         }
         $this->moduleHandler->alter('search_api_solr_search_results', $results, $query, $resultset);
         $this->postQuery($results, $query, $resultset);
         return $results;
     } catch (SearchApiException $e) {
         throw new SearchApiException(t('An error occurred while trying to search with Solr: @msg.', array('@msg' => $e->getMessage())));
     }
 }