This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find
executable file
·166 lines (152 loc) · 4.67 KB
/
find
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
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env php
<?php
/**
* Script for querying data from Bags indexed in ElasticSearch.
*
* Run 'php bagit_searcher.php --help' for usage.
*/
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$cmd = new Commando\Command();
$cmd->option('q')
->aka('query')
->describedAs('Query to perform. Do not mix with --id.');
$cmd->option('i')
->aka('id')
->describedAs('The ID of a Bag. The Elasticsearch document for the Bag will be retrieved and displayed. Do not mix with --query.')
->default('');
$cmd->option('a')
->aka('all')
->describedAs("List all Bags' IDs accompanied by their location.")
->boolean();
$cmd->option('e')
->aka('elasticsearch_url')
->describedAs('URL (including port number) of your Elasticsearch endpoint. Default is "http://localhost:9200".')
->default('http://localhost:9200');
$cmd->option('x')
->aka('elasticsearch_index')
->describedAs('Elasticsearch index. Default is "bags".')
->default('bags');
$climate = new League\CLImate\CLImate;
$hosts = array($cmd['e']);
$clientBuilder = ClientBuilder::create();
$clientBuilder->setHosts($hosts);
$client = $clientBuilder->build();
// If the request is for a all Bags, retrieve them here and go no further.
if ($cmd['all']) {
$params = [
'index' => 'bags',
'type' => 'bag',
'body' => [
'query' => [
'match_all' => new \stdClass(),
]
]
];
$results = $client->search($params);
foreach ($results['hits']['hits'] as $hit) {
$table_data[] = array('Bag ID' => $hit['_id'], 'Bag Location' => $hit['_source']['bag_location']);
}
$climate->table($table_data);
exit;
}
// If the request is for a specific Bag's document, retrieve it here and go no further.
if (strlen($cmd['id'])) {
$params = [
'index' => 'bags',
'type' => 'bag',
'id' => $cmd['id'],
];
try {
$response = $client->get($params);
print_r($response);
}
catch (Exception $e) {
$message = json_decode($e->getMessage());
if (!$message->found) {
print "Bag with ID " . $cmd['i'] . " not found.\n";
}
else {
print "Oops - unanticipated error: " . $e->getMessage() . "\n";
}
}
exit;
}
// Build the query.
list($field, $query) = explode(':', $cmd['q']);
switch ($field) {
case 'content':
$query_field = 'content';
break;
case 'description':
$query_field = 'bag-info.External-Description';
break;
case 'date':
$query_field = 'bag-info.Bagging-Date';
break;
case 'org':
$query_field = 'bag-info.Source-Organization';
break;
case 'file':
$query_field = 'data_files';
break;
case 'bag_location_exact':
$query_field = 'bag_location_exact';
break;
case 'bag_location':
$query_field = 'bag_location';
break;
case 'tombstone':
$query_field = 'tombstone';
break;
default:
print "Sorry, I don't recognize that field; you can use 'content', 'description', 'date', 'org', 'file', 'tombstone', 'bag_location_exact', or 'bag_location'." . PHP_EOL;
exit;
}
$params = [
'index' => 'bags',
'type' => 'bag',
'body' => [
'query' => [
'match' => array($query_field => $query),
]
]
];
// Get the results and show them to the user.
$results = $client->search($params);
if ($results['hits']['total'] > 0) {
print "Your query found " . $results['hits']['total'] . " hit(s): " . PHP_EOL;
$table_data = array();
foreach ($results['hits']['hits'] as $hit) {
switch ($field) {
case 'content':
$table_data[] = array('Bag ID' => $hit['_id'], 'Text content' => $hit['_source']['content']);
break;
case 'description':
$table_data[] = array('Bag ID' => $hit['_id'], 'External-Description' => $hit['_source']['bag-info']['External-Description']);
break;
case 'date':
$table_data[] = array('Bag ID' => $hit['_id'], 'Bagging-Date' => $hit['_source']['bag-info']['Bagging-Date']);
break;
case 'org':
$table_data[] = array('Bag ID' => $hit['_id'], 'Bagging-Date' => $hit['_source']['bag-info']['Source-Organization']);
break;
case 'file':
$table_data[] = array('Bag ID' => $hit['_id'], 'Data files' => implode(", ", $hit['_source']['data_files']));
break;
case 'bag_location_exact':
$table_data[] = array('Bag ID' => $hit['_id'], 'Source path' => $hit['_source']['bag_location_exact']);
break;
case 'bag_location':
$table_data[] = array('Bag ID' => $hit['_id'], 'Source path' => $hit['_source']['bag_location']);
break;
case 'tombstone':
$table_data[] = array('Bag ID' => $hit['_id'], 'Tombstone' => $hit['_source']['tombstone']);
break;
}
}
$climate->table($table_data);
}
else {
print "Your query found no hits. Sorry." . PHP_EOL;
}