author | ymh <ymh.work@gmail.com> |
Wed, 09 Nov 2016 15:05:41 +0100 | |
changeset 406 | cf0f23803a53 |
parent 375 | 145561ff51ff |
child 407 | 2dba812c7ef2 |
permissions | -rw-r--r-- |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
1 |
<?php |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
2 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
3 |
namespace CorpusParole\Console\Commands; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
4 |
|
325 | 5 |
|
6 |
||
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
7 |
use Illuminate\Console\Command; |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
8 |
use EasyRdf\Resource; |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
9 |
use EasyRdf\Literal; |
325 | 10 |
use EasyRdf\Graph; |
11 |
||
12 |
use Carbon\Carbon; |
|
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
13 |
|
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
14 |
use GuzzleHttp\Client; |
325 | 15 |
use GuzzleHttp\Exception\TransferException; |
16 |
use GuzzleHttp\Psr7; |
|
17 |
||
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
18 |
use CorpusParole\Libraries\Utils; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
19 |
use CorpusParole\Repositories\DocumentRepository; |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
20 |
use CorpusParole\Libraries\CocoonUtils; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
21 |
use CorpusParole\Models\GeonamesHierarchy; |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
22 |
use CorpusParole\Services\BnfResolverInterface; |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
23 |
use CorpusParole\Services\LexvoResolverInterface; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
24 |
use Es; |
325 | 25 |
use Log; |
26 |
use Cache; |
|
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
27 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
28 |
class IndexDocuments extends Command |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
29 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
30 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
31 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
32 |
* The name and signature of the console command. |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
33 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
34 |
* @var string |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
35 |
*/ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
36 |
protected $signature = 'corpus-parole:indexDocuments |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
37 |
{--limit=0 : index only the first n documents, 0 (default) means index everything } |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
38 |
{--no-bulk : index documents one by one instead of using ElasticSearch bulk indexing } |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
39 |
{--step-size=100 : number of documents to retrieve from repository at a time before indexing} |
323 | 40 |
{--reset-geo-cache : reset geo cache befr indexing}'; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
41 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
42 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
43 |
* The console command description. |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
44 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
45 |
* @var string |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
46 |
*/ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
47 |
protected $description = 'Index documents into ElasticSearch.'; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
48 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
49 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
50 |
* Create a new command instance. |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
51 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
52 |
* @return void |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
53 |
*/ |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
54 |
public function __construct( |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
55 |
DocumentRepository $documentRepository, |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
56 |
Client $httpClient, |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
57 |
BnfResolverInterface $bnfResolver, |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
58 |
LexvoResolverInterface $lexvoResolver) |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
59 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
60 |
$this->documentRepository = $documentRepository; |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
61 |
$this->bnfResolver = $bnfResolver; |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
62 |
$this->lexvoResolver = $lexvoResolver; |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
63 |
$this->httpClient = $httpClient; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
64 |
parent::__construct(); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
65 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
66 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
67 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
68 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
69 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
70 |
* Reset Elasticsearch index |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
71 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
72 |
* @return int (1 if sucess, 0 if error) |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
73 |
*/ |
323 | 74 |
private function resetIndex() |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
75 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
76 |
$indexParams = [ |
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
375
diff
changeset
|
77 |
'index' => config('elasticsearch.index') |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
78 |
]; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
79 |
if(Es::indices()->exists($indexParams)){ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
80 |
$response = Es::indices()->delete($indexParams); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
81 |
if($response['acknowledged']!=1){ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
82 |
return 0; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
83 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
84 |
} |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
85 |
// Note: removed the "'store' => True" parameters on fields and use _source on record instead |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
86 |
|
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
87 |
$indexParams['body'] = [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
88 |
'settings' => [ |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
89 |
'number_of_shards' => config('elasticsearch.shards'), |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
90 |
'number_of_replicas' => config('elasticsearch.replicas'), |
320 | 91 |
'index.mapping.ignore_malformed' => True, |
92 |
'index.requests.cache.enable' => True |
|
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
93 |
], |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
94 |
'mappings' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
95 |
'document' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
96 |
'properties' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
97 |
'title' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
98 |
'type' => 'string', |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
99 |
'fields' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
100 |
'raw' => [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
101 |
'type' => 'string', |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
102 |
'index' => 'not_analyzed' |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
103 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
104 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
105 |
], |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
106 |
'date' => [ 'type' => 'date', 'index' => 'not_analyzed'], |
369
796725d33b67
Add location filter to documents api end-point
ymh <ymh.work@gmail.com>
parents:
326
diff
changeset
|
107 |
'geonames_hierarchy' => [ 'type' => 'string', 'index' => 'not_analyzed'], |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
108 |
'location' => [ 'type' => 'geo_point'], |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
109 |
'creation_date' => ['type' => 'date', 'index' => 'not_analyzed'], |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
110 |
'language' => ['type' => 'string', 'index' => 'not_analyzed'], |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
111 |
'discourse_types' => ['type' => 'string', 'index' => 'not_analyzed'], |
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
112 |
'creation_years' => [ |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
113 |
'type' => 'nested', |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
114 |
'properties' => [ |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
115 |
'year' => [ 'type' => 'short', 'index' => 'not_analyzed'], |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
116 |
'weight' => [ 'type' => 'float', 'index' => 'not_analyzed'], |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
117 |
] |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
118 |
] , |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
119 |
'subject' => [ |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
120 |
'type' => 'nested', |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
121 |
'properties' => [ |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
122 |
'label' => [ 'type' => 'string', 'index' => 'not_analyzed'], |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
123 |
'code' => [ 'type' => 'string', 'index' => 'not_analyzed'], |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
124 |
'label_code' => [ 'type' => 'string', 'index' => 'not_analyzed'] |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
125 |
] |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
126 |
] |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
127 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
128 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
129 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
130 |
]; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
131 |
$response = Es::indices()->create($indexParams); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
132 |
if($response['acknowledged']!=1){ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
133 |
return 0; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
134 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
135 |
return 1; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
136 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
137 |
|
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
138 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
139 |
private function getGeonamesHierarchyArray($geonamesid) { |
325 | 140 |
|
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
141 |
$hcache = GeonamesHierarchy::where('geonamesid', $geonamesid)->first(); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
142 |
if(is_null($hcache)) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
143 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
144 |
// TODO: add delay to respect geonames 2k request/hour |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
145 |
// TODO: manage errors |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
146 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
147 |
$apiBody = $this->httpClient->get( |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
148 |
config('corpusparole.geonames_hierarchy_webservice_url'), |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
149 |
[ 'query' => |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
150 |
[ 'geonameId' => $geonamesid, |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
151 |
'username' => config('corpusparole.geonames_username') ], |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
152 |
'accept' => 'application/json' // TODO: check this |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
153 |
] |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
154 |
)->getBody(); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
155 |
$hjson = json_decode($apiBody); |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
156 |
$hcache = new GeonamesHierarchy(); |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
157 |
$hcache->geonamesid = $geonamesid; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
158 |
$hcache->hierarchy = $hjson; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
159 |
$hcache->save(); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
160 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
161 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
162 |
$res = []; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
163 |
foreach($hcache->hierarchy['geonames'] as $hierarchyElem) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
164 |
if(in_array($hierarchyElem['fcode'], ['CONT','PCLI', 'PCL','PCLD', 'PCLF', 'PCLH', 'PCLIX', 'PCLIS', 'ADM1'])) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
165 |
array_push($res, $hierarchyElem['geonameId']); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
166 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
167 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
168 |
return $res; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
169 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
170 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
171 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
172 |
/** |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
173 |
* get geonames hierarchy data. |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
174 |
* @return array list of geonames ids |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
175 |
*/ |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
176 |
private function getGeonamesHierarchy($doc) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
177 |
$geoRes = $doc->getGeoInfo(); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
178 |
if(is_null($geoRes)) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
179 |
return []; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
180 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
181 |
// aggregate hierachy list from geonames results |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
182 |
$res = []; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
183 |
foreach($geoRes->getGeonamesLocs() as $gurl) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
184 |
$geonamesId = CocoonUtils::getGeonamesidFromUrl($gurl); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
185 |
if(is_null($geonamesId)) { |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
186 |
continue; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
187 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
188 |
$hierarchyIds = $this->getGeonamesHierarchyArray($geonamesId); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
189 |
$res = array_unique(array_merge($res, $hierarchyIds)); |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
190 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
191 |
return $res; |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
192 |
|
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
193 |
} |
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
194 |
|
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
195 |
/** |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
196 |
* get subjects as { 'label': label, 'code': code } objects |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
197 |
* Takes only into account the bnf subjects |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
198 |
*/ |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
199 |
private function getSubjects($doc) { |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
200 |
|
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
201 |
$sres = array_reduce($doc->getSubjects(), function($res, $s) { |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
202 |
$mBnf = []; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
203 |
$mLexvo = []; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
204 |
|
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
205 |
if($s instanceof Resource && preg_match(config('corpusparole.bnf_ark_url_regexp'), $s->getUri(), $mBnf) === 1) { |
323 | 206 |
|
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
207 |
array_push($res, [ |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
208 |
'uri' => $mBnf[0], |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
209 |
'code' => $mBnf[1], |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
210 |
'type' => 'bnf' |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
211 |
]); |
323 | 212 |
} elseif($s instanceof Resource && preg_match(config('corpusparole.lexvo_url_regexp'), $s->getUri(), $mLexvo) === 1) { |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
213 |
array_push($res, [ |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
214 |
'uri' => $mLexvo[0], |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
215 |
'code' => $mLexvo[1], |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
216 |
'type' => 'lxv' |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
217 |
]); |
323 | 218 |
} elseif($s instanceof Literal && strpos($s->getDatatypeUri(), config('corpusparole.olac_base_url')) === 0 ) { |
219 |
array_push($res, [ |
|
220 |
'uri' => $s->getValue(), |
|
221 |
'code' => $s->getValue(), |
|
222 |
'type' => 'olac' |
|
223 |
]); |
|
224 |
} elseif($s instanceof Literal) { |
|
225 |
array_push($res, [ |
|
226 |
'uri' => $s->getValue(), |
|
227 |
'code' => $s->getValue(), |
|
228 |
'type' => 'txt' |
|
229 |
]); |
|
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
230 |
} |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
231 |
return $res; |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
232 |
}, []); |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
233 |
|
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
234 |
$labelsBnf = $this->bnfResolver->getLabels( |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
235 |
array_unique(array_reduce( |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
236 |
$sres, |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
237 |
function($r, $so) { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
238 |
if($so['type'] === 'bnf') { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
239 |
array_push($r, $so['uri']); |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
240 |
} |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
241 |
return $r; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
242 |
},[] |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
243 |
)) |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
244 |
); |
323 | 245 |
$labelsLexvo = $this->lexvoResolver->getNames( |
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
246 |
array_unique(array_reduce( |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
247 |
$sres, |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
248 |
function($r, $so) { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
249 |
if($so['type'] === 'lxv') { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
250 |
array_push($r, $so['uri']); |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
251 |
} |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
252 |
return $r; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
253 |
},[] |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
254 |
)) |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
255 |
); |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
256 |
|
322
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
257 |
return array_map(function($so) use ($labelsBnf, $labelsLexvo) { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
258 |
$label = $so['uri']; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
259 |
if($so['type'] === 'bnf') { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
260 |
$label = $labelsBnf[$label]; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
261 |
} elseif ($so['type'] === 'lxv') { |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
262 |
$label = $labelsLexvo[$label]; |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
263 |
} |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
264 |
return [ 'label' => $label, 'code' => $so['code'], 'label_code' => $label."|".$so['type']."|".$so['code'] ]; }, $sres |
084aae09edf4
correction on importRDF documents + evolution theme controller
ymh <ymh.work@gmail.com>
parents:
321
diff
changeset
|
265 |
); |
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
266 |
} |
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
267 |
|
325 | 268 |
private function graphResolvCoordinate($loc, $graph) { |
269 |
$latLit = $graph->getLiteral($loc, "<http://www.w3.org/2003/01/geo/wgs84_pos#lat>"); |
|
270 |
if(is_null($latLit) || empty($latLit->getValue())) { |
|
271 |
return null; |
|
272 |
} |
|
273 |
$lat = $latLit->getValue(); |
|
274 |
||
275 |
$longLit = $graph->getLiteral($loc, "<http://www.w3.org/2003/01/geo/wgs84_pos#long>"); |
|
276 |
if(is_null($longLit) || empty($longLit->getValue())) { |
|
277 |
return null; |
|
278 |
} |
|
279 |
$long = $longLit->getValue(); |
|
280 |
||
281 |
return [ $lat, $long ]; |
|
282 |
} |
|
283 |
||
284 |
private function loadGraph($url, $type) { |
|
285 |
try { |
|
286 |
$r = $this->httpClient->get($url); |
|
287 |
} catch (TransferException $e) { |
|
288 |
$this->error("loadGraph : Error Loading $url"); |
|
289 |
Log::error("loadGraph : Error Loading $url"); |
|
290 |
Log::error("loadGraph : Error request " . Psr7\str($e->getRequest())); |
|
291 |
if ($e->hasResponse()) { |
|
292 |
$this->error("loadGraph : Error response " . Psr7\str($e->getResponse())); |
|
293 |
Log::error("loadGraph : Error response " . Psr7\str($e->getResponse())); |
|
294 |
} |
|
295 |
return null; |
|
296 |
} |
|
297 |
try { |
|
298 |
$message = (string)$r->getBody(); |
|
299 |
$graph = new Graph($url, $message, $type); |
|
300 |
return $graph; |
|
301 |
} catch (EasyRdf\Exception $e) { |
|
302 |
$this->error("loadGraph : Error parsing $url"); |
|
303 |
Log::error("loadGraph : Error parsing $url"); |
|
304 |
if($e instanceof EasyRdf\Parser\Exception) { |
|
305 |
Log::error("loadGraph : Error exception line ".$e->getLine().", column: ".$e->getColumn()); |
|
306 |
} |
|
307 |
$this->error("loadGraph : Error exception message ".$e->getMessage()); |
|
308 |
Log::error("loadGraph : Error exception message ".$e->getMessage()); |
|
309 |
Log::error("loadGraph : Error content $message"); |
|
310 |
return null; |
|
311 |
} |
|
312 |
||
313 |
} |
|
314 |
||
315 |
private function geonamesResolveCoordinates($loc) { |
|
316 |
$coords = cache("corpus.geonames.coord.$loc"); |
|
317 |
if(is_null($coords)) { |
|
318 |
$graph = $this->loadGraph("{$loc}about.rdf", 'rdfxml'); |
|
319 |
$coords = is_null($graph)?null:$this->graphResolvCoordinate($loc, $graph); |
|
320 |
cache(["corpus.geonames.coord.$loc" => is_null($coords)?false:$coords], Carbon::now()->addMinutes(20)); |
|
321 |
} |
|
322 |
return ($coords===false)?null:$coords; |
|
323 |
} |
|
324 |
||
325 |
private function dbpediaResolveCoordinates($loc) { |
|
326 |
$coords = cache("corpus.dbpedia.coord.$loc"); |
|
327 |
if(is_null($coords)) { |
|
328 |
$graph = $this->loadGraph("$loc.rdf", 'rdfxml'); |
|
329 |
$coords = is_null($graph)?null:$this->graphResolvCoordinate($loc, $graph); |
|
330 |
cache(["corpus.dbpedia.coord.$loc"=> is_null($coords)?false:$coords], Carbon::now()->addMinutes(20)); |
|
331 |
} |
|
332 |
return ($coords===false)?null:$coords; |
|
333 |
} |
|
334 |
||
335 |
private function getLocation($doc) { |
|
336 |
||
337 |
$geoRes = $doc->getGeoInfo(); |
|
338 |
||
339 |
if(is_null($geoRes)) { |
|
340 |
return null; |
|
341 |
} |
|
342 |
||
343 |
$locUrls = []; |
|
344 |
foreach($geoRes->getRefLocs() as $loc) { |
|
345 |
if(preg_match(config('corpusparole.geonames_url_regexp'), $loc, $m) === 1) { |
|
346 |
||
347 |
if(!array_key_exists('geonames', $locUrls)) { |
|
348 |
$locUrls['geonames'] = []; |
|
349 |
} |
|
350 |
array_push($locUrls['geonames'], "http://sws.geonames.org/$m[1]/"); |
|
351 |
||
352 |
} elseif(preg_match(config('corpusparole.dbpedia_url_regexp'), $loc, $md) === 1) { |
|
353 |
if(!array_key_exists('dbpedia', $locUrls)) { |
|
354 |
$locUrls['dbpedia'] = []; |
|
355 |
} |
|
356 |
//$this->line("DBPEDIA MATCH $loc ".print_r($md,true)); |
|
357 |
array_push($locUrls['dbpedia'], "http://$md[1]/data/$md[4]"); |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
$coordinates = null; |
|
362 |
foreach($locUrls as $locType => $locList) { |
|
363 |
foreach($locList as $locationUrl) { |
|
364 |
$coordinates = call_user_func([$this, "${locType}ResolveCoordinates"], $locationUrl); |
|
365 |
if(!is_null($coordinates)) { |
|
366 |
break; |
|
367 |
} |
|
368 |
} |
|
369 |
} |
|
370 |
||
371 |
if(is_null($coordinates)) { |
|
372 |
$coordinates = [$geoRes->getLatitudeValue(), $geoRes->getLongitudeValue()]; |
|
373 |
} |
|
374 |
||
375 |
if(empty($coordinates[0]) || empty($coordinates[1])) { |
|
376 |
return null; |
|
377 |
} else { |
|
378 |
return [floatval($coordinates[0]), floatval($coordinates[1])]; |
|
379 |
} |
|
380 |
||
381 |
} |
|
382 |
||
383 |
private function getCreationDate($doc) { |
|
384 |
||
385 |
$created = $doc->getCreated(); |
|
386 |
if(is_null($created)) { |
|
387 |
return null; |
|
388 |
} |
|
389 |
$dateType = $created->getDatatypeUri(); |
|
390 |
$res = null; |
|
391 |
||
392 |
if($dateType === "http://purl.org/dc/terms/Period") { |
|
393 |
$res = $this->processPeriod($created->getValue()); |
|
394 |
} |
|
395 |
elseif($dateType === "http://purl.org/dc/terms/W3CDTF") { |
|
396 |
$res = $this->processDate($created->getValue()); |
|
397 |
} |
|
398 |
||
399 |
return $res; |
|
400 |
||
401 |
} |
|
402 |
||
403 |
private function extractDate($dateStr) { |
|
404 |
if(preg_match("/^\\d{4}$/", $dateStr) === 1) { |
|
405 |
$dateStr = "$dateStr-1-1"; |
|
406 |
} |
|
407 |
$date = date_create($dateStr); |
|
408 |
if($date === false ) { |
|
409 |
Log::warning("DateStatsController:extractYear bad format for date $dateStr"); |
|
410 |
return null; |
|
411 |
} |
|
412 |
return $date; |
|
413 |
} |
|
414 |
||
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
415 |
private function processPeriod($periodStr, $asDate=false) { |
325 | 416 |
$start = null; |
417 |
$end = null; |
|
418 |
foreach(explode(";", $periodStr) as $elem) { |
|
419 |
$elem = trim($elem); |
|
420 |
if(strpos($elem, 'start=') === 0) { |
|
421 |
$startDate = $this->extractDate(trim(substr($elem, 6))); |
|
422 |
if(is_null($startDate)) { |
|
423 |
return null; |
|
424 |
} |
|
425 |
$start = intval($startDate->format("Y")); |
|
426 |
if($start === false) { |
|
427 |
return null; |
|
428 |
} |
|
429 |
} elseif(strpos($elem, 'end=') === 0) { |
|
430 |
$endDate = $this->extractDate(trim(substr($elem, 4))); |
|
431 |
if(is_null($endDate)) { |
|
432 |
return null; |
|
433 |
} |
|
434 |
$end = intval($endDate->format("Y")); |
|
435 |
if($end === false) { |
|
436 |
return null; |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
if(is_null($start) || is_null($end) || $start>$end ) { |
|
442 |
Log::warning("Bad format for $periodStr"); |
|
443 |
return null; |
|
444 |
} |
|
445 |
||
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
446 |
return array_map(function($y) use ($asDate){ |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
447 |
$date = \DateTime::createFromFormat("Y", "$y"); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
448 |
if($asDate) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
449 |
return $date; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
450 |
} else { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
451 |
return $date->format(\DateTime::W3C); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
452 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
453 |
|
325 | 454 |
}, range($start, $end)); |
455 |
} |
|
456 |
||
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
457 |
private function processDate($dateStr, $asDate=false) { |
325 | 458 |
$date = $this->extractDate($dateStr); |
459 |
if(is_null($date)) { |
|
460 |
return null; |
|
461 |
} else { |
|
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
462 |
if($asDate) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
463 |
return $date; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
464 |
} else { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
465 |
return $date->format(\DateTime::W3C); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
466 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
467 |
|
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
468 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
469 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
470 |
|
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
471 |
private function getCreationYears($doc) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
472 |
$created = $doc->getCreated(); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
473 |
if(is_null($created)) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
474 |
return []; |
325 | 475 |
} |
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
476 |
$dateType = $created->getDatatypeUri(); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
477 |
$dates = null; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
478 |
|
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
479 |
if($dateType === "http://purl.org/dc/terms/Period") { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
480 |
$dates = $this->processPeriod($created->getValue(), true); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
481 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
482 |
elseif($dateType === "http://purl.org/dc/terms/W3CDTF") { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
483 |
$dates = $this->processDate($created->getValue(), true); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
484 |
if(!is_null($dates)) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
485 |
$dates = [ $dates, ]; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
486 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
487 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
488 |
if(is_null($dates)) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
489 |
return []; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
490 |
} |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
491 |
$count = count($dates); |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
492 |
return array_map(function($d) use ($count) { |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
493 |
return [ |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
494 |
'year' => intval($d->format("Y")), |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
495 |
'weight' => 1/$count |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
496 |
]; |
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
497 |
|
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
498 |
}, $dates); |
325 | 499 |
} |
500 |
||
501 |
private function getDiscourseTypes($doc) { |
|
502 |
return array_reduce($doc->getDiscourseTypes(), function($res, $d) { |
|
503 |
$val = null; |
|
504 |
if($d instanceof Resource) { |
|
505 |
$val = $d->getUri(); |
|
506 |
} elseif($d instanceof Literal) { |
|
507 |
$datatype = $d->getDatatypeURI(); |
|
508 |
$val = (!empty($datatype)?"$datatype#":"").$d->getValue(); |
|
509 |
} |
|
510 |
if(!empty($val)) { |
|
511 |
array_push($res,$val); |
|
512 |
} |
|
513 |
return $res; |
|
514 |
}, []); |
|
515 |
} |
|
516 |
||
517 |
private function getDocBody($doc) { |
|
518 |
return [ |
|
519 |
'title' => (string)$doc->getTitle(), |
|
520 |
'date' => (string)$doc->getModified(), |
|
521 |
'location' => $this->getLocation($doc), |
|
522 |
'creation_date' => $this->getCreationDate($doc), |
|
375
145561ff51ff
change datestats to use elasticsearch
ymh <ymh.work@gmail.com>
parents:
369
diff
changeset
|
523 |
'creation_years' => $this->getCreationYears($doc), |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
524 |
'language' => $doc->getLanguagesValue(), |
325 | 525 |
'discourse_types' => $this->getDiscourseTypes($doc), |
526 |
'geonames_hierarchy' => $this->getGeonamesHierarchy($doc), |
|
527 |
'subject' => $this->getSubjects($doc), |
|
528 |
]; |
|
529 |
} |
|
530 |
||
321
aefaad270b9b
reimplement ThemeController using ES requests to be able to sort by label
ymh <ymh.work@gmail.com>
parents:
320
diff
changeset
|
531 |
/** |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
532 |
* Index one document into Elasticsearch |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
533 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
534 |
* @return int (1 if sucess, 0 if error) |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
535 |
*/ |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
536 |
private function indexOne($docId, $docBody) |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
537 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
538 |
$query_data = [ |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
539 |
'index' => config('elasticsearch.index'), |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
540 |
'type' => 'document', |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
541 |
'id' => $docId, |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
542 |
'body' => $docBody |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
543 |
]; |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
544 |
Es::index($query_data); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
545 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
546 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
547 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
548 |
* Index multiple document into Elasticsearch |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
549 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
550 |
* @return int (1 if sucess, 0 if error) |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
551 |
*/ |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
552 |
private function indexBulk($docBodies) |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
553 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
554 |
$query_data = ['body' => []]; |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
555 |
foreach($docBodies as $docId => $docBody){ |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
556 |
$query_data['body'][] = [ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
557 |
'index' => [ |
308
e032d686d88e
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
25
diff
changeset
|
558 |
'_index' => config('elasticsearch.index'), |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
559 |
'_type' => 'document', |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
560 |
'_id' => $docId |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
561 |
] |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
562 |
]; |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
563 |
$query_data['body'][] = $docBody; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
564 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
565 |
Es::bulk($query_data); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
566 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
567 |
/** |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
568 |
* Execute the console command. |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
569 |
* |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
570 |
* @return mixed |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
571 |
*/ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
572 |
public function handle() |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
573 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
574 |
$this->info('Options:'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
575 |
$noBulk = $this->option('no-bulk'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
576 |
if ($noBulk) |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
577 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
578 |
$this->comment(' - Indexing without bulk insert'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
579 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
580 |
else |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
581 |
{ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
582 |
$this->comment(' - Indexing using bulk insert'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
583 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
584 |
$limit = $this->option('limit'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
585 |
if ($limit>0) { |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
586 |
$this->comment(' - Indexing only the first '.$limit.' documents'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
587 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
588 |
$stepSize = $this->option('step-size'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
589 |
$this->comment(' - Indexing with step size of '.$stepSize); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
590 |
|
323 | 591 |
if($this->option('reset-geo-cache', false)) { |
592 |
// delete all rows in GeonamesHierarchy |
|
593 |
GeonamesHierarchy::getQuery()->delete(); |
|
594 |
$this->comment('Geonames cache reset!'); |
|
595 |
} |
|
596 |
||
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
597 |
$this->info('Resetting index...'); |
323 | 598 |
$success = $this->resetIndex(); |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
599 |
if($success==1){ |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
600 |
$this->comment('Index reset!'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
601 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
602 |
else{ |
406
cf0f23803a53
upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents:
375
diff
changeset
|
603 |
$this->error('Error resetting index ' . config('elasticsearch.index')); |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
604 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
605 |
|
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
606 |
$this->info('Indexing documents...'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
607 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
608 |
$limit = (int)$limit; |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
609 |
$total = $this->documentRepository->getCount(); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
610 |
|
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
611 |
if($limit>0) { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
612 |
$total = min($limit, $total); |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
613 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
614 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
615 |
$progressBar = $this->output->createProgressBar($total); |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
616 |
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
617 |
|
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
618 |
$page = 0; |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
619 |
$lastPage = PHP_INT_MAX; |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
620 |
$docIds = []; |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
621 |
|
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
622 |
while($page++<$lastPage) { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
623 |
$docsPaginator = $this->documentRepository->paginate(null, $stepSize, config('corpusparole.pagination_page_param'), $page, "_graph"); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
624 |
$lastPage = $docsPaginator->lastPage(); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
625 |
$docsBodies = []; |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
626 |
foreach($docsPaginator as $docResult) { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
627 |
$docId = (string)$docResult->getId(); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
628 |
$progressBar->setMessage($docId); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
629 |
$progressBar->advance(); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
630 |
$doc = $this->documentRepository->get($docId); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
631 |
$docBody = $this->getDocBody($doc); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
632 |
if($noBulk) { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
633 |
$this->indexOne($docId, $docBody); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
634 |
} else { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
635 |
$docsBodies[$docId] = $docBody; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
636 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
637 |
$docIds[] = $docId; |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
638 |
} |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
639 |
if(!$noBulk) { |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
640 |
$this->indexBulk($docsBodies); |
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
641 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
642 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
643 |
$progressBar->finish(); |
326
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
644 |
$this->info("\nIndexing completed for " . count(array_unique($docIds))." documents (of ".count($docIds).")."); |
226d5b17a119
- First implementation of filter for languages.
ymh <ymh.work@gmail.com>
parents:
325
diff
changeset
|
645 |
|
24
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
646 |
} |
de47e8f66e8b
Added command "corpus-parole:indexDocuments" to index documents into ElasticSearch
durandn
parents:
diff
changeset
|
647 |
} |