diff --git a/README.md b/README.md index 8cd6e46f..19055aa9 100644 --- a/README.md +++ b/README.md @@ -245,4 +245,5 @@ with the concept usage in a Solr context: * `drush search_api_pantheon:diagnose` (sapd) The DIAGNOSE command will check the various pieces of the Search API install and throw errors on the pieces that are not working. This command will develop further as the module nears general availability. -* +* `drush search_api_pantheon:select` (saps) This command will run the given query against Solr server. It's recommended to use + `?debug=true` in any Solr page to get a good query to pass to this command to debug results. diff --git a/drush.services.yml b/drush.services.yml index eced28f6..f70c3181 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -9,3 +9,8 @@ services: arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.schema_poster" ] tags: - { name: drush.command } + search_api_pantheon.drush_query: + class: \Drupal\search_api_pantheon\Commands\Query + arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.endpoint", "@search_api_pantheon.solarium_client" ] + tags: + - { name: drush.command } diff --git a/src/Commands/Query.php b/src/Commands/Query.php new file mode 100644 index 00000000..fd1a5616 --- /dev/null +++ b/src/Commands/Query.php @@ -0,0 +1,116 @@ +logger = $loggerChannelFactory->get('SearchAPIPantheon Drush'); + $this->pantheonGuzzle = $pantheonGuzzle; + $this->endpoint = $endpoint; + $this->solr = $solariumClient; + } + + /** + * Search_api_pantheon:select. + * + * @usage search-api-pantheon:select + * Runs a select query against Pantheon Solr. + * + * @command search-api-pantheon:select + * + * @option wt Output format + * @option rows Number of rows to return + * @option qf Query fields + * @option defType Default search type + * @option omitHeader Do not output header + * @option fields Fields to return + * + * @aliases saps + * + * @throws \Drupal\search_api_solr\SearchApiSolrException + * @throws \JsonException + * @throws \Exception + */ + public function select($query, $options = [ + 'wt' => 'json', + 'rows' => 10, + 'qf' => '', + 'defType' => 'edismax', + 'omitHeader' => 'true', + 'fields' => 'ss_search_api_id,ss_search_api_language,score,hash', + ]) { + $this->logger->notice('Running a select query against Pantheon Solr.'); + + $this->logger->notice('Query: ' . urldecode($query)); + $options['query'] = urldecode($query); + + $query_object = $this->solr->createSelect($options); + $query_object->setResponseWriter($options['wt']); + + if ($options['defType']) { + $query_object->addParam('defType', $options['defType']); + } + if ($options['omitHeader']) { + $query_object->setOmitHeader(TRUE); + } + if ($options['qf']) { + $query_object->addParam('qf', $options['qf']); + } + + $query_object->addParam('TZ', 'UTC'); + + $result = $this->solr->execute($query_object); + + if ($result instanceof ResultInterface) { + $this->logger->notice('Query executed successfully.'); + $this->logger->notice('Query result:'); + $this->logger->notice(json_encode($result->getData(), JSON_PRETTY_PRINT)); + } + else { + $this->logger->error('Query failed.'); + $this->logger->error('Query result:'); + $this->logger->error(json_encode($result->getData(), JSON_PRETTY_PRINT)); + } + } + +}