From 6c7f36f6b81be093b52bcefa938caa35a272654f Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Thu, 1 Aug 2013 16:38:39 -0700 Subject: [PATCH] Use filtered query instead of filter in Elasticsearch Summary: The 'filter' works like this: Get all results matching query (all if there's no query), compute facets (if there are any) and then filter out the uninteresting results. The 'filtered' query applies the filters when searching, not when processing results. This is obviously not documented anywhere in the great Elasticsearch documentation. http://stackoverflow.com/questions/14007078/performance-of-elastic-queries We don't hit this problem very often as we usually use some query. Test Plan: Searched for open documents using Elasticsearch, verified the sent JSON, verified results. Reviewers: epriestley, wez Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D6643 --- .../PhabricatorSearchSelectController.php | 2 -- .../engine/PhabricatorSearchEngineElastic.php | 17 ++++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/applications/search/controller/PhabricatorSearchSelectController.php b/src/applications/search/controller/PhabricatorSearchSelectController.php index 0d6bbc30b0..3477614bd5 100644 --- a/src/applications/search/controller/PhabricatorSearchSelectController.php +++ b/src/applications/search/controller/PhabricatorSearchSelectController.php @@ -17,9 +17,7 @@ final class PhabricatorSearchSelectController $user = $request->getUser(); $query = new PhabricatorSearchQuery(); - $query_str = $request->getStr('query'); - $matches = array(); $query->setQuery($query_str); $query->setParameter('type', $this->type); diff --git a/src/applications/search/engine/PhabricatorSearchEngineElastic.php b/src/applications/search/engine/PhabricatorSearchEngineElastic.php index e7f0b693bc..106eac6ec2 100644 --- a/src/applications/search/engine/PhabricatorSearchEngineElastic.php +++ b/src/applications/search/engine/PhabricatorSearchEngineElastic.php @@ -97,7 +97,7 @@ final class PhabricatorSearchEngineElastic extends PhabricatorSearchEngine { $spec = array(); $filter = array(); - if ($query->getQuery()) { + if ($query->getQuery() != '') { $spec[] = array( 'field' => array( 'field.corpus' => $query->getQuery(), @@ -156,15 +156,14 @@ final class PhabricatorSearchEngineElastic extends PhabricatorSearchEngine { if ($filter) { $filter = array('filter' => array('and' => $filter)); - if ($spec) { - $spec = array( - 'query' => array( - 'filtered' => $spec + $filter, - ), - ); - } else { - $spec = $filter; + if (!$spec) { + $spec = array('query' => array('match_all' => new stdClass())); } + $spec = array( + 'query' => array( + 'filtered' => $spec + $filter, + ), + ); } if (!$query->getQuery()) {