-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElasticsearch.html
686 lines (592 loc) · 21.7 KB
/
Elasticsearch.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<html>
<head>
<meta charset="utf-8">
<title>Enipedia Search</title>
<style type="text/css">
#map-canvas { height: 400px; width:800px; }
* {
font-family: Tahoma, Geneva, sans-serif;
font-size:14px;
}
table.resultTable
{
border-width: 0 0 1px 1px;
border-spacing: 0;
border-collapse: collapse;
border-style: solid;
}
.resultTable td, .resultTable th
{
margin: 0;
padding: 4px;
border-width: 1px 1px 0 0;
border-style: solid;
}
</style>
<script src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=places"></script>
<script type="text/javascript">
/**
TODO
* would be great to be able to query multiple indexes simultaneous and return the best results from each
* need to have different ways to format the results from different data sources
* Load libraries locally on server
* standardize various data sources to the extend possible
* trim/collapse long results in tables - i.e. don't need to show the whole text of the wikipedia article unless the user clicks on it
* Create permalink so people can reproduce view and settings
* Any way to get search results indexed by search engines? This could be interesting for helping to link the data and attract attention
**/
var map;
var markersArray = [];
var infowindow = new google.maps.InfoWindow();
//http://nack.co/get-url-parameters-using-jquery/
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results) {
return results[1];
} else {
return null;
}
}
function expandText(textID) {
document.getElementById("shortText_" + textID).style.display = 'none';
document.getElementById("fullText_" + textID).style.display = 'block';
}
function collapseText(textID) {
document.getElementById("shortText_" + textID).style.display = 'block';
document.getElementById("fullText_" + textID).style.display = 'none';
}
//initialize the map
function initialize() {
var query = $.urlParam('q');
var mapOptions = {
center: new google.maps.LatLng(20,0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
if (query){ //only perform initial search if a query string has been found
// convert plus to space (charachter encoding w/ urlencode function of wiki)
query = query.replace(/\+/g, ' ');
// remove extra spaces
query = query.replace(/ +(?= )/g,'');
$('#searchbox').val(decodeURIComponent(query));
search();
}
}
google.maps.event.addDomListener(window, 'load', initialize);
// Something to put here?
// Run function once document is ready
//$( document ).ready(function() {
//})
function clearMarkers() {
for (var i = 0; i < markersArray.length; i++ ) {
markersArray[i].setMap(null);
}
markersArray = [];
}
//Thank you internet http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-an-url
function ValidUrl(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\'\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
if(!pattern.test(str)) {
return false;
} else {
return true;
}
}
function carmaPrinter(columnNames, columnValues, score, index, objType, objID){
var content = "<table class='resultTable'><tr>";
content += "<th>Score</th>";
content += "<th>Source</th>";
var idLoc=0;
for (i=0; i < columnNames.length; i++) {
if (columnNames[i] == "plant_id"){
idLoc = i;
}
content += "<th>";
content += columnNames[i];
content += "</th>";
}
content += "</tr><tr>";
content += "<td valign='top'>" + score + "</td>";
content += "<td valign='top'>" + index + "</td>";
for (i=0; i < columnValues.length; i++) {
if (i == idLoc){
content += "<td valign='top'>";
content += "<a href=\"http://carma.org/plant/detail/" + columnValues[i] + "\">" + columnValues[i] + "</a>";
content += "</td>";
} else {
content += "<td valign='top'>";
content += formatTableData(columnValues[i]);
content += "</td>";
}
}
content += "</tr></table><p>";
return content;
}
function geoPrinter(columnNames, columnValues, score, index, objType, objID){
var content = "<table class='resultTable'><tr>";
content += "<th>Score</th>";
content += "<th>Source</th>";
content += "<th>Name</th>";
content += "<th>ID</th>";
var idLoc=0;
var nameLoc=0;
for (i=0; i < columnNames.length; i++) {
if (columnNames[i] == "Description_ID"){
idLoc = i;
} else if (columnNames[i] == "Name"){
nameLoc = i;
} else {
content += "<th>";
content += columnNames[i];
content += "</th>";
}
}
content += "</tr><tr>";
content += "<td valign='top'>" + score + "</td>";
content += "<td valign='top'>" + index + "</td>";
content += "<td valign='top'>";
content += columnValues[nameLoc];
content += "</td>";
content += "<td valign='top'>";
content += "<a href=\"http://globalenergyobservatory.org/geoid/" + columnValues[idLoc] + "\">" + columnValues[idLoc] + "</a>";
content += "</td>";
for (i=0; i < columnValues.length; i++) {
if (i != idLoc && i != nameLoc) {
content += "<td valign='top'>";
content += formatTableData(columnValues[i]);
content += "</td>";
}
}
content += "</tr></table><p>";
return content;
}
function openStreetMapPrinter(columnNames, columnValues, score, index, objType, objID){
var content = "<table class='resultTable'><tr>";
content += "<th>Score</th>";
content += "<th>Source</th>";
content += "<th>ID</th>";
for (i=0; i < columnNames.length; i++) {
content += "<th>";
content += columnNames[i];
content += "</th>";
}
content += "</tr><tr>";
content += "<td valign='top'>" + score + "</td>";
content += "<td valign='top'>OpenStreetMap</td>";
content += "<td valign='top'><a href=\"http://www.openstreetmap.org/browse/" + objType + "/" + objID + "\">" + objType + "/" + objID + "</a></td>";
for (i=0; i < columnValues.length; i++) {
content += "<td valign='top'>";
content += formatTableData(columnValues[i]);
content += "</td>";
}
content += "</tr></table><p>";
return content;
}
function industryAboutEntryPrinter(columnNames, columnValues, score, index, objType, objID){
// put url link into name
var content = "<table class='resultTable'><tr>";
content += "<th>Score</th>";
content += "<th>Source</th>";
var nameLoc;
var urlLoc;
for (i=0; i < columnNames.length; i++) {
if (columnNames[i] == "name"){
nameLoc = i;
}
if (columnNames[i] == "url"){
urlLoc = i;
} else { //only create table header element if this is not the url
content += "<th>";
content += columnNames[i];
content += "</th>";
}
}
content += "</tr><tr>";
content += "<td valign='top'>" + score + "</td>";
content += "<td valign='top'>IndustryAbout</td>";
for (i=0; i < columnValues.length; i++) {
if (i == nameLoc){
content += "<td valign='top'>";
content += "<a href=\"" + columnValues[urlLoc] + "\">" + columnValues[i] + "</a>";
content += "</td>";
} else if (i != urlLoc){
content += "<td valign='top'>";
content += formatTableData(columnValues[i]);
content += "</td>";
}
}
content += "</tr></table><p>";
return content;
}
function defaultEntryPrinter(columnNames, columnValues, score, index, objType, objID){
var content = "<table class='resultTable'><tr>";
content += "<th>Score</th>";
content += "<th>Source</th>";
content += "<th>ID</th>";
for (i=0; i < columnNames.length; i++) {
content += "<th>";
content += columnNames[i];
content += "</th>";
}
content += "</tr><tr>";
content += "<td valign='top'>" + score + "</td>";
content += "<td valign='top'>" + index + "</td>";
content += "<td valign='top'>" + objID + "</td>";
for (i=0; i < columnValues.length; i++) {
/**
check if the text is too long (i.e. raw wikipedia text)
and if we should truncate the text with an option to expand it.
**/
if (columnValues[i].length > 300){
content += "<td valign='top'>";
// make a unique id so we know what we're clicking on and collapsing/expanding
textID = index + objID;
textID = textID.replace(/\W/g, '');
// want div of short text and full text
// ===== Short Text =====
content += "<div id='shortText_" + textID + "'>";
content += formatTableData(columnValues[i].substring(0, 299));
content += "<div id='expandText_" + textID + "' onClick=expandText('" + textID + "')>"
content += "<font color=blue>...(expand text)</font></div>";
content += "</div>";
// ===== Long Text =====
content += "<div id='fullText_" + textID + "' style='display:none'>";
content += formatTableData(columnValues[i]);
content += "<div id='collapseText_" + textID + "' onClick=collapseText('" + textID + "')>"
content += "<font color=blue>(collapse text)</font></div>";
content += "</div>";
content += "</td>";
} else {
content += "<td valign='top'>";
content += formatTableData(columnValues[i]);
content += "</td>";
}
}
content += "</tr></table><p>";
return content;
}
//Basically see if there is a URL and create a link to it
function formatTableData(text){
var content = "";
if (ValidUrl(text)){
content += "<a href=\"" + text + "\">" + text + "</a>";
} else {
content += text;
}
return content;
}
function search(){
$('#search_status').empty();
$('#search_status').append("Searching... <img src=loading.gif>");
//remove previous markers
clearMarkers();
var query = $('#searchbox').val();
var numResults = $('#numSearchResults').val();
var indicesToCheck = ["wikipedia", "carmav2", "carmav3", "osm", "geo", "eprtr", "euets", "lcpd", "geo"];
var indicesToSearch = [];
for (index in indicesToCheck){
if ($('#'+indicesToCheck[index]).prop('checked')){
indicesToSearch.push(indicesToCheck[index]);
}
}
var queryURL = "http://enipedia.tudelft.nl/search/" + indicesToSearch.join(",") + "/_search"
if (typeof map.getBounds() === 'undefined') {
var lat1 = 90;
var lon1 = 180;
var lat2 = -90;
var lon2 = -180;
} else {
var lat1 = map.getBounds().getNorthEast().lat();
var lon1 = map.getBounds().getNorthEast().lng();
var lat2 = map.getBounds().getSouthWest().lat();
var lon2 = map.getBounds().getSouthWest().lng();
}
//search doesn't work if +/- 180 is included
if (lon2 == -180){
lon2 = -179.999;
}
if (lon1 == 180){
lon1 = 179.999;
}
var topLeft = lat1+","+lon2;
var bottomRight = lat2+","+lon1;
var queryData = {};
queryData.from = 0;
queryData.size = numResults;
if (query != ""){
// let there be fuzzyness
if ($('#fuzzyLikeThis').prop('checked')){
queryData.query = {
fuzzy_like_this: {
like_text: query
}
};
} else { // just do normal search, it's quicker
queryData.query = {
"common" : {
"_all" : { "query":query,
"cutoff_frequency":0.001
}
}
};
}
} else { //match everything if query is blank
// set checkbox to true if it isn't already, otherwise the whole world will be shown
$('#searchInMapView').prop('checked', true);
queryData.query = {
"filtered": {
"query": {
"match_all": {
}
}
}
};
}
if ($('#searchInMapView').prop('checked')){
queryData.filter = {
"geo_bounding_box": {
"location": {
"top_left": topLeft,
"bottom_right": bottomRight
}
}};
}
// See what I've created
//alert(JSON.stringify(queryData));
$.ajax({
type: "POST",
url: queryURL,
data: JSON.stringify(queryData),
//jsonp doesn't work
dataType: "json",
crossDomain: true,
success: function( data ) {
var totalHits = data.hits.total;
var hitsReturned = numResults;
// check if the amount of hits returned is less than the number of results requested
if (hitsReturned > totalHits){
hitsReturned = totalHits
}
$('#search_status').empty();
$('#search_status').append(hitsReturned + " of " + totalHits + " hits returned in " + parseFloat(data.took)/1000 + " seconds");
var bounds = new google.maps.LatLngBounds();
var hits = data.hits.hits;
// clear the previous table
$('#table_goes_here').empty();
// get the min and max score so that the icons can be sized
// based on a standard range from min to max
var minScore = 9999;
var maxScore = -9999;
for (resultNum=0; resultNum < hits.length; resultNum++) {
var score = hits[resultNum]._score;
if (score > maxScore) {
maxScore = score;
}
if (score < minScore) {
minScore = score;
}
}
for (resultNum=0; resultNum < hits.length; resultNum++) {
var index = hits[resultNum]._index;
var objID = hits[resultNum]._id;
var objType = hits[resultNum]._type;
//console.debug ("%o", hits[resultNum]);
var columnNames = $.map(hits[resultNum]._source, function(value, key) {
return key;
});
var columnValues = $.map(hits[resultNum]._source, function(value, key) {
return value;
});
var score = hits[resultNum]._score;
var content = "";
var lat = 0;
var lon = 0;
if (hits[resultNum]._source.latitude){
lat = hits[resultNum]._source.latitude;
}
if (hits[resultNum]._source.lat){
lat = hits[resultNum]._source.lat;
}
// occurs in GEO
if (hits[resultNum]._source.Saved_Latitude_Start){
lat = hits[resultNum]._source.Saved_Latitude_Start;
}
if (hits[resultNum]._source.longitude){
lon = hits[resultNum]._source.longitude;
}
if (hits[resultNum]._source.lon){
lon = hits[resultNum]._source.lon;
}
if (hits[resultNum]._source.long){
lon = hits[resultNum]._source.long;
}
// occurs in GEO
if (hits[resultNum]._source.Saved_Longitude_Start){
lon = hits[resultNum]._source.Saved_Longitude_Start;
}
if(lat !=0.0 && lon != 0.0){
var myLatLng = new google.maps.LatLng(lat,lon);
if (minScore != maxScore){
scoreScalingFactor = 0.5 + ((score-minScore)/(maxScore - minScore));
} else {
scoreScalingFactor = 1
}
var markerImage = new google.maps.MarkerImage(
"http://maps.google.com/mapfiles/marker.png",
null, /* size is determined at runtime */
null, /* origin is 0,0 */
null, /* anchor is bottom center of the scaled image */
new google.maps.Size(20*scoreScalingFactor, 34*scoreScalingFactor)
);
var markerOptions = {position: myLatLng, map:map, title:"hello", icon: markerImage};
var marker = new google.maps.Marker(markerOptions);
marker.data = hits[resultNum]._source;
marker.setMap(map);
google.maps.event.addListener(marker, 'click', function() { open_marker_info(this)} );
markersArray.push(marker);
bounds.extend(myLatLng);
}
//TODO this needs to be customized based on the index that is being worked with
// could specify which columns need to be formatted differently - i.e. what's a link.
// probably want to put the link on the name if it exists, maybe reorder fields based
// on popularity/usefulness
switch(index)
{
case "carmav2":
content = carmaPrinter(columnNames, columnValues, score, index, objType, objID);
break;
case "carmav3":
content = carmaPrinter(columnNames, columnValues, score, index, objType, objID);
break;
case "osm":
content = openStreetMapPrinter(columnNames, columnValues, score, index, objType, objID);
break;
case "geo":
content = geoPrinter(columnNames, columnValues, score, index, objType, objID);
break;
case "industryabout":
content = industryAboutEntryPrinter(columnNames, columnValues, score, index, objType, objID);
break;
default:
content = defaultEntryPrinter(columnNames, columnValues, score, index, objType, objID);
}
$('#table_goes_here').append(content);
}
if (hits.length > 0){ //don't update map if we didn't find anything
map.fitBounds(bounds);
}
// set minimum zoom level - usually needed for if only a single point is returned
// http://stackoverflow.com/questions/2437683/google-maps-api-v3-can-i-setzoom-after-fitbounds
var listener = google.maps.event.addListener(map, "idle", function() {
if (map.getZoom() > 15) map.setZoom(15);
google.maps.event.removeListener(listener);
});
}
});
}
function open_marker_info(marker){
var content = "";
//var content = "<div style='font-size: 9pt; font-family: Arial'><span style='font-size: 12pt'>" + marker.data.name.value + "</span><ul>";
if(marker.data["name"]){
content = "<div style='font-size: 9pt; font-family: Arial'><span style='font-size: 12pt'>" + marker.data["name"] + "</span><ul>";
} else {
content = "<div style='font-size: 9pt; font-family: Arial'><ul>";
}
for (var p in marker.data) {
//if (p != "wiki" && p != "name" && p != "point" && marker.data[p].value != null && !(p == "ownerenc" && marker.data.owner))
content += "<li>" + p.replace("_"," ") + ": " + marker.data[p] + "</li>";
}
//marker.data.wiki.value = marker.data.wiki.value.replace("'", "%27");
//content += "</ul><a href='http://enipedia.tudelft.nl/wiki/" + marker.data.wiki.value + "' target='EnipView'>view</a> | ";
//content += "<a href='http://enipedia.tudelft.nl/enipedia/index.php?title=" + marker.data.wiki.value + "&action=formedit' target='EnipEdit'>edit</a> in Enipedia</div>";
content += "</ul>";
infowindow.setContent(content);
infowindow.open(map, marker);
}
function clearAllDataSetSelections(){
$("#wikipedia").prop('checked', false);
$("#carmav2").prop('checked', false);
$("#carmav3").prop('checked', false);
$("#osm").prop('checked', false);
$("#geo").prop('checked', false);
//$("#industryabout").prop('checked', false);
$("#euets").prop('checked', false);
$("#eprtr").prop('checked', false);
$("#lcpd").prop('checked', false);
}
function selectAllDataSets(){
$("#wikipedia").prop('checked', true);
$("#carmav2").prop('checked', true);
$("#carmav3").prop('checked', true);
$("#osm").prop('checked', true);
$("#geo").prop('checked', true);
//$("#industryabout").prop('checked', true);
$("#euets").prop('checked', true);
$("#eprtr").prop('checked', true);
$("#lcpd").prop('checked', true);
}
//if press enter, then pretend to press the search button
//otherwise the page just refreshes and nothing is returned
$("#searchbox").keyup(function(event){
alert('hi');
if(event.keyCode == 13){
$("#searchbutton").click();
}
});
</script>
</head>
<body>
<!-- This table needed to wrap text -->
<table width=800><tr><td>
Here you can search across multiple datasets for information about power plants. <br>
Further documentation and development notes can be found <a href="http://enipedia.tudelft.nl/wiki/Elasticsearch_on_Enipedia">here</a>.
</td></tr></table>
<table>
<tr>
<td colspan=2><hr></td>
</tr>
<tr><td colspan=2>
<input id="searchbox" type=text size=30 onkeydown="if (event.keyCode == 13) {search();}"/>
<input id="searchbutton" type="button" value="search" onclick="search();"/>
Number of Results:<select id="numSearchResults">
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
</select>
Search within current map view<input type=checkbox id="searchInMapView"/>
Fuzzy search<input type=checkbox id="fuzzyLikeThis"/>
<br>
</td>
</tr>
<tr>
<td><div id="map-canvas"></div></td>
<td valign="top">
Data sets to search:<br>
<input type=checkbox id="wikipedia" checked/>Wikipedia <br>
<input type=checkbox id="carmav2" onclick="search()"/>CARMA v2 <br>
<input type=checkbox id="carmav3" onclick="search()"/>CARMA v3 <br>
<input type=checkbox id="osm" checked/>OpenStreetMap <br>
<input type=checkbox id="geo" checked/>Global Energy Observatory (GEO) <br>
<!--<input type=checkbox id="industryabout" checked/>IndustryAbout <br>-->
<input type=checkbox id="euets" checked/>EU ETS <br>
<input type=checkbox id="eprtr" checked/>E-PRTR <br>
<input type=checkbox id="lcpd" checked/>LCPD <br>
<p>
Select:
<a onclick="selectAllDataSets()"><font color="blue">All</font></a>, <a onclick="clearAllDataSetSelections()"><font color="blue">None</font></a>
</td>
</tr>
</table>
<div id="search_status"></div>
<div id="table_goes_here"></div>
</body>
</html>