|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Solr\Bridge; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Solr\Exception\ServerException; |
|
13
|
|
|
use Solr\Options\Connection as ConnectionOption; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
|
|
16
|
|
|
class Manager |
|
17
|
|
|
{ |
|
18
|
|
|
const SOLR_DATE_FORMAT = 'Y-m-d\TH:i:s\Z'; |
|
19
|
|
|
const SORT_ASCENDING = 0; |
|
20
|
|
|
const SORT_DESCENDING = 1; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ConnectionOption |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $connectOption; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ConnectionOption $connectOption) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->connectOption = $connectOption; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getClient($path='/solr') |
|
33
|
|
|
{ |
|
34
|
|
|
$connectOption = $this->connectOption; |
|
35
|
|
|
$options = [ |
|
36
|
|
|
'secure' => $connectOption->isSecure(), |
|
37
|
|
|
'hostname' => $connectOption->getHostname(), |
|
38
|
|
|
'port' => $connectOption->getPort(), |
|
39
|
|
|
'path' => $path, |
|
40
|
|
|
'login' => $connectOption->getUsername(), |
|
41
|
|
|
'password' => $connectOption->getPassword(), |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
return new \SolrClient($options); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \SolrInputDocument $document |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addDocument(\SolrInputDocument $document,$path='/solr') |
|
52
|
|
|
{ |
|
53
|
|
|
$client = $this->getClient($path); |
|
54
|
|
|
try{ |
|
55
|
|
|
$client->addDocument($document); |
|
56
|
|
|
$client->commit(); |
|
57
|
|
|
$client->optimize(); |
|
58
|
|
|
}catch (\Exception $e){ |
|
59
|
|
|
throw new ServerException('Can not add document to server!',$e->getCode(),$e); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
static public function factory(ServiceLocatorInterface $sl) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return new self( |
|
66
|
|
|
$sl->get('Solr/Options/Connection') |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |