/
Indexer.php
61 lines (50 loc) · 1.35 KB
/
Indexer.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
<?php
namespace FS\SolrBundle\Indexer;
use FS\SolrBundle\Doctrine\Mapper\Mapping\CommandFactory;
use FS\SolrBundle\SolrConnection;
use FS\SolrBundle\Doctrine\Mapper\EntityMapper;
use FS\SolrBundle\Doctrine\Mapper\MetaInformation;
class Indexer implements IndexerInterface {
/**
*
* @var EntityMapper
*/
private $mapper = null;
/**
*
* @var CommandFactory
*/
private $commandFactory = null;
/**
*
* @var \SolrClient
*/
private $solrClient = null;
/**
*
* @param SolrConnection $connection
* @param CommandFactory $commandFactory
*/
public function __construct(SolrConnection $connection, CommandFactory $commandFactory) {
$this->solrClient = $connection->getClient();
$this->commandFactory = $commandFactory;
$this->mapper = new EntityMapper();
}
/* (non-PHPdoc)
* @see FS\SolrBundle\Indexer.IndexerInterface::toIndex()
*/
public function toIndex(MetaInformation $metaInformation) {
$command = $this->commandFactory->get('all');
$this->mapper->setMappingCommand($command);
$document = $this->mapper->toDocument($metaInformation);
$this->addDocumentToIndex($document);
}
private function addDocumentToIndex($doc) {
try {
$updateResponse = $this->solrClient->addDocument($doc);
$this->solrClient->commit();
} catch (\Exception $e) {
throw new IndexerException('could not index entity');
}
}
}