Widget:HsvSearch
From Shaker Pedia
//TNOTE: no longer active
//https://github.com/nervetattoo/elasticsearch
require "../top.php"; homeMenu();
- $libdir = __DIR__;
- $libdir = "/usr/share/php" ;
//$libdir = "/home/223045/users/.home/php"; $libdir = "/home/customer/www/awboc.com/php"; require_once $libdir . '/search/vendor/autoload.php';
use \ElasticSearch\Client; // The recommended way to go about things is to use an environment variable called ELASTICSEARCH_URL $es = Client::connection();
- $verbose = true;
$verbose = false;
// Alternatively you can use dsn string
- $host = "shaker-es.awboc.com";
$dns = $host;
- $dns = "es_admin:HefCoiv1@es.tnrnet.com:103";
- $es->get($id);
@$search = $_REQUEST['search']; @$filter = $_REQUEST['filter']; @$index = $_REQUEST['idx']; @$start = $_REQUEST['s'];
if(empty($index)) $index = 'hsv';
$indexes = Array( "hsv" => "hsvsearch", "wvcem" => "shakerwcem", );
- $search = "hsvsearch";
$idxname = $indexes[$index];
$tail = ($index == 'dict') ? '' : "&idx=cem";
$idxname = "hsvsearch";
$es = Client::connection("http://$dns/$idxname/dbid/");
//print "
"; print_r($es); print "
\n";
$dpick = Array(); if(!empty($filter)) $dpick[$filter] = 'selected';
$ipick = Array(); if(!empty($index)) $ipick[$index] = 'selected';
- include "top.php";
- homeMenu();
/* ?>
- /
if($index == 'dict') { showTerms(); } ?>
- if(empty($search) && empty($filter)) exit;
$qv = $search;
if(empty($qv)) $qv = "*";
if(empty($start)) $start = 0;
$step = 20;
function addAgg(&$query) { # pub_s sect_s village_s author_s name_s $ctrs = Array( "Publication" => "pubs_s", "Village" => "village_s", "Authors" => "author_s", "Names" => "name_s", "Sections" => "sect_s", ); $aggs = Array(); foreach($ctrs as $tag => $field) { $aggs[$tag] = Array("terms" => Array("field" => $field . ".ky")); } $query["aggregations"] = $aggs; }
if(empty($search)) { $q = Array( "size" => 0, /* "aggregations" => Array( "Pubs" => Array("terms" => Array("field" => "pub_s.ky")) ),
- /
); addAgg($q);
} else if(empty($filter)) {
$q = Array( // "fields" => Array(".*"), "from" => $start, "size" => $step, "query" => Array( "multi_match" => Array( "query" => $qv, "fields" => Array("content", "title", "pub_s"), ), ), /* "aggregations" => Array( "Pubs" => Array("terms" => Array("field" => "pub_s.ky")) ),
- /
/* "match" => Array("content" => $qv),
- /
"highlight" => Array( "pre_tags" => Array(""), "post_tags"=> Array(""), "fields" => Array( "content"=>Array("type" => "plain"), "title"=>Array("type" => "plain") ), ), ); addAgg($q);
} else { $q = Array( "stored_fields" => Array("*"), "from" => $start, "size" => $step, "query" => Array('filtered' => Array( "query" => Array( "match" => Array("content" => $qv) ),
"filter" => Array( "term" => Array("tags" => strtolower($filter)), )
),
),
"highlight" => Array( "pre_tags" => Array(""), "post_tags"=> Array(""), "fields" => Array("content"=>Array("type" => "plain"), ),
), /* "aggs" => Array( "tagList" => Array( 'filter' => Array("term" => Array("tags" => "PERSON"))), ),
- /
); addAgg($q); }
- $result = $es->search('content:*church*');
- $qj = json_encode($q);
$result = $es->search($q);
if($verbose) {
print "
"; print "QUERY="; print_r($q); print "RESULT="; print_r($result); print "\n";
}
if(isset($result['error'])) {
print "ES=($dns)
\n"; print_r($result); exit; } # print __LINE__ . ":"; print_r($_GET); $gurl = ""; $pre = "?"; foreach($_GET as $g => $v) { if($g == 's') continue; $gurl .= $pre . $g . "=" .urlencode($v); $pre = "&"; } $aggResult = $result['aggregations']; foreach($aggResult as $tag => $details) { $buck = $details['buckets']; if(count($buck)==0) continue; print "
\n"; // print "($tag)".print_r($details,true)."\n"; print "$tag:\n";\n";
foreach($buck as $p) {
print <<{$p['key']}({$p['doc_count']}) END; } print "
}
if(empty($search)) exit;
$step = 20; $top = $result['hits']['total']; print <<
Hits: {$top} END; if($top < $start) { print <<
[Restart Page position] END; exit; } for($i=0; $i<$top; $i += $step) { $si = $i+1; print <<
[$si] END; } //https://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-request-highlighting.html
print <<
\n"; #print_r($result);
\n"; print "
Title/Body TOP;
foreach($result['hits']['hits'] as $r) {
// print " ".print_r($r, true); $id = $r['_source']['id']; $page = $r['_source']['page']; $entry = $r['_source']['entry']; $showUrl = "toc.php?entry=$entry&hi=".urlencode($search) . $tail; $title = isset($r['highlight']['title']) ? $r['highlight']['title'][0] : $r['_source']['title']; # $title = $r['_source']['title']; # $title = $r['highlight']['title'][0] ; $volume = $r['_source']['doc_set']; /* print <<{$r['_source']['id']} {$title} {$r['_source']['tags']} HEAD; */ $purl = makePageUrl($page, $id, $entry); $phtml = "$entry"; print << $phtml Title:{$title} Where: {$volume} HEAD; print " \n"; if(isset($r['highlight']['content'])) { $hrow = $r['highlight']['content']; // print " $el:
\n"; foreach($hrow as $h) { // Cleanse ofor codes $h = preg_replace( Array('//', '/ /'), '', $h); print $h . "
\n"; } // if($verbose) { print "H:"; print_r($h); } } $d = $r['_source']; $fdisp = Array( "GuideI" => Array("title_s", " pub_s", "sect_s", "village_s", "info_t"), "GuideII" => Array("title_s", "pub_s", "sect_s", "info_t"), "xGuideIII" => Array("refnum_i", "title_s", "author_s", "name_s", "inforef_i", "info_t", "pub_s", "info_t"), "GuideIII" => Array("title_s", "author_s", "name_s", "pub_s", "info_t"), ); // print "END OF LOOP\n"; if(false) print " ".__LINE__.":".print_r($d,true); /* print ""; foreach($d as $k => $v) print $k . " "; */ $which = $d['doc_set']; $flist = $fdisp[$which]; print <<
END; foreach($flist as $sfld) { if(empty($d[$sfld])) continue; if($sfld == 'info_t') print "
\n"; print "".$sfld . ": " . $d[$sfld] . "
\n"; } } print "