AJAX Guided Form Facets

Events happening in the community are now at Drupal community events on www.drupal.org.
jimijamesi's picture

I am using Drupal 6.22 / module apacheSolr 1.5 / Java based Solr 3.1
I would like to extend the default solr search form to include location input and guided facets in an expanded state (displayed via AJAX as part of the form, before any search results are displayed):

1) Location input (postal code field and slider to set proximity range connects to Solr Geo-spatial)
http://wiki.apache.org/solr/SpatialSearch
http://thedrupalblog.com/geospatial-apache-solr-searching-drupal-6-upgra...

2) AJAX Guided facets - based on term user types, with autosuggest module enabled.
Once user either selects term/clicks button/hits enter > the search form expands to display relevant Facets and Counts (Vocabulary & Content Type) of the query term typed by the user.

For the Guided facets. Two different yet similar solutions both from evolvingweb.ca
The Drupal module apachesolr-ajax
Alternatively pure AJAX-SOLR (not drupal dependant) found at https://github.com/evolvingweb/ajax-solr/wiki

Perhaps there is an easier way using the existing facet filters (default solr blocks) and a bit of ajax to display them with the search form?

I also found a recipe (from drupalcon chicago 2011 session apache solr chops) for adding dropdown facets (category/group/content-type) to the search form with hook_form_FORM_ID_alter(). This is similar to the drupal.org search which expands (refine this search) and displays radio buttons. But both of these examples are static-form-filters not live-facet-counts.

If anyone has opinions about how to proceed, or ideas on an easier implementation route, it would be appreciated. thx in advance.

AttachmentSize
auto facets.gif29.51 KB

Comments

Mockup

nick_vh's picture

Would you be able to create a kind of graphical mockup for this? Seems a really interesting idea to try out but we should be certain on what we are trying to achieve!

mockup

jimijamesi's picture

I attached a mockup above. I think the location inputs would be a hook extension of the solr search form, or a separate filter?? Then the facets would be a partial solr search result displayed via AJAX based on the users query term. This way we are not updating a form with live facets, just offering a pre result filter. Anyone have some thoughts as to best way to proceed?

This probably shouldn't be

nick_vh's picture

This probably shouldn't be that hard. You can take a look at ahah_helper to help you out on the callback which should be called when typing in the field. The form probably needs to be a custom function that executes the search and returns you with a list of its facets. This does imply a lot of queries on the solr index every time it writes a letter so be careful with speed when implementing this.

You might want to start of with http://drupal.org/project/apachesolr_ajax to see some concepts in action?

hook_form_search_block_form_alter()

jimijamesi's picture

trying to implement using hook_form_search_block_form_alter()
the form id: search_block_form (makes this only apply to the search block)

What is the best way to trigger the pre-result facet lookup.
A)If based on change in the user typing search query (a solr lookup per letter typed)
what would that looklike
perhaps if the user stops typing for 1second then it looksup
B)Autocomplete has two modes custom widget and drupal widget > the drupal widget does not automatically go to search results page when you click an auto-complete suggested term. So in this case it can lookup facets after autocomplete has finished.
C) hijack the search submit button & keyboard enter so they display facets > Then offer a new submit /enter as part of the callback
D) Include a checkbox for show pre result facets on a change in that checkbox (if true) then run custom function.

Which provides the best usability, I could not get scenario C to work. Currently exploring D
Anyone with an opinion and perhaps an idea what would the code look like for A or B

ahah path

jimijamesi's picture

If the search block with a pre result facet is displayed on evry page of site
Can I use a wildcard for ahah_helper_path(array('example6'))

also because solr for drupal v6 can only search on path 'search/apachesolr_search'
will this not work in blocks on other site pages.
Can it be set to a custom function and still return results on various pages that the block is sitting on (every other site page)?

    '#ahah' => array(
      'event' => 'change',
      'path' => ahah_helper_path(array('example6')),
      'wrapper' => 'example6-wrapper',
      'method' => 'replace',
      ),

pre-result facet

jimijamesi's picture

I have a custom function now that is triggered by a Display Facets checkbox (change event-using ahah_helper)
I also have a variable $current_search_typed for what the user has typed in the search field (user has not hit enter/submit)
What is the most direct way to return the facets??

$current_search_typed&facet=true&facet.field=tid&facet.field=type_name

It is my understanding that the full query must be run for the facets to be calculated & displayed

I found these 2 functions but perhaps there is a more direct route?

apachesolr_do_query($caller, $current_query, &$params)

apachesolr_search_execute($keys, $filterstring, $solrsort, $base_path = '', $page = 0, $caller = 'apachesolr_search')

executing solr query for facets

jimijamesi's picture

The block based search form is set to look up facets if the checkbox is true. However the apache query to return the facets is not working. It is returning a white screen.
I am trying to run the query from inside my custom drop_in_facets() function, maybe the solr does not like running inside my custom function?
I would like to supply the user some pre-facets before the user hits submit button
Perhaps hook_menu $items for the solr search/facets?
thx

/
* Implementation of hook_menu().
*/

function solrguided_menu() {
  $items = array(); 
  $items['refine'] = array(
    'title' => t('Guided Facets'),
    'description' => t('Filer results by Selecting a facet'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('solrguided_form_search_block_form_alter'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  // perhaps we need some hook_menu $items for facets tid and type_name -- or the whole solr return facets part??
  return $items;
}
  
/

* Implementation of hook_form_alter()
*/
function solrguided_form_search_block_form_alter(&$form, $form_state) {

  //ahah_helper_register($form, $form_state);
  // not needed thows error perhaps this form already invokes ahah_helper_register
 
  $form['refine'] = array(
    '#type' => 'fieldset',
    '#title' => t('Refine Facets'),
    '#weight' => 5,
    '#prefix' => '<div id="refine2-wrapper">', // This is our wrapper div
    '#suffix' => '</div>',
    '#tree' => TRUE, // don't forget to set #tree!
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
);
 
  $form['refine']['facetarea'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display Pre Result Filter'),
    '#default_value' => $form_state['values']['refine']['facetarea'],
    //'#default_value' => TRUE,
    '#required' => FALSE,
    '#ahah' => array(
          //'event' => 'change',
          'path' => ahah_helper_path(array('refine')),
          'wrapper' => 'refine2-wrapper',
          //'method' => 'replace',
          ),
    );


$current_typed_query = $form_state['values']['search_block_form'];
// this is what the user has typed in the searchbox but has not hit enter/submit

if ($form_state['values']['refine']['facetarea'] == TRUE) {
   

    $form['refine']['extra_element'] = array
    (
      '#value' => $current_typed_query,
    );
    drop_in_facets($current_typed_query);
    // triggering the function with the solr query in it
  }
}
function drop_in_facets($current_typed_query) {

solrguided_search_view();

solrguided_apachesolr_prepare_query(&$query, &$params) {
    $query->q($current_typed_query);
       
    $params['facet'] = TRUE;
    $params['facet.field'] = 'tid';
    $params['facet.field'] = 'type_name';
   
    }

// cache the built query
$current_query = apachesolr_current_query($query);
dsm($current_query);

//$response = $solr->search(...);
//$results = apachesolr_process_response($response, $final_query);
//theme('search_results', $results, ...)

}

Lucene, Nutch and Solr

Group organizers

Group categories

Projects

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: