Passed
Push — master ( 6deaee...64bf45 )
by Gordon
04:46 queued 02:47
created

SearchFixturesTest::checkSumDocumentCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\ManticoreSearch\Tests\Service;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Dev\SapphireTest;
7
use Suilven\FreeTextSearch\Container\Facet;
8
use Suilven\FreeTextSearch\Helper\BulkIndexingHelper;
9
use Suilven\FreeTextSearch\Indexes;
10
use Suilven\FreeTextSearch\Types\SearchParamTypes;
11
use Suilven\ManticoreSearch\Helper\ReconfigureIndexesHelper;
12
use Suilven\ManticoreSearch\Service\Searcher;
13
use Suilven\ManticoreSearch\Tests\Models\FlickrAuthor;
14
use Suilven\ManticoreSearch\Tests\Models\FlickrPhoto;
15
use Suilven\ManticoreSearch\Tests\Models\FlickrSet;
16
use Suilven\ManticoreSearch\Tests\Models\FlickrTag;
17
18
class SearchFixturesTest extends SapphireTest
19
{
20
    protected static $fixture_file = ['tests/fixtures/sitetree.yml', 'tests/fixtures/flickrphotos.yml'];
21
22
    protected static $extra_dataobjects = [
23
        FlickrPhoto::class,
24
        FlickrTag::class,
25
        FlickrSet::class,
26
        FlickrAuthor::class,
27
    ];
28
29
    /** @var int */
30
    private static $pageID;
0 ignored issues
show
introduced by
The private property $pageID is not used, and could be removed.
Loading history...
31
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
36
        /** @var \Suilven\FreeTextSearch\Indexes $indexesService */
37
        $indexesService = new Indexes();
38
        $indexesArray = $indexesService->getIndexes();
39
        $helper = new ReconfigureIndexesHelper();
40
        $helper->reconfigureIndexes($indexesArray);
41
42
        $helper = new BulkIndexingHelper();
43
        $helper->bulkIndex('sitetree');
44
        $helper->bulkIndex('flickrphotos');
45
        $helper->bulkIndex('members');
46
    }
47
48
49
    public function testSimilar(): void
50
    {
51
        $page = $this->objFromFixture(SiteTree::class, 'sitetree_49');
52
        $searcher = new Searcher();
53
        $searcher->setIndexName('sitetree');
54
        $result = $searcher->searchForSimilar($page);
55
56
        // 1 is the original doc, 3 others contain sheep, 1 contains mussy an 1 contains shuttlecock
57
        $this->assertEquals(6, $result->getTotaNumberOfResults());
58
        $hits = $result->getRecords();
59
        $ids = [];
60
        foreach ($hits as $hit) {
61
            $ids[] = $hit->ID;
62
        }
63
64
        $this->assertEquals([49, 40, 45, 21, 36, 47], $ids);
65
    }
66
67
68
    public function testAndSearch(): void
69
    {
70
        $searcher = new Searcher();
71
        $searcher->setIndexName('sitetree');
72
        $searcher->setSearchType(SearchParamTypes::AND);
73
        $result = $searcher->search('sheep shuttlecock');
74
        $this->assertEquals(1, $result->getTotaNumberOfResults());
75
        $hits = $result->getRecords();
76
        $ids = [];
77
        foreach ($hits as $hit) {
78
            $ids[] = $hit->ID;
79
        }
80
        $this->assertEquals([49], $ids);
81
    }
82
83
84
    public function testORSearch(): void
85
    {
86
        $searcher = new Searcher();
87
        $searcher->setIndexName('sitetree');
88
        $searcher->setSearchType(SearchParamTypes::OR);
89
        $result = $searcher->search('sheep shuttlecock');
90
        $this->assertEquals(5, $result->getTotaNumberOfResults());
91
        $hits = $result->getRecords();
92
        $ids = [];
93
        foreach ($hits as $hit) {
94
            $ids[] = $hit->ID;
95
        }
96
        $this->assertEquals([49, 45, 21, 36, 47], $ids);
97
    }
98
99
100
    public function testFlickrFacetsEmptySearchTerm(): void
101
    {
102
        $searcher = new Searcher();
103
        $searcher->setIndexName('flickrphotos');
104
        $searcher->setSearchType(SearchParamTypes::AND);
105
        $searcher->setFacettedTokens(['ISO', 'Aperture', 'Orientation']);
106
        $result = $searcher->search('*');
107
        $this->assertEquals(50, $result->getTotaNumberOfResults());
108
        $hits = $result->getRecords();
109
        $ids = [];
110
        foreach ($hits as $hit) {
111
            $ids[] = $hit->ID;
112
        }
113
        $this->assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], $ids);
114
115
        $facets = $result->getFacets();
116
        $this->assertEquals([
117
            1600 => 7,
118
            800 => 11,
119
            400 => 6,
120
            200 => 8,
121
            100 => 10,
122
            64 => 4,
123
            25 => 4,
124
        ], $facets[0]->asKeyValueArray());
125
126
        $this->assertEquals([
127
            32 => 1,
128
            27 => 9,
129
            22 => 6,
130
            16 => 7,
131
            11 => 7,
132
            8 => 5,
133
            5 => 6,
134
            4 => 3,
135
            2 => 6,
136
        ], $facets[1]->asKeyValueArray());
137
138
        $this->assertEquals([
139
            90 => 20,
140
            0 => 30,
141
        ], $facets[2]->asKeyValueArray());
142
143
        $this->checkSumDocumentCount($facets[0], 50);
144
        $this->checkSumDocumentCount($facets[1], 50);
145
        $this->checkSumDocumentCount($facets[2], 50);
146
    }
147
148
149
    public function testFlickrFacetsIncludeSearchTerm(): void
150
    {
151
        $searcher = new Searcher();
152
        $searcher->setIndexName('flickrphotos');
153
        $searcher->setSearchType(SearchParamTypes::AND);
154
        $searcher->setFacettedTokens(['ISO', 'Aperture', 'Orientation']);
155
        $result = $searcher->search('Tom');
156
        $this->assertEquals(13, $result->getTotaNumberOfResults());
157
        $hits = $result->getRecords();
158
        $ids = [];
159
        foreach ($hits as $hit) {
160
            $ids[] = $hit->ID;
161
        }
162
163
        $this->assertEquals([5, 9, 12, 16, 23, 24, 29, 32, 34, 43, 46, 47, 48], $ids);
164
165
        /** @var array<\Suilven\FreeTextSearch\Container\Facet> $facets */
166
        $facets = $result->getFacets();
167
168
        $this->assertEquals('ISO', $facets[0]->getName());
169
        $this->assertEquals('Aperture', $facets[1]->getName());
170
        $this->assertEquals('Orientation', $facets[2]->getName());
171
172
173
        $this->assertEquals([
174
            1600 => 2,
175
            800 => 3,
176
            400 => 1,
177
            200 => 2,
178
            100 => 2,
179
            25 => 3,
180
        ], $facets[0]->asKeyValueArray());
181
182
183
        $this->assertEquals([
184
            27 => 1,
185
            16 => 3,
186
            11 => 2,
187
            8 => 3,
188
            5 => 1,
189
            2 => 3,
190
        ], $facets[1]->asKeyValueArray());
191
192
        $this->assertEquals([
193
            90 => 5,
194
            0 => 8,
195
        ], $facets[2]->asKeyValueArray());
196
197
        $this->checkSumDocumentCount($facets[0], 13);
198
        $this->checkSumDocumentCount($facets[1], 13);
199
        $this->checkSumDocumentCount($facets[2], 13);
200
    }
201
202
203
    private function checkSumDocumentCount(Facet $facet, int $expectedCount): void
204
    {
205
        $sum = 0;
206
        $kvArray = $facet->asKeyValueArray();
207
208
        /** @var \Suilven\FreeTextSearch\Container\FacetCount $key */
209
        foreach (\array_keys($kvArray) as $key) {
210
            $sum += $kvArray[$key];
211
        }
212
213
        $this->assertEquals($expectedCount, $sum);
214
    }
215
}
216