To return a custom number of search items, for the exhibit apachesolr feed or distributed search, I set and reset variable apachesolr_rows.

Not sure if this is the best solution, but how about if the code in apachesolr_search_search case 'search' was placed in its own function after defining $params?

That way, do not have to replicate most of the code inside of apachesolr_search_search in order to return results or set/reset variable.

Comments

aufumy’s picture

StatusFileSize
new17.66 KB

This patch is against current dev branch.

Moved code inside apachesolr_search_search() to
* apachesolr_search_params($query) which returns $params
* apachesolr_search_query($keys, $query, $params) which contains $solr->search() and returns $results;

So that when returning custom search results, can create a hook_apachesolr_modify_query with a custom caller (e.g. mytest_caller):

$keys = 'test';
$query = apachesolr_drupal_query($keys, 'type:blog');
if (is_null($query)) {
  throw new Exception(t('Could not construct a Solr query()'));
}       
$params = apachesolr_search_params($query);

apachesolr_modify_query($query, $params, 'mytest_caller');
if (!$query) {
  return array();
}
$results = apachesolr_search_query($keys, $query, $params);
aufumy’s picture

Title: Custom number of search results returned, for a different purpose than the pager number » Move code out of apachesolr_search_search into separate functions
aufumy’s picture

StatusFileSize
new17.66 KB

Changed the function name from apachesolr_search_query to apachesolr_search_results for more clarity.

aufumy’s picture

StatusFileSize
new19.74 KB

Moved $_GET variables to the top, and passed it to _apachesolr_search_search, such as $filters, $solrsort, $page.

So that when other modules would like to return results from apache solr module can call on
_apachesolr_search_search($keys, $filters, $solrsort, $page);

e.g.
$results = _apachesolr_search_search('test', 'type:blog', '', 3);

pwolanin’s picture

Status: Needs review » Needs work

If this is supposed to be a public API function, then it should not have a leading _

aufumy’s picture

Status: Needs work » Needs review
StatusFileSize
new19.75 KB

This patch, the function name is

function apachesolr_search_searchresults($keys, $filters = '', $solrsort = '', $page = 0, $caller = 'apachesolr_search')

pwolanin’s picture

This approach doesn't seem to me to be maximizing re-usability of the code. hook_search should be calling 3 or 4 functions that each do a subset of the current monster code, so that each piece could be reused or substituted, I think.

pwolanin’s picture

StatusFileSize
new18.28 KB

Like this - just moving code around, no functional changes.

aufumy’s picture

StatusFileSize
new4.83 KB

How about hook_search calling one function which does some processing/validation and calls the 3 or 4 functions.

    case 'search':
      $filters = isset($_GET['filters']) ? $_GET['filters'] : '';
      $solrsort = isset($_GET['solrsort']) ? $_GET['solrsort'] : '';
      $page = isset($_GET['page']) ? $_GET['page'] : 0;

      return apachesolr_search_searchresults($keys, $filters, $solrsort, $page, 'apachesolr_search');

inside function apachesolr_search_searchresults() is the try/catch block with

    $solr = apachesolr_get_solr();
    $params = apachesolr_search_params_basic($query, $solr);
    apachesolr_search_add_facet_params($params, $query, $solr);
    apachesolr_search_add_boost_params($params, $query, $solr);

as well as apachesolr_drupal_query(), apachesolr_current_query(), apachesolr_modify_query(), $solr->search(), apachesolr_static_response_cache(), apachesolr_has_searched() and finally returning apachesolr_process_response()

On the note of all this moving around, as well how about the use of >module_load_include() to move the search code, which is only used on search pages out of the apachesolr_search.module file, to reduce the overall load to drupal pages.

Likely, the increase in performance of drupal will be minimal for this type of change in only one module, but it might add up with more modules.

Scott Reynolds’s picture

I looked at the latest patch (#8) and I love it. This is great stuff. Big huge +1. Patch looks solid to me. Im excited for this one

Scott Reynolds’s picture

Need to examine the functions and the params passed into each one. for instance apachesolr_search_params_basic($query, $solr) doesn't use the $solr object. The only one that uses the solr object is the boost function, and that doesn't use the query object. So a lil more refactoring is probably required.

Consider declaring the variable type for the query object in the functions as well? I.E.

function apachesolr_search_params_basic(Drupal_Apache_Solr_Query_Interface $query)
pwolanin’s picture

@Scott - yes, $solr is not needed in all, but thought in terms of having these functions as an APi there is not harm in requiting it so that we can change the implementation of each function more readily.

@afumy - yes, once the refactoring is working, I'd expect to move a bunch of this code into an include file.

Scott Reynolds’s picture

I would urge you not to move this to an include file

apachesolr_search_params_basic($query, $solr);
apachesolr_search_add_facet_params($params, $query, $solr);
apachesolr_search_add_boost_params($params, $query, $solr);

If another module needs these functions it should note be put in a separate file (/me glares at pathauto)

Re: passing in extra params
- readability
- clarity

I have really no strong arguments other then Im sure CS professors would be offended. I think its wrong, and I wouldn't commit it with the $solr objects.

pwolanin’s picture

@Scott - moving code to an include file to not is really that bad, really a trade-off in terms of how often it's needed.

Scott Reynolds’s picture

Then create a way to lazy load it thats not _pathauto_include(). Basically, don't require the 'other' module to know that I have to include a file.

apachesolr_search_params_basic() {
   module_load_include('apachesolr_api.inc', 'apachesolr_search');
  return _apachesolr_search_params_basic();
}
pwolanin’s picture

StatusFileSize
new20.88 KB

This is closer to what afumy is suggesting, but I think it makes sense for the try/catch to be outside to an external caller could handle the Exception differently.

pwolanin’s picture

StatusFileSize
new20.94 KB

slightly better abstraction - pass in $base_path too. Also, make sorting work...

pwolanin’s picture

committed this last patch to CVS - please test before marking fixed so I can roll a release.

pwolanin’s picture

Status: Needs review » Fixed

ok, well it's in beta10

aufumy’s picture

Awesome Peter, thank you.

This really helps, as I had some people using the pre-dismax style of returning custom search results, by calling apachesolr_search_search('search', "keywords type:blog"), now can call apachesolr_search_execute('keywords', 'type:blog')

aufumy’s picture

I am of the opinion that CS professors would be offended to learn that only ~1% or less of code being loaded into each and every page load is used. Possibly a couple of megabytes worth of memory could be freed up.

Functions that other modules are likely to use might make sense to be inside the .module file, otherwise there is no point imo.

Scott Reynolds’s picture

Functions that other modules are likely to use might make sense to be inside the .module file, otherwise there is no point imo.

http://drupal.org/project/apachesolr_views
http://drupal.org/project/localsolr
http://drupal.org/project/apachesolr_ubercart
http://drupal.org/project/apachesolr_rdf

4 immature modules that are using apache solr intergration as an api. Seems to me these api functions could be pretty useful.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.