From 1f14ab470f4f42854563175d8a6ed24f15ce207b Mon Sep 17 00:00:00 2001 From: Peter Knut Date: Sun, 21 Mar 2021 15:56:46 +0100 Subject: [PATCH] Replace deprecated "filtered" query with "bool" query - Allow to choose "must", "should", "must_not" condition. - Add system "_id" column to the field list. So it can be used in search condition. --- adminer/drivers/elastic.inc.php | 58 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/adminer/drivers/elastic.inc.php b/adminer/drivers/elastic.inc.php index fe33c315..bf46fdcb 100644 --- a/adminer/drivers/elastic.inc.php +++ b/adminer/drivers/elastic.inc.php @@ -111,7 +111,7 @@ if (isset($_GET["elastic"])) { function select($table, $select, $where, $group, $order = array(), $limit = 1, $page = 0, $print = false) { global $adminer; $data = array(); - $query = "$table/_search"; + $query = (min_version(6) ? "" : "$table/") . "_search"; if ($select != array("*")) { $data["fields"] = $select; } @@ -131,21 +131,15 @@ if (isset($_GET["elastic"])) { } foreach ($where as $val) { list($col, $op, $val) = explode(" ", $val, 3); - if ($col == "_id") { - $data["query"]["ids"]["values"][] = $val; - } - elseif ($col . $val != "") { - $term = array("term" => array(($col != "" ? $col : "_all") => $val)); + if ($col . $val != "") { + $term = array(($col != "" ? $col : "_all") => $val); if ($op == "=") { - $data["query"]["filtered"]["filter"]["and"][] = $term; - } else { - $data["query"]["filtered"]["query"]["bool"]["must"][] = $term; + $data["query"]["bool"]["filter"][] = array("term" => $term); + } elseif (in_array($op, array("must", "should", "must_not"))) { + $data["query"]["bool"][$op][]["match"] = $term; } } } - if ($data["query"] && !$data["query"]["filtered"]["query"] && !$data["query"]["ids"]) { - $data["query"]["filtered"]["query"] = array("match_all" => array()); - } $start = microtime(true); $search = $this->_conn->query($query, $data); if ($print) { @@ -164,7 +158,7 @@ if (isset($_GET["elastic"])) { if ($select != array("*")) { $fields = array(); foreach ($select as $key) { - $fields[$key] = $hit['fields'][$key]; + $fields[$key] = $key == "_id" ? [$hit["_id"]] : $hit['fields'][$key]; } } foreach ($fields as $key => $val) { @@ -362,19 +356,29 @@ if (isset($_GET["elastic"])) { } } - $return = array(); - if ($mappings) { - foreach ($mappings as $name => $field) { - $return[$name] = array( - "field" => $name, - "full_type" => $field["type"], - "type" => $field["type"], - "privileges" => array("insert" => 1, "select" => 1, "update" => 1), - ); - if ($field["properties"]) { // only leaf fields can be edited - unset($return[$name]["privileges"]["insert"]); - unset($return[$name]["privileges"]["update"]); - } + $return = array( + "_id" => array( + "field" => "_id", + "full_type" => "text", + "type" => "text", + "privileges" => array("insert" => 1, "select" => 1), + ) + ); + + foreach ($mappings as $name => $field) { + $return[$name] = array( + "field" => $name, + "full_type" => $field["type"], + "type" => $field["type"], + "privileges" => array( + "insert" => 1, + "select" => 1, + "update" => 1, + ), + ); + if ($field["properties"]) { // only leaf fields can be edited + unset($return[$name]["privileges"]["insert"]); + unset($return[$name]["privileges"]["update"]); } } return $return; @@ -477,7 +481,7 @@ if (isset($_GET["elastic"])) { return array( 'possible_drivers' => array("json + allow_url_fopen"), 'jush' => "elastic", - 'operators' => array("=", "query"), + 'operators' => array("=", "must", "should", "must_not"), 'functions' => array(), 'grouping' => array(), 'edit_functions' => array(array("json")),