diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index 07a23705..72d77621 100755 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -284,7 +284,7 @@ public function endsWith($key, $value) return $this; } - /** + /** * Adds a constraint for finding string values that contain a provided * string. This may be slow for large datasets. * @@ -300,6 +300,26 @@ public function contains($key, $value) return $this; } + /** + * Adds a constraint for finding string values that contain a provided + * string using Full Text Search + * + * @param string $key The key to check. + * @param mixed $value The substring that the value must contain. + * + * @return ParseQuery Returns this query, so you can chain this call. + */ + public function fullText($key, $value) + { + $this->addCondition( + $key, + '$text', + ['$search' => ['$term' => $value]] + ); + + return $this; + } + /** * Returns an associative array of the query constraints. * diff --git a/tests/Parse/ParseQueryFullTextTest.php b/tests/Parse/ParseQueryFullTextTest.php new file mode 100644 index 00000000..5dfcb2c6 --- /dev/null +++ b/tests/Parse/ParseQueryFullTextTest.php @@ -0,0 +1,81 @@ +set('subject', $subjects[$i]); + $allObjects[] = $obj; + } + ParseObject::saveAll($allObjects); + } + + public function testFullTextQuery() + { + $this->provideTestObjects(); + $query = new ParseQuery('TestObject'); + $query->fullText('subject', 'coffee'); + $results = $query->find(); + $this->assertEquals( + 3, + count($results), + 'Did not return correct objects.' + ); + } + + public function testFullTextSort() + { + $this->provideTestObjects(); + $query = new ParseQuery('TestObject'); + $query->fullText('subject', 'coffee'); + $query->ascending('$score'); + $query->select('$score'); + $results = $query->find(); + $this->assertEquals( + 3, + count($results), + 'Did not return correct number of objects.' + ); + $this->assertEquals(1, $results[0]->get('score')); + $this->assertEquals(0.75, $results[1]->get('score')); + $this->assertEquals(0.75, $results[2]->get('score')); + } +}