Commit 062e138c by Klimov Paul

Method "\yii\mongo\Collection::fullTextSearch()" added.

parent 99b6ae27
...@@ -176,6 +176,7 @@ class Collection extends Object ...@@ -176,6 +176,7 @@ class Collection extends Object
* @param string|array $columns column name or list of column names. * @param string|array $columns column name or list of column names.
* If array is given, each element in the array has as key the field name, and as * If array is given, each element in the array has as key the field name, and as
* value either 1 for ascending sort, or -1 for descending sort. * value either 1 for ascending sort, or -1 for descending sort.
* Use value 'text' to specify text index.
* You can specify field using native numeric key with the field name as a value, * You can specify field using native numeric key with the field name as a value,
* in this case ascending sort will be used. * in this case ascending sort will be used.
* For example: * For example:
...@@ -183,6 +184,7 @@ class Collection extends Object ...@@ -183,6 +184,7 @@ class Collection extends Object
* [ * [
* 'name', * 'name',
* 'status' => -1, * 'status' => -1,
* 'description' => 'text',
* ] * ]
* ~~~ * ~~~
* @throws Exception on failure. * @throws Exception on failure.
...@@ -540,7 +542,6 @@ class Collection extends Object ...@@ -540,7 +542,6 @@ class Collection extends Object
if (!empty($condition)) { if (!empty($condition)) {
$command['query'] = $this->buildCondition($condition); $command['query'] = $this->buildCondition($condition);
} }
$token = $this->composeLogToken('mapReduce', [$map, $reduce, $out]); $token = $this->composeLogToken('mapReduce', [$map, $reduce, $out]);
Yii::info($token, __METHOD__); Yii::info($token, __METHOD__);
try { try {
...@@ -557,6 +558,49 @@ class Collection extends Object ...@@ -557,6 +558,49 @@ class Collection extends Object
} }
/** /**
* Performs full text search.
* @param string $search string of terms that MongoDB parses and uses to query the text index.
* @param array $condition criteria for filtering a results list.
* @param array $fields list of fields to be returned in result.
* @param integer $limit the maximum number of documents to include in the response (by default 100).
* @param string $language he language that determines the list of stop words for the search
* and the rules for the stemmer and tokenizer. If not specified, the search uses the default
* language of the index.
* @return array the highest scoring documents, in descending order by score.
* @throws Exception on failure.
*/
public function fullTextSearch($search, $condition = [], $fields = [], $limit = null, $language = null) {
$command = [
'search' => $search
];
if (!empty($condition)) {
$command['filter'] = $this->buildCondition($condition);
}
if (!empty($fields)) {
$command['project'] = $fields;
}
if ($limit !== null) {
$command['limit'] = $limit;
}
if ($language !== null) {
$command['language'] = $language;
}
$token = $this->composeLogToken('text', $command);
Yii::info($token, __METHOD__);
try {
Yii::beginProfile($token, __METHOD__);
$command = array_merge(['text' => $this->getName()], $command);
$result = $this->mongoCollection->db->command($command);
$this->tryResultError($result);
Yii::endProfile($token, __METHOD__);
return $result['results'];
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* Checks if command execution result ended with an error. * Checks if command execution result ended with an error.
* @param mixed $result raw command execution result. * @param mixed $result raw command execution result.
* @throws Exception if an error occurred. * @throws Exception if an error occurred.
......
...@@ -279,4 +279,35 @@ class CollectionTest extends MongoTestCase ...@@ -279,4 +279,35 @@ class CollectionTest extends MongoTestCase
$indexInfo = $collection->mongoCollection->getIndexInfo(); $indexInfo = $collection->mongoCollection->getIndexInfo();
$this->assertEquals(1, count($indexInfo)); $this->assertEquals(1, count($indexInfo));
} }
/**
* @depends testBatchInsert
* @depends testCreateIndex
*/
public function testFullTextSearch()
{
if (version_compare('2.4', $this->getServerVersion(), '>')) {
$this->markTestSkipped("Mongo Server 2.4 required.");
}
$collection = $this->getConnection()->getCollection('customer');
$rows = [
[
'name' => 'customer 1',
'status' => 1,
'amount' => 100,
],
[
'name' => 'some customer',
'status' => 1,
'amount' => 200,
],
];
$collection->batchInsert($rows);
$collection->createIndex(['name' => 'text']);
$result = $collection->fullTextSearch('some');
$this->assertNotEmpty($result);
}
} }
\ No newline at end of file
...@@ -135,4 +135,15 @@ class MongoTestCase extends TestCase ...@@ -135,4 +135,15 @@ class MongoTestCase extends TestCase
} }
return $result; return $result;
} }
/**
* Returns the Mongo server version.
* @return string Mongo server version.
*/
protected function getServerVersion()
{
$connection = $this->getConnection();
$buildInfo = $connection->getDatabase()->executeCommand(['buildinfo' => true]);
return $buildInfo['version'];
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment