]> git.joonet.de Git - adminer.git/commitdiff
Elastic: Respect limit, page, order, select and first where
authorJakub Vrana <jakub@vrana.cz>
Fri, 19 Jul 2013 21:09:43 +0000 (14:09 -0700)
committerJakub Vrana <jakub@vrana.cz>
Fri, 19 Jul 2013 21:11:44 +0000 (14:11 -0700)
adminer/drivers/elastic.inc.php

index 3678eabfa3596c136ebbfb49949d90065e5664a7..72ff8f7c6d5e12fec076216c7e215d631de72c30 100644 (file)
@@ -10,10 +10,10 @@ if (isset($_GET["elastic"])) {
                        var $extension = "JSON", $server_info, $errno, $error, $_url;
                        
                        function query($path) {
+                               @ini_set('track_errors', 1); // @ - may be disabled
                                $file = @file_get_contents($this->_url . ($this->_db != "" ? "$this->_db/" : "") . $path, false, stream_context_create(array('http' => array(
                                        'ignore_errors' => 1, // available since PHP 5.2.10
                                ))));
-                               @ini_set('track_errors', 1); // @ - may be disabled
                                if (!$file) {
                                        $this->error = $php_errormsg;
                                        return $file;
@@ -90,8 +90,19 @@ if (isset($_GET["elastic"])) {
                        global $adminer;
                        $query = $adminer->selectQueryBuild($select, $where, $group, $order, $limit, $page);
                        if (!$query) {
-                               $query = "$table/_search";
+                               $query = "$table/_search?default_operator=AND"
+                                       . ($select != array("*") ? "&fields=" . urlencode(implode(",", $select)) : "")
+                                       . ($order ? "&sort=" . urlencode(ereg_replace(' DESC(,|$)', ':desc\1', implode(",", $order))) : "")
+                                       . ($limit ? "&size=$limit" . ($page ? "&from=" . ($page * $limit) : "") : "")
+                               ;
+                               foreach ((array) $_GET["where"] as $val) {
+                                       if ("$val[col]$val[val]" != "") {
+                                               $query .= "&q=" . urlencode(($val["col"] != "" ? "$val[col]:" : "") . $val["val"]);
+                                               //! uses only last condition
+                                       }
+                               }
                        }
+                       echo $adminer->selectQuery($query);
                        $search = $this->_conn->query($query);
                        if (!$search) {
                                return false;
@@ -99,12 +110,18 @@ if (isset($_GET["elastic"])) {
                        $return = array();
                        foreach ($search['hits']['hits'] as $hit) {
                                $row = array();
-                               foreach ($hit['_source'] as $key => $val) {
-                                       $row[$key] = (is_array($val) ? implode(", ", $val) : $val);
+                               $fields = $hit['_source'];
+                               if ($select != array("*")) {
+                                       $fields = array();
+                                       foreach ($select as $key) {
+                                               $fields[$key] = $hit['fields'][$key];
+                                       }
+                               }
+                               foreach ($fields as $key => $val) {
+                                       $row[$key] = (is_array($val) ? json_encode($val) : $val); //! display JSON and others differently
                                }
                                $return[] = $row;
                        }
-                       echo $adminer->selectQuery($query);
                        return new Min_Result($return);
                }