getTimestamp(); return sprintf("%s-%s-%s",$d[2],$d[0],$d[1]); } else { return $in; } } function indent($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; for ($i=0; $i<=$strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else if(($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos --; for ($j=0; $j<$pos; $j++) { $result .= $indentStr; } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos ++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } $prevChar = $char; } return $result; } /** * Send a POST requst using cURL * @param string $url to request * @param array $post values to send * @param array $options for cURL * @return string */ function curl_post($url, array $post = NULL, array $options = array()) { $enc = json_encode($post); # $enc = str_replace("[]","{}",$enc); #echo("
".indent($enc)."
"); $defaults = array( CURLOPT_POST => 1, CURLOPT_HEADER => 0, CURLOPT_URL => $url, CURLOPT_FRESH_CONNECT => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FORBID_REUSE => 1, CURLOPT_TIMEOUT => 4, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $enc ); $ch = curl_init(); curl_setopt_array($ch, ($options + $defaults)); if( ! $result = curl_exec($ch)) { trigger_error(curl_error($ch)); } curl_close($ch); return $result; } FUNCTION shorturl($url){ $length = STRLEN($url); IF($length > 45){ $length = $length - 30; $first = SUBSTR($url, 0, -$length); $last = SUBSTR($url, -15); $new = $first."[ ... ]".$last; RETURN $new; }ELSE{ RETURN $url; } } function printNav() { global $start, $count, $hits; echo ''; } #require 'vendor/autoload.php'; if ($_GET) { require_once __DIR__ . '/vendor/autoload.php'; $elasticaClient = new \Elastica\Client(array('host' => $eshostname, 'port' => $esport)); $elasticaQueryString = new Elastica\Query\QueryString(); if (array_key_exists("id",$_GET)) { $elasticaQuery = new Elastica\Query\Ids("doc",array($_GET["id"])); } else { $elasticaQuery = new Elastica\Query("doc"); $start = 0; $count = 10; if (array_key_exists("st",$_GET) and $_GET["st"] !== "") { $start = $_GET["st"]; } if (array_key_exists("c",$_GET) and $_GET["c"] !== "") { $count = $_GET["c"]; } $elasticaQuery->setFrom($start); // Where to start? $elasticaQuery->setLimit($count); // How many? } $elasticaIndex = $elasticaClient->getIndex($esindex); $elasticaFilter = new \Elastica\Filter\BoolAnd(); if (array_key_exists("qs",$_GET)) { $querystring = $_GET["qs"]; #$request->query(Sherlock::query()->Term()->field("message") # ->term("ElasticSearch")); $query["query"] = ["query_string" => [ "default_field" => "body", "query" => $querystring ]]; $elasticaQueryString->setQuery($_GET["qs"]); $elasticaQuery->setQuery($elasticaQueryString); $elasticaQuery->setHighlight(array( "fields" => array( "body" => array( "fragment_size" => 200, "number_of_fragments" => 1 ) ) )); } $query["filter"] = []; $source = $_GET["qc"]; if ($source and $source != "all") { $query["filter"] = [ "term" => [ "source" => $source ] ]; $elasticaFilterSource = new \Elastica\Filter\Term(); $elasticaFilterSource->setTerm('source', $source); $elasticaFilter->addFilter($elasticaFilterSource); $filter = 1; } $datefrom = $_GET["qdf"]; $dateto = $_GET["qdt"]; if ($datefrom or $dateto) { $streamtime = []; if ($datefrom) { $streamtime["from"] = convertdate($datefrom); #$streamtime["include_lower"] = true; } if ($dateto) { $streamtime["to"] = convertdate($dateto); #$streamtime["include_upper"] = true; } $query["filter"]["range"] = [ $estimefield => $streamtime ]; #var_dump($streamtime); $elasticaFilterDate = new \Elastica\Filter\Range('stream_time',$streamtime); #$elasticaFilter->addFilter($elasticaFilterDate); #$elasticaFilterDate = new \Elastica\Filter\Range(); #$elasticaFilterDate->addField('date',$streamtime); $elasticaFilter = $elasticaFilterDate; $filter = 1; } #$queryset = []; #if (count($query["filter"]) > 0) { # $fullquery = ["query" => ["filtered" => &$query]]; # $queryset = &$fullquery["query"]; # $queryset["fields"] = array("stream_time", "source", "url"); #} else { # unset($query["filter"]); # $queryset = &$query; # $fullquery = &$queryset; # $queryset["fields"] = array("stream_time", "source", "url"); #} #$queryset["highlight"] = [ "fields" => [ "body" => [ "number_of_fragments" => 1 ] ] ]; if (!array_key_exists("id",$_GET)) { $elasticaQuery->setFields(array($estimefield, "source", "url")); $elasticaFacetDate = new \Elastica\Facet\DateHistogram("date"); $elasticaFacetDate->setField($estimefield); $elasticaFacetDate->setInterval("2h"); #$elasticaFacetSource = new \Elastica\Facet\Terms("source"); #$elasticaFacetSource->setField("source"); #$elasticaFacetSource->setAllTerms(true); #$elasticaQuery->addFacet($elasticaFacetDate); #$elasticaQuery->addFacet($elasticaFacetSource); } if ($filter) { #$elasticaQuery->setFilter($elasticaFilter); #$elasticaQuery->setQuery(new \Elastica\Query\Filtered($elasticaQueryString,$elasticaFilter)); $elasticaQuery->setFilter($elasticaFilter); } #$fullquery = [ # "query" => [ # "filtered" => [ # "query" => [ # "query_string" => [ # "query" => $_GET["qs"] # ], # "filter" => [ # "term" => [ # "source" => $_GET["qc"] # ] # ] # ] # ] # ] #]; #$url = "http://localhost:9200/kba2012/doc/_search?".http_build_query([ "from" => $start, "size" => $count]); # #$result = curl_post($url, $fullquery); #$results = json_decode($result,true); #if (is_array($results) and array_key_exists("hits",$results)) { # $hits = $results["hits"]["hits"]; # $total = $results["hits"]["total"]; #} //Search on the index. #echo "
";
  #print json_encode($elasticaQuery->toArray());
  #echo "
"; $elasticaResultSet = $elasticaIndex->search($elasticaQuery); $elasticaResults = $elasticaResultSet->getResults(); #$elasticaFacets = $elasticaResultSet->getFacets(); $hits = $elasticaResultSet->getTotalHits(); if (array_key_exists("raw",$_GET)) { if (array_key_exists("id",$_GET)) { foreach ($elasticaResults as $elasticaResult) { $data = $elasticaResult->getData(); echo $data["body"]; } } else { foreach ($elasticaResults as $elasticaResult) { $id = $elasticaResult->getId(); $data = $elasticaResult->getData(); $snippets = $elasticaResult->getHighlights(); echo $id."\n"; } } exit; } #echo "
";
  #foreach ($elasticaResults as $elasticaResult) {
  #    var_dump($elasticaResult->getData());
  #    var_dump($elasticaResult->getHighlights());
  #}
  #echo "
"; } ?> getData(); echo '
'; echo $data["body"]; echo '
'; echo "
";
    var_dump($elasticaResult);
    echo "
"; } } else { printNav(); #echo ''; echo '
'; foreach ($elasticaResults as $elasticaResult) { $id = $elasticaResult->getId(); $data = $elasticaResult->getData(); $snippets = $elasticaResult->getHighlights(); echo '
'.$id.' - '.$data["source"].' - '.$data[$estimefield].'
'; echo ''; echo '
'.$snippets["body"][0].'...
'; } #echo "
";
  #var_dump($elasticaFacets);
  #echo "
"; echo '
'; printNav(); } #foreach ($hits as $hit) { # $source = $hit['fields']; # echo '
'.$hit["_id"].' - '.$source["source"].' - '.$source["stream_time"].'
'; # echo '
'.shorturl($source["url"]).'
'; # echo '
'.$hit["highlight"]["body"][0].'
'; #} #echo("
".indent($result)."
"); ?>