5

I've created a pretty nice view with faceted filters following this guy's tutorial. It's basically a cheap way of doing getting the faceted feature without having to set up Solr.

The problem I have is that I would actually like to pre-query the index with some search terms from the homepage with a search form...basically:

  1. search for a term and get redirected to the view with the facets.

  2. use the facets to further filter my nodes.

How would something like this be possible?

Currently I have configured (in the Search API configurations) one server that uses database search as service class, and one Index hooked to that server, which indexes different fields (Content type and three different taxonomy terms) which are exposed as search facets in the view.

Number 2 is basically covered: I have a view page where all the nodes are listed and can be filtered through the facets... what I want is for the users to get to the front page, enter their search query (could have autocomplete or not, I'll figure later) hit "submit" and find all their search results in a page similar to the one I have with the facets... so they can further narrow their search. Kind of like when you look for something in linkedin.

2

For #1 You could try using Rules to catch the term and do a redirect. You could also use hook_form_FORM_ID_alter to catch it

Edit:

You can use "Drupal is initializing" for the event, and add a condition that examines the args coming in. For instance, search/some%20search%query would be:

arg(0) = search arg(1) = some search query

So here are the steps:

Event -> Drupal is initializing Condition -> Execute Custom PHP

$arg0 = arg(0);
$arg1 = arg(1);
if ($arg0 == 'search' && $arg1 == 'some search query') {
  drupal_goto('search/my-predetermined-facet');
}

That should do it.

  • Thanks for the answer. I have added more details of what I currently have, let me know if I'm missing something – la_f0ka May 16 '12 at 19:42
  • Basically you need (Part A) a mechanism that listens on the search box for certain terms (I usually use Rules), and then (Part B) a predetermined URL to redirect the user to which has all the right facets. I'm not sure how the module works, but usually when you select a bunch of facets it simply changes the URL. – Johnathan Elmore May 16 '12 at 21:07
  • yes, that's exactly what it does. So I can figure out how to get Rules to pre-query this for me and redirect it to my view with the facets... I'll dig more into this tomorrw. thanks – la_f0ka May 16 '12 at 21:27
  • @Jonathan .... I'm trying to figure out how to get rules to listen to the search box. While creating the rule I don't find anything "search related" in the React on event field – la_f0ka May 17 '12 at 18:04
  • Edited my answer with the details on how to do this in Rules – Johnathan Elmore May 17 '12 at 20:42
1

I wanted to chime in here as I found an alternative way of using Rules to do this.

Plus I feel what I have spent the past 3 days trying to figure out is similar and Johnathan's edited answer led me in the right direction to solve my issue.

How to redirect to a custom search page with facets ( a custom panel in my case ) from the default search block when using Search API w/ Solr and pre-fill the exposed filter with the term searched for by the user.

Install the Custom Search module. Use the Custom Search module to do turn on a custom search path to something unique like "mysearch/[key]" the [key] will extract the search term used in the default search box.

Next Use Rules to catch the custom path used by Custom Search, extract the search term(s), redirect where you want to end up, and, most importantly, pre-fill any facets or exposed filters with the "?filter_name=variable" appended to the path.

EDIT: Go to Rules -> Add New Rule -> React on event -> Drupal is initializing -> Add Condition -> Execute custom PHP code:

$arg0 = arg(0);
if ($arg0 == 'mysearch') {
  return true;
}

-- This is to catch the custom search path created with the custom search module above so we only apply the action to someone searching in the default search box.

Add Action -> Page redirect:

search/products?search_api_views_fulltext=<?php $arg1 = arg(1); print_r($arg1); ?>

-- You can fill in the query string variables with your facet names or search box ( like I did ) and use the PHP code to extract the incoming arguments you want to pass on to the search page so the facet/search box will be filled in correctly. -- "search_api_views_fulltext" is the name of the exposed filter ( the search box in this case )

The issue I ran into was when I had a facet field that was referenced through the main node. These show up in the Facets Tab like "Product Reference » Status" after adding an additional field to the index on the Fields Tab.

The URL for the Facet was returning { facet_field_name=%253ASelected_facet }. What should be a " : " that is URL encoded as " %3A " is being displayed as " %253A " but only for those fields that are referenced through, not the facets connected directly to the main node. The redirect part fails here and you are unable to pre-select the facet. You can create a manual link in the menu system with the " %253A " and everything works, its just the redirect. I have no answer for this one, so I moved all my facets to the main node and it works just fine.

I hope this helps.

  • Thanks a lot for your help. I have a really stupid question (probably because I never used Rules before)... but what is the "react on event" I should be searching for here (i.imgur.com/HAVFy.png)? Am I even in the correct place? – la_f0ka May 22 '12 at 20:49
  • Actually... I've been playing a lot with it and I managed to do it differently... I still don't know how to set up my search results to look the way I want (drupal.stackexchange.com/questions/31762/…) – la_f0ka May 23 '12 at 12:04
  • You are correct. I have updated my post with more details to reflect this for clarity. – Ravenight May 24 '12 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.