Anyone worked, maybe privately, on a notifications integration module for ApacheSolr? I have a request to implement something along the lines of PubMed's saved searches email alert system (see attached for the UI or visit http://www.ncbi.nlm.nih.gov/sites/myncbi/searches/ - requires PubMed login).

Roughly, the functionality would include:

* Link/toggle on each search result page (eg in a block) to save the current search
* Block of recently saved searches (ex view-based block)
* Administrative UI that allows users to edit/delete previously saved searches
** Administrative UI Settings
*** Options to send new notifications at set intervals for new unique search results
*** Format selector to allow users several options for styling results (optional)
*** Maximum number of records to send at a given interval
* Anyone want to write this for me so I can spend more time drinking beer?

Questions:
* Does anyone know of similar existing efforts that I can use or help out with?
** If not, is this kind of functionality others in the community might find useful?
* Is this sort of module properly scoped?
** That is, should I be focusing on creating or using a more generic tool?
* Any general advice on how to best tackle problem (if you were going to do this you'd...)

Thanks in advance.

CommentFileSizeAuthor
myncbi.gif37.66 KBcfennell

Comments

janusman’s picture

I'm working on something similar: we would somehow flag queries (thinking it would be an add-on to the flag module), and there'd be an RSS feed (instead of an email) that aggregates those queries. I think I already reused some code from opensearch.module to build the RSS channel.

I like the idea of the email too; however in our case we're trying to delegate more functionality to external tools.

How does that sound?

cfennell’s picture

Sounds great, a few questions:

Flag:
* I'm guessing with Flag I'd lose named searches/search labels since you'd have a single flag for all searches?

RSS Feed:
* Is the feed an aggregation you mention a collection of the actual results as feed items or an aggregation of links to opensearch style feeds?
* Are you addressing the "new unique" items feature I mentioned above before adding results to an RSS feed, or are you letting your external tools handle determining what's new downstream (as with opensearch)?
** I'm guessing it's the latter.

In general, I sort of like the idea of utilizing RSS feeds as the search result delivery mechanism as I do also need to implement RSS feeds per result page a la opensearch.module at some point. If that were separated out, my ApacheSolr Notification module(s) could do everything I outlined above with the exception of actually requesting/retrieving the results. Maybe I can achieve that with just opensearch alone though. Hmm.

Do you have any code that you might be willing to share so I could see how/if the RSS piece might fit into my plan?

janusman’s picture

Maybe this code will help. Hope it's self-explanatory =)

/**
 * Outputs an RSS-formatted channel of aggregated search results defined by
 * fields in nodes.
 * 
 * @param $queries array
 *   An array of queries: each element should have an array with keywords and
 *   filters. E.g.: 
 *     $queries = array(
 *       array('kw' => 'keyword1 keyword2', 'filters' => 'tid:1',
 *       array('kw' => 'keyword', 'filters' => 'tid:2 tid:3',
 *       array('kw' => '', 'filters' => 'type:page')
 *     );
 * @param $channel_title string
 * @param $channel_path string
 *   A drupal path (url() syntax).
 * @param $channel_description string
 * @param $limit int
 *   Number of result items to return in the RSS channel. Defaults to 20.
 */
function mymodule_aggregatequeries($queries, $channel_title, $channel_path, $channel_description, $limit = 20) {
  if (!is_array($queries)) {
    return "";
  }

  // Run the queries against apachesolr
  $all_results = array();
  foreach ($queries as $query) {
    $results = apachesolr_search_execute($query['kw'], $query['filters'], 'created desc', 'search/apachesolr_search', 0);
    $response = apachesolr_static_response_cache();
    $found = $response->response->numFound;
    apachesolr_has_searched(FALSE);
    if ($found) {
      $all_results = array_merge($all_results, $results);
    }
  }

  // Reorder results by date created
  usort($all_results, create_function('$a,$b', 'return ($a["date"] < $b["date"]);'));

  // Limit to first $limit items
  $all_results = array_slice($all_results, 0, $limit);

  // Make links absolute
  foreach ($all_results as $index => $result) {
    $all_results[$index]['link'] = $GLOBALS["base_url"] . $all_results[$index]['link'];
  }
  
  // Output an XML channel with the items
 $namespaces = array(
    'xmlns:dc="http://purl.org/dc/elements/1.1/"',
    'xmlns:relevance="http://a9.com/-/opensearch/extensions/relevance/1.0/"'
  );

  foreach ($all_results as $result) {
    $items .= format_rss_item($result['title'], $result['link'], $result['snippet'], array('relevance:score' => (float)$result['score']));
  }

  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  $output .= "<rss version=\"2.0\" xml:base=\"". url('', array('absolute' => TRUE)) ."\" ". implode(' ', $namespaces) .">\n";
  $output .= format_rss_channel(
    $channel_title,
    url($channel_path, array('absolute' => TRUE)),
    $channel_description,
    $items,
    $GLOBALS["locale"]
  );
  $output .= "</rss>\n";

  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
  print $output;
  exit;
}
cfennell’s picture

Ok, thanks =)

rickvug’s picture

Sub. This is something that may be done as part of a project I'm a part of. I'll update if/when this piece of work moves forward and has been completed.

cfennell’s picture

I finally got back to this issue and have put together a few (experimental) modules that achieve search alert system. FWIW, I did look at the notifications framework to see how much of the flag_url_alerts module's functionality might be handled there but felt like it wasn't a good fit in the end. So, for now, flag_url_alerts is yet another notifications-style module, one that uses the flag system to manage subscriptions, but I am also open to change.

For now, I don't have project pages set up for these modules but have made them available via CVS:

http://drupalcode.org/viewvc/drupal/contributions/modules/apachesolr_ale...
http://drupalcode.org/viewvc/drupal/contributions/modules/flag_url/

There are a couple of README.txt files in flag_url and flag_url_alerts that minimally document the API etc. The README.txt in apachesolr alerts describes how to setup and test the functionality.

There's probably a lot that could change here, I consider this just a first step and would genuinely welcome contributions from others and am open to significant changes. I'm really focused on the basic functionality. Ping me with questions, feedback, ideas or indications of further interest.

Thanks.

kekkis’s picture

Subscribe. Very interesting.

pydubreucq’s picture

Subscribe too

jpmckinney’s picture

Status: Active » Closed (won't fix)

This should be a contrib module. Extremely unlikely to make it into this module.

wiremuse’s picture

The links to the code are no longer working. Is this still available some where?