1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Solr\Bridge; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Solr\Exception\ServerException; |
14
|
|
|
use Solr\Options\ModuleOptions; |
15
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Manage connection with the SolrServer |
19
|
|
|
* |
20
|
|
|
* @author Anthonius Munthi <[email protected]> |
21
|
|
|
* @since 0.27 |
22
|
|
|
* @package Solr\Bridge |
23
|
|
|
*/ |
24
|
|
|
class Manager |
25
|
|
|
{ |
26
|
|
|
const SOLR_DATE_FORMAT = 'Y-m-d\TH:i:s\Z'; |
27
|
|
|
const SORT_ASCENDING = 0; |
28
|
|
|
const SORT_DESCENDING = 1; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ModuleOptions |
32
|
|
|
*/ |
33
|
|
|
protected $connectOption; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Manager constructor. |
37
|
|
|
* @param ModuleOptions $connectOption |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ModuleOptions $connectOption) |
40
|
|
|
{ |
41
|
|
|
$this->connectOption = $connectOption; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get \SolrClient with custom path option |
46
|
|
|
* |
47
|
|
|
* @param string $path |
48
|
|
|
* @return \SolrClient |
49
|
|
|
*/ |
50
|
|
|
public function getClient($path='/solr') |
51
|
|
|
{ |
52
|
|
|
$connectOption = $this->connectOption; |
53
|
|
|
$options = [ |
54
|
|
|
'secure' => $connectOption->isSecure(), |
55
|
|
|
'hostname' => $connectOption->getHostname(), |
56
|
|
|
'port' => $connectOption->getPort(), |
57
|
|
|
'path' => $path, |
58
|
|
|
'login' => $connectOption->getUsername(), |
59
|
|
|
'password' => $connectOption->getPassword(), |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
return new \SolrClient($options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add new document into Solr server |
67
|
|
|
* |
68
|
|
|
* @param \SolrInputDocument $document |
69
|
|
|
* @param string $path |
70
|
|
|
* @throws ServerException When failed adding document to server |
71
|
|
|
*/ |
72
|
|
|
public function addDocument(\SolrInputDocument $document,$path='/solr') |
73
|
|
|
{ |
74
|
|
|
$client = $this->getClient($path); |
75
|
|
|
try{ |
76
|
|
|
$client->addDocument($document); |
77
|
|
|
$client->commit(); |
78
|
|
|
$client->optimize(); |
79
|
|
|
}catch (\Exception $e){ |
80
|
|
|
throw new ServerException('Can not add document to server!',$e->getCode(),$e); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create new instance for Solr\Manager |
86
|
|
|
* @param ServiceLocatorInterface $sl |
87
|
|
|
* @return Manager |
88
|
|
|
*/ |
89
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
return new self( |
92
|
|
|
$sl->get('Solr/Options/Module') |
|
|
|
|
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |