-
Notifications
You must be signed in to change notification settings - Fork 15
/
SolrIndexerExample.php
113 lines (86 loc) · 2.9 KB
/
SolrIndexerExample.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
<?php
/**
*
* Fill in the URL to your Solr core and to your data catalog installation here
*
*/
$solr_core_url = 'https://www.example.com/solr/data_catalog';
$data_catalog_base_url = 'https://www.example.com';
$db_output_url = $data_catalog_base_url . '/api/Dataset/all.json?output_format=solr';
$solr_output_url = $solr_core_url . '/select/?q=*:*&wt=json';
$solr_submit_url = $solr_core_url . '/update/json?commit=true&overwrite=true';
$solr_remove_url = $solr_core_url . '/update/?commit=true';
if (! $fh = fopen($db_output_url, 'r')) {
exit("Could not open '{$db_output_url}'\n");
}
$db_json = stream_get_contents($fh);
$db_json_parsed = json_decode($db_json);
fclose($fh);
#
# Find items to remove
if (! $fh = fopen($solr_output_url, 'r')) {
exit("Could not open '{$solr_output_url}'\n");
}
$solr_json = stream_get_contents($fh);
fclose($fh);
foreach (json_decode($solr_json)->response->docs as $row) {
$found=0;
foreach($db_json_parsed as $db_row) {
if ($row->id == $db_row->id) {
$found=1;
break;
}
}
if ($found!=1) {
$to_solr = "{'delete': {'id': ".$row->id."}}";
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json' . "\r\n"
. 'Content-Length: ' . strlen($to_solr) . "\r\n",
'content' => $to_solr,
]
]);
file_get_contents($solr_remove_url, null, $context);
print "delete ".$row->id."\n";
}
}
#
# Find new/added items
foreach ($db_json_parsed as $row) {
if ($row->dataset_end_date && $row->dataset_start_date) {
$end_date = $row->dataset_end_date;
if ('Present' == $row->dataset_end_date) {
$end_date = (int) (date('Y') + 1);
}
if ($row->dataset_start_date == $end_date) {
$row->dataset_years = ["{$row->dataset_start_date}-01-01T00:00:00Z","{$end_date}-01-01T00:00:00Z"];
}
else {
$row->dataset_years = [];
for ($i = $row->dataset_start_date; $i <= $end_date; $i++) {
$row->dataset_years[] = "{$i}-01-01T00:00:00Z";
}
}
}
foreach ($row as $k => $v) {
if (! $v) {
$row->{$k} = '';
}
}
if ($row->date_added) {
$from_symfony = $row->date_added->date;
$row->date_added = trim(explode(' ', $from_symfony)[0]) . 'T00:00:00Z';
}
$to_solr = json_encode([$row]);
echo $to_solr;
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json' . "\r\n"
. 'Content-Length: ' . strlen($to_solr) . "\r\n",
'content' => $to_solr,
]
]);
file_get_contents($solr_submit_url, null, $context);
}