Skip to content

Full Text Search #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
81 changes: 81 additions & 0 deletions tests/Parse/ParseQueryFullTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Parse\Test;

use Parse\ParseException;
use Parse\ParseObject;
use Parse\ParseQuery;

class ParseQueryFullTextTest extends \PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
Helper::setUp();
}

public function setUp()
{
Helper::clearClass('TestObject');
}

public function tearDown()
{
Helper::tearDown();
}

/**
* This function used as a helper function in test functions to save objects.
*/
public function provideTestObjects()
{
$subjects = [
'coffee',
'Coffee Shopping',
'Baking a cake',
'baking',
'Café Con Leche',
'Сырники',
'coffee and cream',
'Cafe con Leche'
];

$allObjects = [];
for ($i = 0; $i < count($subjects); ++$i) {
$obj = new ParseObject('TestObject');
$obj->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'));
}
}