|
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.26 |
|
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 $options; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Manager constructor. |
|
37
|
|
|
* @param ModuleOptions $options |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(ModuleOptions $options) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->options = $options; |
|
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
|
|
|
$options = $this->options; |
|
53
|
|
|
$options = [ |
|
54
|
|
|
'secure' => $options->isSecure(), |
|
55
|
|
|
'hostname' => $options->getHostname(), |
|
56
|
|
|
'port' => $options->getPort(), |
|
57
|
|
|
'path' => $path, |
|
58
|
|
|
'login' => $options->getUsername(), |
|
59
|
|
|
'password' => $options->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
|
|
|
* @return ModuleOptions |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getOptions() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->options; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create new instance for Solr\Manager |
|
94
|
|
|
* @param ServiceLocatorInterface $sl |
|
95
|
|
|
* @return Manager |
|
96
|
|
|
*/ |
|
97
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
/* @var ModuleOptions $options */ |
|
100
|
|
|
$options = $sl->get('Solr/Options/Module'); |
|
101
|
|
|
return new self($options); |
|
102
|
|
|
} |
|
103
|
|
|
} |