diff --git a/extensions/sphinx/Command.php b/extensions/sphinx/Command.php index a31f79a..c54861f 100644 --- a/extensions/sphinx/Command.php +++ b/extensions/sphinx/Command.php @@ -496,13 +496,29 @@ class Command extends Component return $this->setSql($sql); } - public function callSnippets($index, $source, $query, array $options = []) + /** + * Builds a snippet from provided data and query, using specified index settings. + * @param string $index name of the index, from which to take the text processing settings. + * @param string|array $source is the source data to extract a snippet from. + * It could be either a single string or array of strings. + * @param string $query the full-text query to build snippets for. + * @param array $options list of options in format: optionName => optionValue + * @return static the command object itself + */ + public function callSnippets($index, $source, $query, $options = []) { $params = []; $sql = $this->db->getQueryBuilder()->callSnippets($index, $source, $query, $options, $params); return $this->setSql($sql)->bindValues($params); } + /** + * Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics. + * @param string $index the name of the index from which to take the text processing settings + * @param string $text the text to break down to keywords. + * @param boolean $fetchStatistic whether to return document and hit occurrence statistics + * @return string the SQL statement for call keywords. + */ public function callKeywords($index, $text, $fetchStatistic = false) { $params = []; diff --git a/extensions/sphinx/QueryBuilder.php b/extensions/sphinx/QueryBuilder.php index f463a0e..e6d110e 100644 --- a/extensions/sphinx/QueryBuilder.php +++ b/extensions/sphinx/QueryBuilder.php @@ -269,15 +269,26 @@ class QueryBuilder extends Object } /** - * Builds a SQL statement for truncating a DB index. + * Builds a SQL statement for truncating an index. * @param string $index the index to be truncated. The name will be properly quoted by the method. - * @return string the SQL statement for truncating a DB index. + * @return string the SQL statement for truncating an index. */ public function truncateIndex($index) { return 'TRUNCATE RTINDEX ' . $this->db->quoteIndexName($index); } + /** + * Builds a SQL statement for call snippet from provided data and query, using specified index settings. + * @param string $index name of the index, from which to take the text processing settings. + * @param string|array $source is the source data to extract a snippet from. + * It could be either a single string or array of strings. + * @param string $query the full-text query to build snippets for. + * @param array $options list of options in format: optionName => optionValue + * @param array $params the binding parameters that will be modified by this method + * so that they can be bound to the Sphinx command later. + * @return string the SQL statement for call snippets. + */ public function callSnippets($index, $source, $query, $options, &$params) { if (is_array($source)) { @@ -297,10 +308,30 @@ class QueryBuilder extends Object $params[$indexParamName] = $index; $queryParamName = self::PARAM_PREFIX . count($params); $params[$queryParamName] = $query; - $optionSql = ''; // @todo + if (!empty($options)) { + $optionParts = []; + foreach ($options as $name => $value) { + $phName = self::PARAM_PREFIX . count($params); + $params[$phName] = $value; + $optionParts[] = $phName . ' AS ' . $name; + } + $optionSql = ', ' . implode(', ', $optionParts); + } else { + $optionSql = ''; + } return 'CALL SNIPPETS(' . $dataSql. ', ' . $indexParamName . ', ' . $queryParamName . $optionSql. ')'; } + /** + * Builds a SQL statement for returning tokenized and normalized forms of the keywords, and, + * optionally, keyword statistics. + * @param string $index the name of the index from which to take the text processing settings + * @param string $text the text to break down to keywords. + * @param boolean $fetchStatistic whether to return document and hit occurrence statistics + * @param array $params the binding parameters that will be modified by this method + * so that they can be bound to the Sphinx command later. + * @return string the SQL statement for call keywords. + */ public function callKeywords($index, $text, $fetchStatistic, &$params) { $indexParamName = self::PARAM_PREFIX . count($params); diff --git a/tests/unit/extensions/sphinx/CommandTest.php b/tests/unit/extensions/sphinx/CommandTest.php index 9183b6c..af34c4f 100644 --- a/tests/unit/extensions/sphinx/CommandTest.php +++ b/tests/unit/extensions/sphinx/CommandTest.php @@ -253,17 +253,35 @@ class CommandTest extends SphinxTestCase $this->assertEquals(0, count($rows), 'Unable to delete record!'); } + /** + * @depends testQuery + */ public function testCallSnippets() { $db = $this->getConnection(); $query = 'pencil'; - $data = ['Some data sentence about ' . $query]; - $rows = $db->createCommand()->callSnippets('yii2_test_item_index', $data, $query)->queryColumn(); + $source = 'Some data sentence about ' . $query; + + $rows = $db->createCommand()->callSnippets('yii2_test_item_index', $source, $query)->queryColumn(); $this->assertNotEmpty($rows, 'Unable to call snippets!'); $this->assertContains('' . $query . '', $rows[0], 'Query not present in the snippet!'); + + $rows = $db->createCommand()->callSnippets('yii2_test_item_index', [$source], $query)->queryColumn(); + $this->assertNotEmpty($rows, 'Unable to call snippets for array source!'); + + $options = [ + 'before_match' => '[', + 'after_match' => ']', + 'limit' => 20, + ]; + $snippet = $db->createCommand()->callSnippets('yii2_test_item_index', $source, $query, $options)->queryScalar(); + $this->assertContains($options['before_match'] . $query . $options['after_match'], $snippet, 'Unable to apply options!'); } + /** + * @depends testQuery + */ public function testCallKeywords() { $db = $this->getConnection();