forked from simgroep/concurrent-spider-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexer.php
152 lines (124 loc) · 3.89 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Simgroep\ConcurrentSpiderBundle;
use PhpAmqpLib\Message\AMQPMessage;
use Solarium\Client;
use Solarium\QueryType\Update\Query\Query;
/**
* This class provides a gateway to the datastore for spidered webpages.
*/
class Indexer
{
/**
* @var \Solarium\Client
*/
private $client;
/**
* Holds the documents before submitting them to Solr
*
* @var array
*/
private $documents = [];
/**
* @var array
*/
private $mapping;
/**
* Constructor.
*
* @param \Solarium\Client $client
* @param array $mapping
*/
public function __construct(Client $client, array $mapping)
{
$this->client = $client;
$this->mapping = $mapping;
}
/**
* Indicates whether an URL already has been indexed or not.
*
* @param string $uri
* @param array $metadata
*
* @return boolean
*/
public function isUrlIndexed($uri, array $metadata = [])
{
$this->setCoreNameFromMetadata($metadata);
$query = $this->client->createSelect();
$query->setQuery(sprintf("id:%s", sha1($uri)));
$result = $this->client->select($query);
return ($result->getNumFound() > 0);
}
/**
* Make a document ready to be indexed.
*
* @param \PhpAmqpLib\Message\AMQPMessage $message
*/
public function prepareDocument(AMQPMessage $message)
{
$data = json_decode($message->body, true);
$this->setCoreNameFromMetadata($data['metadata']);
$updateQuery = $this->client->createUpdate();
$document = $updateQuery->createDocument();
foreach ($this->mapping as $field => $solrField) {
if ($field === 'groups') {
foreach ($solrField as $groupFieldName => $solrGroupFields) {
foreach ($solrGroupFields as $fieldName => $solrGroupFieldName) {
$composedFieldName = $groupFieldName . '.' . $fieldName;
$composedSolrFieldName = $groupFieldName . '.' . $solrGroupFieldName;
if (array_key_exists($composedFieldName, $data['document'])) {
$document->addField($composedSolrFieldName, $data['document'][$composedFieldName]);
}
}
}
continue;
}
if (array_key_exists($field, $data['document'])) {
$document->addField($solrField, $data['document'][$field]);
}
}
$this->documents[] = $document;
if (count($this->documents) >= 10) {
$this->addDocuments($updateQuery, $this->documents);
$this->documents = [];
}
}
/**
* Remove document from solr.
*
* @param \PhpAmqpLib\Message\AMQPMessage $message
* @param array $metadata
*/
public function deleteDocument(AMQPMessage $message)
{
$data = json_decode($message->body, true);
$this->setCoreNameFromMetadata($data['metadata']);
$updateQuery = $this->client->createUpdate();
$updateQuery->addDeleteById(sha1($data['url']));
$updateQuery->addCommit();
$this->client->update($updateQuery);
}
/**
* Set Core Name to write/read data
*
* @param array $metadata
*/
protected function setCoreNameFromMetadata(array $metadata)
{
if (array_key_exists('core', $metadata)) {
$this->client->getEndpoint()->setCore($metadata['core']);
}
}
/**
* Add multiple documents to the data store.
*
* @param \Solarium\QueryType\Update\Query\Query $updateQuery
* @param array $documents
*/
protected function addDocuments(Query $updateQuery, array $documents)
{
$updateQuery->addDocuments($documents);
$updateQuery->addCommit();
$this->client->update($updateQuery);
}
}