/** * @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; }
/** * {@inheritdoc} * * @return Client */ public function build() { $solariumClient = new Client(array('endpoint' => $this->settings), $this->eventDispatcher); foreach ($this->plugins as $pluginName => $plugin) { $solariumClient->registerPlugin($pluginName, $plugin); } return $solariumClient; }
private function testSolrConnection() { $client = new SolariumClient($this->getSolrConfig()); $ping = $client->createPing(); try { $client->ping($ping); return true; } catch (Exception $e) { return false; } }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $query = $input->getOption('query') ?: '*:*'; $this->logger->info('Starting delete query "{query}"', array('query' => $query)); $updateCommand = $this->solrClient->createUpdate(); $updateCommand->addDeleteQuery($query); $updateCommand->addCommit(); /** @var Result $result */ $result = $this->solrClient->execute($updateCommand); $this->logger->info('Finished delete query "{query}", status: {status}, duration: {duration}', array('query' => $query, 'status' => $result->getStatus(), 'duration' => $result->getQueryTime())); }
/** * @return mixed|void */ public function connect() { if (!$this->resource) { $resource = new Client($this->config); $resource->registerQueryType(Query::QUERY_ADMIN, 'Spryker\\Shared\\Library\\Storage\\Adapter\\Solr\\Solarium\\QueryType\\Admin\\Query'); if ($this->endpoint) { $resource->setDefaultEndpoint($this->endpoint); } $this->resource = $resource; } }
/** * 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); }
/** * Changes the query to a "More Like This" query. */ public function setMoreLikeThis(Query &$solarium_query, QueryInterface $query, $mlt_options = array(), $index_fields = array(), $fields) { $solarium_query = $this->solr->createMoreLikeThis(array('handler' => 'select')); // The fields to look for similarities in. if (empty($mlt_options['fields'])) { return; } $mlt_fl = array(); foreach ($mlt_options['fields'] as $mlt_field) { // Solr 4 has a bug which results in numeric fields not being supported // in MLT queries. // Date fields don't seem to be supported at all. $version = $this->getSolrVersion(); if ($fields[$mlt_field][0] === 'd' || $version == 4 && in_array($fields[$mlt_field][0], array('i', 'f'))) { continue; } $mlt_fl[] = $fields[$mlt_field]; // For non-text fields, set minimum word length to 0. if (isset($index_fields[$mlt_field]) && !SearchApiUtility::isTextType($index_fields[$mlt_field]->getType())) { $solarium_query->addParam('f.' . $fields[$mlt_field] . '.mlt.minwl', 0); } } //$solarium_query->setHandler('mlt'); $solarium_query->setMltFields($mlt_fl); /** @var \Solarium\Plugin\CustomizeRequest\CustomizeRequest $customizer */ $customizer = $this->solr->getPlugin('customizerequest'); $customizer->createCustomization('id')->setType('param')->setName('qt')->setValue('mlt'); // @todo Make sure these configurations are correct $solarium_query->setMinimumDocumentFrequency(1); $solarium_query->setMinimumTermFrequency(1); }
/** * Implements Search::Framework::SearchEngineAbstract::delete(). * * @return \Solarium\QueryType\Update\Result */ public function delete() { $update = $this->_client->createUpdate(); $update->addDeleteQuery('*:*'); $update->addCommit(); return $this->_client->update($update); }
/** * This method is a hook e.g. to notice an external change tracker that all the in memory synchronization is * finished, i.e. can be persisted (e.g. by calling an entity manager's flush()). */ public function commit() { if (count($this->deletedDocumentIds) === 0 && count($this->newOrUpdatedDocuments) === 0) { return; } $this->messages[] = "Flushing " . count($this->newOrUpdatedDocuments) . " inserts or updates and " . count($this->deletedDocumentIds) . " deletes"; $update = $this->solrClient->createUpdate(); if ($this->deletedDocumentIds) { $update->addDeleteByIds($this->deletedDocumentIds); } if ($this->newOrUpdatedDocuments) { $update->addDocuments($this->newOrUpdatedDocuments); } $update->addCommit(); $this->solrClient->execute($update); $this->deletedDocumentIds = array(); $this->newOrUpdatedDocuments = array(); /* * Manually trigger garbage collection * \Solarium\QueryType\Update\Query\Document\Document might hold a reference * to a "helper" object \Solarium\Core\Query\Helper, which in turn references * back the document. This circle prevents the normal, refcount-based GC from * cleaning up the processed Document instances after we release them. * * To prevent memory exhaustion, we start a GC cycle collection run. */ $update = null; gc_collect_cycles(); }
private function flush() { $this->logger->info("Flushing {numberInsertsUpdates} inserts or updates and {numberDeletes} deletes", array('numberInsertsUpdates' => count($this->newOrUpdatedDocuments), 'numberDeletes' => count($this->deletedDocumentIds))); if (count($this->deletedDocumentIds) === 0 && count($this->newOrUpdatedDocuments) === 0) { return; } $update = $this->solrClient->createUpdate(); if ($this->deletedDocumentIds) { $update->addDeleteByIds($this->deletedDocumentIds); } if ($this->newOrUpdatedDocuments) { $update->addDocuments($this->newOrUpdatedDocuments); } $update->addCommit(); $this->solrClient->execute($update); $this->deletedDocumentIds = array(); $this->newOrUpdatedDocuments = array(); $this->logger->debug("Flushed"); /* * Manually trigger garbage collection * \Solarium\QueryType\Update\Query\Document\Document might hold a reference * to a "helper" object \Solarium\Core\Query\Helper, which in turn references * back the document. This circle prevents the normal, refcount-based GC from * cleaning up the processed Document instances after we release them. * * To prevent memory exhaustion, we start a GC cycle collection run. */ $update = null; gc_collect_cycles(); }
protected function init() { $origDispatcher = $this->eventDispatcher; parent::init(); if ($origDispatcher) { $this->eventDispatcher = $origDispatcher; } }
public function ping() { $config = $this->getConfig(); $solr = new Solarium\Client($config); $ping = $solr->createPing(); try { $ping = $solr->ping($ping); $ping = $ping->getData(); $alive = false; if (isset($ping['status']) && $ping['status'] === "OK") { $alive = true; } } catch (\Solarium\Exception $e) { return false; } return $alive; }
/** * 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']; }
public function delete($id, $core) { try { $this->_config['endpoint']['localhost']['path'] = '/solr/' . $core . '/'; // create a client instance $client = new Client($this->_config); // get an update query instance $update = $client->createUpdate(); // add the delete id and a commit command to the update query $update->addDeleteById($id); $update->addCommit(); // this executes the query and returns the result $result = $client->update($update); if ($result->getStatus()) { throw new Exception('Invalid result returned from solr.'); } } catch (Exception $e) { $this->_log($e); } }
/** * @param object $doc * @param MetaInformationInterface $metaInformation * @param Event $event */ private function addDocumentToIndex($doc, MetaInformationInterface $metaInformation, Event $event) { try { $indexName = $metaInformation->getIndex(); $client = new \FS\SolrBundle\Client\Client($this->solrClientCore); $client->update($doc, $indexName); } catch (\Exception $e) { $errorEvent = new ErrorEvent(null, $metaInformation, json_encode($this->solrClientCore->getOptions()), $event); $errorEvent->setException($e); $this->eventManager->dispatch(Events::ERROR, $errorEvent); } }
/** * @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; }
/** * {@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; }
/** * @test */ public function adapterCommitsWhenBatchSizeIsReached() { // create 10 deletions for ($i = 0; $i < 10; $i++) { $deleteObject = new \stdClass(); $deleteObject->id = $i; $this->adapter->delete($deleteObject); unset($deleteObject); } // create 10 inserts/updates for ($i = 0; $i < 10; $i++) { $updateObject = new \stdClass(); $updateObject->id = $i; $this->adapter->updated($updateObject); unset($updateObject); } $query = $this->getMock(SolariumUpdate::class); $this->solrClient->expects($this->once())->method('createUpdate')->will($this->returnValue($query)); $query->expects($this->once())->method('addDeleteByIds'); $query->expects($this->once())->method('addDocuments'); $query->expects($this->once())->method('addCommit'); $this->solrClient->expects($this->once())->method('execute')->with($query); $this->adapter->afterObjectProcessed(); }
public function testCheckMinimalHigher() { $this->assertFalse(Client::checkMinimal('99.0')); }
/** * Add multiple documents to the data store. * * @param \Solarium\QueryType\Update\Query\Query $updateQuery * @param array $documents */ protected function addDocuments(Query $updateQuery, array $documents) { $updateQuery->addDocuments($documents); $this->client->update($updateQuery); }
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; }
use Solarium\QueryType\Select\Query\Query as Select; // This is a custom query class that could have some customized logic class MyQuery extends Select { } // 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>';
<?php require __DIR__ . '/init.php'; use Solarium\Client; use Solarium\QueryType\Select\Query\Query as Select; htmlHeader(); // create a client instance $client = new Client($config); // first create a base query as a query class class PriceQuery extends Select { protected function init() { parent::init(); // set a query (all prices starting from 12) $this->setQuery('price:[12 TO *]'); // set start and rows param (comparable to SQL limit) using fluent interface $this->setStart(2)->setRows(20); // set fields to fetch (this overrides the default setting 'all fields') $this->setFields(array('id', 'name', 'price')); // sort the results by price ascending $this->addSort('price', self::SORT_ASC); } } // the query instance easily be altered based on user input // try calling this page with "?start=10" added to the url. $query = new PriceQuery(); if (isset($_GET['start']) && is_numeric($_GET['start'])) { $query->setStart($_GET['start']); } // alternatively you can use class inheritance to create query inheritance
/** * Gets the current Solarium update query, creating one if necessary. * * @return \Solarium\QueryType\Update\Query\Query * The Update query. */ protected function getUpdateQuery() { if (!static::$updateQuery) { $this->connect(); static::$updateQuery = $this->solr->createUpdate(); } return static::$updateQuery; }
/** * Exports Kloster data from mysql into solr * * @return bool */ public function mysql2solrExportAction() { $this->dataImport->initializeLogger(self::exportLogFile); $jobOwnerFileContent = file_get_contents($this->dataImport->dumpDirectory . self::executeExportDump); $this->dataImport->importExportLogger->log($jobOwnerFileContent); $start = date('d.m.Y H:i:s'); $date1 = new \DateTime($start); $this->dataImport->importExportLogger->log('Start am ' . $start); if (file_exists($this->dataImport->dumpDirectory . self::executeExportDump)) { unlink($this->dataImport->dumpDirectory . self::executeExportDump); } $klosterData = $this->klosterListAllAction(); $klosterArr = $klosterData[0]; $klosterstandortArr = $klosterData[1]; $klosterordenArr = $klosterData[2]; $standort_ordenArr = $klosterData[3]; $update = $this->client->createUpdate(); $docs = []; foreach ($klosterArr as $k => $v) { $doc = $update->createDocument(); foreach ($v as $i => $v1) { $doc->{$i} = $v1; } array_push($docs, $doc); } foreach ($klosterstandortArr as $k => $v) { foreach ($v as $k1 => $v1) { $doc = $update->createDocument(); foreach ($v1 as $k2 => $v2) { $doc->{$k2} = $v2; } array_push($docs, $doc); } } foreach ($klosterordenArr as $k => $v) { foreach ($v as $k1 => $v1) { $doc = $update->createDocument(); foreach ($v1 as $k2 => $v2) { $doc->{$k2} = $v2; } array_push($docs, $doc); } } foreach ($standort_ordenArr as $k => $v) { foreach ($v as $k1 => $v1) { foreach ($v1 as $k2 => $v2) { $doc = $update->createDocument(); foreach ($v2 as $k3 => $v3) { $doc->{$k3} = $v3; } array_push($docs, $doc); } } } $update->addDocuments($docs); $update->addCommit(); $this->deleteAction(); /** @var \Solarium\Core\Query\Result\ResultInterface $result */ $result = $this->client->execute($update); $logMessage = 'Data export completed in ' . round($result->getQueryTime() / 100) . ' seconds.'; $this->logger->log($logMessage); $end = date('d.m.Y H:i:s'); $date2 = new \DateTime($end); $this->dataImport->importExportLogger->log('Ende am ' . $end); $this->dataImport->importExportLogger->log('Dauer ' . $date1->diff($date2)->i . " Minuten und " . $date1->diff($date2)->s . ' Sekunden'); return $logMessage; }
private function createClient(AdapterInterface $adapter) { $client = new Client(); $client->setAdapter($adapter); return $client; }
/** * Execute query and return the resultset * * @return \Solarium\Core\Query\Result\ResultInterface */ public function run() { return $this->client->select($this->query); }
<?php require __DIR__ . '/init.php'; use Solarium\Client; htmlHeader(); // create a client instance $client = new Client($config); // get a morelikethis query instance $query = $client->createMoreLikeThis(); $query->setQuery('id:SP2514N'); $query->setMltFields('manu,cat'); $query->setMinimumDocumentFrequency(1); $query->setMinimumTermFrequency(1); $query->createFilterQuery('stock')->setQuery('inStock:true'); $query->setInterestingTerms('details'); $query->setMatchInclude(true); // this executes the query and returns the result $resultset = $client->select($query); echo 'Document used for matching:<br/><table>'; foreach ($resultset->getMatch() as $field => $value) { // this converts multivalue fields to a comma-separated string if (is_array($value)) { $value = implode(', ', $value); } echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; } echo '</table><hr/>'; // display the total number of MLT documents found by solr echo 'Number of MLT matches found: ' . $resultset->getNumFound() . '<br/><br/>'; echo '<b>Listing of matched docs:</b>'; // show MLT documents using the resultset iterator
/** * @param QueryInterface $query */ private function applyOnAllCores(QueryInterface $query) { foreach ($this->solariumClient->getEndpoints() as $endpointName => $endpoint) { $this->solariumClient->update($query, $endpointName); } }
/** * Tries to connect to a Solr Server * @param string $db_host * @param string $db_port * @param string $db_path * */ public function __construct($db_host, $db_port, $db_path, $db_core) { $config = array('endpoint' => array('localhost' => array('host' => $db_host, 'port' => $db_port, 'path' => $db_path, 'core' => $db_core))); parent::__construct($config); }