diff --git a/src/Plugin/EmulateElastic/TableKibanaHandler.php b/src/Plugin/EmulateElastic/TableKibanaHandler.php index a7e1fac7..02f2d268 100644 --- a/src/Plugin/EmulateElastic/TableKibanaHandler.php +++ b/src/Plugin/EmulateElastic/TableKibanaHandler.php @@ -113,26 +113,49 @@ protected static function postprocessQueryResult(Struct $queryResult, array $sea if (!array_key_exists('filter', $searchConds)) { continue; } - /** @var array> $propVal */ - $propVal = $hit['_source']; - /** @var array{fields:array,query:string} $filter */ - $filter = $searchConds['filter']; - /** @var array $props */ - $props = explode('.', $filter['fields'][0]); - foreach ($props as $prop) { - /** @var array|string $propVal */ - $propVal = $propVal[$prop]; + if (!self::isHitFiltered($hit['_source'], $searchConds['filter'])) { + unset($resultData['data'][$i]); } - $query = $filter['query']; - if ($query[0] === '"' && $query[-1] === '"') { - $query = substr($filter['query'], 1, -1); - } - if ($propVal === $query) { - continue; - } - unset($resultData['data'][$i]); } $resp['hits']['hits'] = $resultData['data']; + // Updating total to match the size of a filtered dataset + $resp['hits']['total'] = sizeof($resp['hits']['hits']); + } + + /** + * Checking if a result row suits all conditions from Kibana's filter + * + * @param array> $hitSource + * @param array{fields:array,query:string} $filter + * @return bool + */ + protected static function isHitFiltered(array $hitSource, array $filter): bool { + $query = $filter['query']; + if ($query === '*') { + return true; + } + if ($query[0] === '"' && $query[-1] === '"') { + $query = substr($filter['query'], 1, -1); + } + /** @var array $props */ + $props = explode('.', $filter['fields'][0]); + // Checking all result fields if the field wildcard is passed + if ($props === ['*']) { + foreach ($hitSource as $hitSourceVal) { + if ($hitSourceVal === $query) { + return true; + } + } + return false; + } + + // Extracting a result field to filter by + foreach ($props as $prop) { + /** @var array|string $hitSource */ + $hitSource = $hitSource[$prop]; + } + + return ($hitSource === $query); } /**