1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
7
|
|
|
* |
8
|
|
|
* @license GNU General Public License version 3 or later. |
9
|
|
|
* For the full copyright and license information, please read the |
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Tests\Functional\Common; |
14
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\AbstractDocument; |
16
|
|
|
use Kitodo\Dlf\Common\Indexer; |
17
|
|
|
use Kitodo\Dlf\Common\Solr\Solr; |
18
|
|
|
use Kitodo\Dlf\Domain\Model\SolrCore; |
19
|
|
|
use Kitodo\Dlf\Domain\Repository\CollectionRepository; |
20
|
|
|
use Kitodo\Dlf\Domain\Repository\DocumentRepository; |
21
|
|
|
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository; |
22
|
|
|
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase; |
23
|
|
|
use TYPO3\CMS\Core\Core\Bootstrap; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
|
26
|
|
|
class SolrIndexingTest extends FunctionalTestCase |
27
|
|
|
{ |
28
|
|
|
/** @var CollectionRepository */ |
29
|
|
|
protected $collectionRepository; |
30
|
|
|
|
31
|
|
|
/** @var DocumentRepository */ |
32
|
|
|
protected $documentRepository; |
33
|
|
|
|
34
|
|
|
/** @var SolrCoreRepository */ |
35
|
|
|
protected $solrCoreRepository; |
36
|
|
|
|
37
|
|
|
public function setUp(): void |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
|
41
|
|
|
// Needed for Indexer::add, which uses the language service |
42
|
|
|
Bootstrap::initializeLanguageObject(); |
43
|
|
|
|
44
|
|
|
$this->collectionRepository = $this->initializeRepository(CollectionRepository::class, 20000); |
45
|
|
|
$this->documentRepository = $this->initializeRepository(DocumentRepository::class, 20000); |
46
|
|
|
$this->solrCoreRepository = $this->initializeRepository(SolrCoreRepository::class, 20000); |
47
|
|
|
|
48
|
|
|
$this->importCSVDataSet(__DIR__ . '/../../Fixtures/Common/documents_1.csv'); |
49
|
|
|
$this->importCSVDataSet(__DIR__ . '/../../Fixtures/Common/libraries.csv'); |
50
|
|
|
$this->importCSVDataSet(__DIR__ . '/../../Fixtures/Common/metadata.csv'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function canCreateCore() |
57
|
|
|
{ |
58
|
|
|
$coreName = uniqid('testCore'); |
59
|
|
|
$solr = Solr::getInstance($coreName); |
60
|
|
|
self::assertNull($solr->core); |
61
|
|
|
|
62
|
|
|
$actualCoreName = Solr::createCore($coreName); |
63
|
|
|
self::assertEquals($actualCoreName, $coreName); |
64
|
|
|
|
65
|
|
|
$solr = Solr::getInstance($coreName); |
66
|
|
|
self::assertNotNull($solr->core); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
*/ |
72
|
|
|
public function canIndexAndSearchDocument() |
73
|
|
|
{ |
74
|
|
|
$core = $this->createSolrCore(); |
75
|
|
|
|
76
|
|
|
$document = $this->documentRepository->findByUid(1001); |
77
|
|
|
$document->setSolrcore($core->model->getUid()); |
78
|
|
|
$this->persistenceManager->persistAll(); |
79
|
|
|
|
80
|
|
|
$doc = AbstractDocument::getInstance($document->getLocation(), ['useExternalApisForMetadata' => 0]); |
81
|
|
|
$document->setCurrentDocument($doc); |
82
|
|
|
|
83
|
|
|
$indexingSuccessful = Indexer::add($document, $this->documentRepository); |
|
|
|
|
84
|
|
|
self::assertTrue($indexingSuccessful); |
85
|
|
|
|
86
|
|
|
$solrSettings = [ |
87
|
|
|
'solrcore' => $core->solr->core, |
88
|
|
|
'storagePid' => $document->getPid(), |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$solrSearch = $this->documentRepository->findSolrWithoutCollection($solrSettings, ['query' => '*']); |
92
|
|
|
$solrSearch->getQuery()->execute(); |
93
|
|
|
self::assertEquals(1, count($solrSearch)); |
94
|
|
|
self::assertEquals(15, $solrSearch->getNumFound()); |
95
|
|
|
|
96
|
|
|
// Check that the title stored in Solr matches the title of database entry |
97
|
|
|
$docTitleInSolr = false; |
98
|
|
|
foreach ($solrSearch->getSolrResults()['documents'] as $solrDoc) { |
99
|
|
|
if ($solrDoc['toplevel'] && intval($solrDoc['uid']) === intval($document->getUid())) { |
100
|
|
|
self::assertEquals($document->getTitle(), $solrDoc['title']); |
101
|
|
|
$docTitleInSolr = true; |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
self::assertTrue($docTitleInSolr); |
106
|
|
|
|
107
|
|
|
// $solrSearch[0] is hydrated from the database model |
108
|
|
|
self::assertEquals($document->getTitle(), $solrSearch[0]['title']); |
109
|
|
|
|
110
|
|
|
// Test ArrayAccess and Iterator implementation |
111
|
|
|
self::assertTrue(isset($solrSearch[0])); |
112
|
|
|
self::assertFalse(isset($solrSearch[1])); |
113
|
|
|
self::assertNull($solrSearch[1]); |
114
|
|
|
self::assertFalse(isset($solrSearch[$document->getUid()])); |
115
|
|
|
|
116
|
|
|
$iter = []; |
117
|
|
|
foreach ($solrSearch as $key => $value) { |
118
|
|
|
$iter[$key] = $value; |
119
|
|
|
} |
120
|
|
|
self::assertEquals(1, count($iter)); |
121
|
|
|
self::assertEquals($solrSearch[0], $iter[0]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
*/ |
127
|
|
|
public function canSearchInCollections() |
128
|
|
|
{ |
129
|
|
|
$core = $this->createSolrCore(); |
130
|
|
|
|
131
|
|
|
$this->importCSVDataSet(__DIR__ . '/../../Fixtures/Common/documents_fulltext.csv'); |
132
|
|
|
$this->importSolrDocuments($core->solr, __DIR__ . '/../../Fixtures/Common/documents_1.solr.json'); |
133
|
|
|
$this->importSolrDocuments($core->solr, __DIR__ . '/../../Fixtures/Common/documents_fulltext.solr.json'); |
134
|
|
|
|
135
|
|
|
$collections = $this->collectionRepository->findCollectionsBySettings([ |
136
|
|
|
'index_name' => ['Musik', 'Projekt: Dresdner Hefte'], |
137
|
|
|
]); |
138
|
|
|
$musik[] = $collections[0]; |
139
|
|
|
$dresdnerHefte[] = $collections[1]; |
140
|
|
|
|
141
|
|
|
$settings = [ |
142
|
|
|
'solrcore' => $core->solr->core, |
143
|
|
|
'storagePid' => 20000, |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
// No query: Only list toplevel result(s) in collection(s) |
147
|
|
|
$musikSearch = $this->documentRepository->findSolrByCollections($musik, $settings, []); |
148
|
|
|
$dresdnerHefteSearch = $this->documentRepository->findSolrByCollections($dresdnerHefte, $settings, []); |
149
|
|
|
$multiCollectionSearch = $this->documentRepository->findSolrByCollections($collections, $settings, []); |
150
|
|
|
self::assertGreaterThanOrEqual(1, $musikSearch->getNumFound()); |
151
|
|
|
self::assertGreaterThanOrEqual(1, $dresdnerHefteSearch->getNumFound()); |
152
|
|
|
self::assertEquals('533223312LOG_0000', $dresdnerHefteSearch->getSolrResults()['documents'][0]['id']); |
153
|
|
|
self::assertEquals( |
154
|
|
|
// Assuming there's no overlap |
155
|
|
|
$dresdnerHefteSearch->getNumFound() + $musikSearch->getNumFound(), |
156
|
|
|
$multiCollectionSearch->getNumFound() |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
// With query: List all results |
160
|
|
|
$metadataSearch = $this->documentRepository->findSolrByCollection($collections[1], $settings, ['query' => 'Dresden']); |
161
|
|
|
$fulltextSearch = $this->documentRepository->findSolrByCollection($collections[1], $settings, ['query' => 'Dresden', 'fulltext' => '1']); |
162
|
|
|
self::assertGreaterThan($metadataSearch->getNumFound(), $fulltextSearch->getNumFound()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @test |
167
|
|
|
*/ |
168
|
|
|
public function canGetIndexFieldName() |
169
|
|
|
{ |
170
|
|
|
$this->assertEquals('title_usi', Indexer::getIndexFieldName('title', 20000)); |
171
|
|
|
$this->assertEquals('year_uuu', Indexer::getIndexFieldName('year', 20000)); |
172
|
|
|
$this->assertEquals('', Indexer::getIndexFieldName('title')); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function createSolrCore(): object |
176
|
|
|
{ |
177
|
|
|
$coreName = Solr::createCore(); |
178
|
|
|
$solr = Solr::getInstance($coreName); |
179
|
|
|
|
180
|
|
|
$model = GeneralUtility::makeInstance(SolrCore::class); |
181
|
|
|
$model->setLabel('Testing Solr Core'); |
182
|
|
|
$model->setIndexName($coreName); |
183
|
|
|
$this->solrCoreRepository->add($model); |
184
|
|
|
$this->persistenceManager->persistAll(); |
185
|
|
|
|
186
|
|
|
return (object) compact('solr', 'model'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|