Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
062e138c
Commit
062e138c
authored
Dec 05, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Method "\yii\mongo\Collection::fullTextSearch()" added.
parent
99b6ae27
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
1 deletion
+89
-1
Collection.php
extensions/mongo/Collection.php
+45
-1
CollectionTest.php
tests/unit/extensions/mongo/CollectionTest.php
+32
-0
MongoTestCase.php
tests/unit/extensions/mongo/MongoTestCase.php
+12
-0
No files found.
extensions/mongo/Collection.php
View file @
062e138c
...
@@ -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.
...
...
tests/unit/extensions/mongo/CollectionTest.php
View file @
062e138c
...
@@ -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
tests/unit/extensions/mongo/MongoTestCase.php
View file @
062e138c
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment