-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleElasticsearchSubjectIndexer.php
More file actions
96 lines (85 loc) · 2.74 KB
/
ExampleElasticsearchSubjectIndexer.php
File metadata and controls
96 lines (85 loc) · 2.74 KB
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
<?php
namespace SIR;
class ExampleElasticsearchSubjectIndexer {
public function __construct($fullPageName) {
$this->page = new Page($fullPageName)
}
public function indexPage() {
$docId = $this->getDocumentExistsId();
if($docId === false) {
$this->addDocument();
} else {
$this->updateDocument($docId);
}
}
private function addDocument() {
$ch = new CurlTransfer(SUBJECTS_INDEX, "subject", NULL, "POST");
$data = $this->getPageDataJSON();
$ch->setPostFields($data);
try {
$response = json_decode($ch->exec(), true);
error_log($this->page->fullPageName." added."."\n");
} catch(Exception $e) {
error_log($e->getMessage());
}
}
private function updateDocument($docId) {
$data = '
{
"doc": '.$this->getPageDataJSON().'
}';
try {
$ch = new CurlTransfer(SUBJECTS_INDEX, "subject", $docId."/_update", "POST");
$ch->setPostFields($data);
$response = json_decode($ch->exec(), true);
} catch(Exception $e) {
error_log($e->getMessage());
}
}
private function getPageDataJSON() {
$pageDataArray = array(
'subjectName' => $this->page->fullPageName,
'subjectTitle' => Page::getSemanticMWPropertyInstanceValues($this->page->fullPageName, 'HasTypeAndTitle'),
'subjectTitleNA' => Page::getSemanticMWPropertyInstanceValues($this->page->fullPageName, 'HasTypeAndTitle'),
'subjectType' => Page::getSemanticMWPropertyInstanceValues($this->page->fullPageName, 'HasType'),
'subjectLink' => $this->page->getPageURL(),
'subjectProperties' => $this->getPageProperties()
);
return json_encode($pageDataArray);
}
private function getPageProperties() {
$apiCall = array(
'action' => 'browsebysubject',
'subject' => $this->page->fullPageName
);
$resultData = \API::apiCall($apiCall);
$propertiesArray = $resultData['query']['data'];
$propertiesArray2 = array();
foreach($propertiesArray as $propertyData) {
if(is_array($propertyData)) {
$propertyName = $propertyData['property'];
$propertyValuesArray = $propertyData['dataitem'];
if(substr($propertyName, 0, 1) == "_") {
} else {
$property = new Property2($propertyName);
$property->registerPropertyValues($propertyValuesArray);
$propertiesArray2[] = array(
'subjectPropertyName' => $property->propertyName,
'subjectPropertyValues' => $property->propertyValues
);
}
}
}
return $propertiesArray2;
}
private function getDocumentExistsId() {
$pageName = str_replace(" ", "%20", $this->page->fullPageName);
$ch = new CurlTransfer(SUBJECTS_INDEX, "subject", "_search?q=subjectName:%22'.$pageName.'%22", "GET");
$response = json_decode($ch->exec(), true);
if($response['hits']['total'] == 1) {
return $response['hits']['hits'][0]['_id'];
} else {
return false;
}
}
}