diff -u solr/project_solr.module solr/project_solr.module --- solr/project_solr.module 19 Aug 2010 01:31:44 -0000 +++ solr/project_solr.module 19 Aug 2010 02:28:00 -0000 @@ -773,36 +773,39 @@ //---------------------------------------- /** - * Build a Solr query to find project nodes. + * Build and execute a Solr query to find project nodes. * - * @param object $project_type - * A taxonomy term object for the project type to query for. Must include at - * least 'tid' and 'name' fields. + * @param string $base_path + * The base path to use for the Solr query. * @param array $filters * Optional array of Solr filters to add to the query. * * @return * An ApacheSolr $query object. // TODO */ -function project_solr_build_project_query($project_type, $filters = array()) { - // Build a simple query. - $base_path = 'project/' . drupal_strtolower($project_type->name); - - $filters = isset($_GET['filters']) ? $_GET['filters'] : ''; - $query = apachesolr_drupal_query('', $filters, '', $base_path); +function project_solr_run_project_query($base_path, $filters = array()) { + $filterstring = isset($_GET['filters']) ? $_GET['filters'] : ''; + $query = apachesolr_drupal_query('', $filtersstring, '', $base_path); + + // Figure out all the fields we need to use as facets. + // First handle all the implicit filters we're going to add ourselves. + $facet_fields = array( + 'type', + 'im_project_release_api_tids', + ); + // Now add any fields used for optional filters from our caller. + if (!empty($filters)) { + foreach ($filters as $filter) { + $facet_fields[] = $filter['key']; + } + } $params = array( // The fields to return. 'fl' => 'id,nid,title', 'start' => 0, 'rows' => 5, // TODO: make this configurable. - // We need to be able to facet on the project vid value - // so we can pull project categories. - 'facet.field' => array( - 'type', - 'im_vid_' . _project_get_vid(), - 'im_project_release_api_tids', - ), + 'facet.field' => $facet_fields, // Filters are set below via explicit calls to add_filter() rather than // here in the $params array. This is because any filters passed to // apachesolr_drupal_query() will override filters set via the $params @@ -820,7 +823,7 @@ // This can e.g. be used to add available sorts. foreach (module_implements('apachesolr_prepare_query') as $module) { $function_name = $module . '_apachesolr_prepare_query'; - $function_name($query, $params, 'project_solr_view_category_block'); + $function_name($query, $params, 'project_solr_build_project_query'); } // We add our fields after the prepare_query() because prepare_query() @@ -830,7 +833,6 @@ // category. These values are not passed in the url, so we add them here // ourselves to ensure we only get the content we want. $query->add_filter('type', 'project_project'); - $query->add_filter('im_vid_' . _project_get_vid(), $project_type->tid); if (!empty($filters)) { foreach ($filters as $filter) { @@ -838,6 +840,19 @@ } } + // Cache the built query. Since all the built queries go through + // this process, all the hook_invocations will happen later. + apachesolr_current_query($query); + + // This hook allows modules to modify the query and params objects. + apachesolr_modify_query($query, $params, 'project_solr_view_category_block'); + + $solr = apachesolr_get_solr(); + $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params); + + apachesolr_static_response_cache($response); + apachesolr_has_searched(TRUE); + return $query; } @@ -1090,27 +1105,15 @@ function project_solr_fetch_category_items($project_type, $category_term) { $filters = array(); $filters[] = array( - 'key' => 'im_vid_' . _project_get_vid(), + 'key' => 'tid', 'value' => $category_term->tid, ); - $query = project_solr_build_project_query($project_type, $filters); + $base_path = 'project/' . drupal_strtolower($project_type->name); + $query = project_solr_run_project_query($base_path, $filters); - // Before we call modify_query() we want to save the URL query values so - // that our 'More' links are cleaner and free of excess junk. $query_values = $query->get_url_queryvalues(); - // Cache the built query. Since all the built queries go through - // this process, all the hook_invocations will happen later. - apachesolr_current_query($query); - - // This hook allows modules to modify the query and params objects. - apachesolr_modify_query($query, $params, 'project_solr_view_category_block'); - - $solr = apachesolr_get_solr(); - $response = $solr->search($query->get_query_basic(), $params['start'], $params['rows'], $params); - - apachesolr_static_response_cache($response); - apachesolr_has_searched(TRUE); + $response = apachesolr_static_response_cache(); $items = array(); if ($response->response->numFound > 0) { @@ -1126,6 +1129,7 @@ 'class' => 'more', ); } + return $items; }