Skip to content

Commit 9ecd520

Browse files
dplewisBenjamin Wilson Friedman
authored and
Benjamin Wilson Friedman
committed
Full Text Search (#333)
1 parent b4ef08f commit 9ecd520

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

src/Parse/ParseQuery.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function endsWith($key, $value)
284284
return $this;
285285
}
286286

287-
/**
287+
/**
288288
* Adds a constraint for finding string values that contain a provided
289289
* string. This may be slow for large datasets.
290290
*
@@ -300,6 +300,26 @@ public function contains($key, $value)
300300
return $this;
301301
}
302302

303+
/**
304+
* Adds a constraint for finding string values that contain a provided
305+
* string using Full Text Search
306+
*
307+
* @param string $key The key to check.
308+
* @param mixed $value The substring that the value must contain.
309+
*
310+
* @return ParseQuery Returns this query, so you can chain this call.
311+
*/
312+
public function fullText($key, $value)
313+
{
314+
$this->addCondition(
315+
$key,
316+
'$text',
317+
['$search' => ['$term' => $value]]
318+
);
319+
320+
return $this;
321+
}
322+
303323
/**
304324
* Returns an associative array of the query constraints.
305325
*
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Parse\Test;
4+
5+
use Parse\ParseException;
6+
use Parse\ParseObject;
7+
use Parse\ParseQuery;
8+
9+
class ParseQueryFullTextTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public static function setUpBeforeClass()
12+
{
13+
Helper::setUp();
14+
}
15+
16+
public function setUp()
17+
{
18+
Helper::clearClass('TestObject');
19+
}
20+
21+
public function tearDown()
22+
{
23+
Helper::tearDown();
24+
}
25+
26+
/**
27+
* This function used as a helper function in test functions to save objects.
28+
*/
29+
public function provideTestObjects()
30+
{
31+
$subjects = [
32+
'coffee',
33+
'Coffee Shopping',
34+
'Baking a cake',
35+
'baking',
36+
'Café Con Leche',
37+
'Сырники',
38+
'coffee and cream',
39+
'Cafe con Leche'
40+
];
41+
42+
$allObjects = [];
43+
for ($i = 0; $i < count($subjects); ++$i) {
44+
$obj = new ParseObject('TestObject');
45+
$obj->set('subject', $subjects[$i]);
46+
$allObjects[] = $obj;
47+
}
48+
ParseObject::saveAll($allObjects);
49+
}
50+
51+
public function testFullTextQuery()
52+
{
53+
$this->provideTestObjects();
54+
$query = new ParseQuery('TestObject');
55+
$query->fullText('subject', 'coffee');
56+
$results = $query->find();
57+
$this->assertEquals(
58+
3,
59+
count($results),
60+
'Did not return correct objects.'
61+
);
62+
}
63+
64+
public function testFullTextSort()
65+
{
66+
$this->provideTestObjects();
67+
$query = new ParseQuery('TestObject');
68+
$query->fullText('subject', 'coffee');
69+
$query->ascending('$score');
70+
$query->select('$score');
71+
$results = $query->find();
72+
$this->assertEquals(
73+
3,
74+
count($results),
75+
'Did not return correct number of objects.'
76+
);
77+
$this->assertEquals(1, $results[0]->get('score'));
78+
$this->assertEquals(0.75, $results[1]->get('score'));
79+
$this->assertEquals(0.75, $results[2]->get('score'));
80+
}
81+
}

0 commit comments

Comments
 (0)