This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
SearchFacetTest.php
99 lines (89 loc) Β· 2.98 KB
/
SearchFacetTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace AlgoliaSearch\Tests;
use AlgoliaSearch\AlgoliaException;
use AlgoliaSearch\Client;
class SearchFacetTest extends AlgoliaSearchTestCase
{
private $client;
/** @var \AlgoliaSearch\Index */
private $index;
protected function setUp()
{
$this->client = new Client(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_API_KEY'));
$this->index = $this->client->initIndex($this->safe_name('Γ lgol?Γ -php'));
try {
$this->index->clearIndex();
} catch (AlgoliaException $e) {
// not fatal
}
}
protected function tearDown()
{
try {
$this->client->deleteIndex($this->safe_name('Γ lgol?Γ -php'));
} catch (AlgoliaException $e) {
// not fatal
}
}
public function testSearchTest()
{
$settings = array(
'attributesForFaceting' => array('searchable(series)', 'kind')
);
$objects = array(
array(
'objectID' => '1',
'name' => 'Snoopy',
'kind' => array('dog', 'animal'),
'born' => 1950,
'series' => 'Peanuts'
),
array(
'objectID' => '2',
'name' => 'Woodstock',
'kind' => array('bird', 'animal'),
'born' => 1960,
'series' => 'Peanuts'
),
array(
'objectID' => '3',
'name' => 'Charlie Brown',
'kind' => array('human'),
'born' => 1950,
'series' => 'Peanuts'
),
array(
'objectID' => '4',
'name' => 'Hobbes',
'kind' => array('tiger', 'animal', 'teddy'),
'born' => 1985,
'series' => 'Calvin & Hobbes'
),
array(
'objectID' => '5',
'name' => 'Calvin',
'kind' => array('human'),
'born' => 1985,
'series' => 'Calvin & Hobbes'
)
);
$this->index->setSettings($settings);
$task = $this->index->addObjects($objects);
$this->index->waitTask($task['taskID']);
# Straightforward search.
$facetHits = $this->index->searchFacet('series', 'Hobb');
$facetHits = $facetHits['facetHits'];
$this->assertEquals(count($facetHits), 1);
$this->assertEquals($facetHits[0]['value'], 'Calvin & Hobbes');
$this->assertEquals($facetHits[0]['count'], 2);
# Using an addition query to restrict search.
$query = array(
'facetFilters' => 'kind:animal',
'numericFilters' => 'born >= 1955'
);
$facetHits = $this->index->searchFacet('series', 'Peanutz', $query);
$facetHits = $facetHits['facetHits'];
$this->assertEquals($facetHits[0]['value'], 'Peanuts');
$this->assertEquals($facetHits[0]['count'], 1);
}
}