Hello,

I altered the search block and added others fields . When my checkbox is TRUE i need to run hook_apachesolr_modify_query() because i need to add_filter but the problem is i dont know how to do it .I can modify queries but how can i do conditions?

function search_box_form_validate($form, &$form_state) {
if($form_state['values']['test_check'] == 1){
//here i can take the query but how can i call to modify???
$query = apachesolr_drupal_query();

}

}

Thanks very much for your time !

Comments

marthinal’s picture

If someone have the same question one day.I was studing the code and what i wrote before is not possible to do like this.i want to mean modify the query from validate or submit. Inside do_query function we call apachesolr_modify_query(). this function call the hooks and take fq values and put it inside $params['fq'].Then when it comes back to do_query do some operations and then $solr->search does the operation.

Using apache solr APIs its not possible if you dont modify with custom code because you need $form_state from your form to know what values you have to add the filter into modify or prepare hooks.

Maybe theres another way to do the same but using the Solr php library . These are my first steps using the module so im not a guru if you have something to say about it please write.

Thanks ! :)

marthinal’s picture

Searching i found a site that use advanced search http://mcgill.worldcat.org/advancedsearch and do exactly what i was talking about but dont know how at the moment.

ramprassad’s picture

If you want a search functionality using solr,the apachesolr_views module provides this functionality using which you can create an exposed form and add filters(you can create custom too) and expose them.
Then you can use the execute() in apachesolr_views_query.inc to include your conditions

marthinal’s picture

@ramprassad thanks very much for your post .

I was studing the way using an advanced search form into search/apachesolr_search/ altering the form i can put the fields i need.Example with &filters=title:hobbit author:tolkien in my url for example i can filter all that i need adding the fields into the index before.

But i need to take the values form the url so didnt finish ....

Im looking posibilities with apachesolr views like you recommend to me.

Thanks! :)

marthinal’s picture

apachesolr_views is very powerful but one of my options(filters) is an autocomplete that depends of one select field.it would be perfect but no filters handlers in the api views that can i use for that.Maybe the only way is a custom advanced form.

marthinal’s picture

It works.My workaround is

1) i created a module and added custom_search_form_search_form_alter function(you alter the search_form).

here i wrote my form's fields plus $form['#validate'][] = 'custom_search_search_form_validate';

2) into validate:

$keys = $form_state['values']['processed_keys'];

if (isset($form_state['values']['author'])) {
$keys = search_query_insert($keys, 'author', $form_state['values']['author']);
}

if (!empty($keys)) {
form_set_value($form['basic']['inline']['processed_keys'], trim($keys), $form_state);
}

3)ok here you have a form and when you submit the form you have the filter into your url.So we need &filter= at the beggining.

Its possible into apachesolr_search.module
this:
$form_state['redirect'] = array($base . $keys, $get);
to:
$form_state['redirect'] = array($base ,'filters='. $keys);//, $get);

If you know a better way please write :)

thanks !

cfennell’s picture

Issue tags: +Documentation, +examples

Another way you could do this and make use of the ApacheSolr Search API would be to

1) Add your form and validation hooks (as you have done):

function apachesolr_example_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form') {
    $form['stories_only'] = array(
      '#type' => 'checkbox', 
      '#title' => t('Limit to Stories Only.'),
      '#default_value' => ($_GET['stories_only']) ? (int) $_GET['stories_only'] : 0,
    );
    $form['#validate'][] = 'apachesolr_example_search_form_validate';
  }  
}

2) Tack your checkbox (or whatever) value onto the apachesolr_search array in the validation hook:

function apachesolr_example_search_form_validate($form, &$form_state) {
  $get = array();
  $get = json_decode($form_state['values']['apachesolr_search']['get'], TRUE);
  if ($form_state['values']['stories_only']) {
    $get['stories_only'] = (int) $form_state['values']['stories_only'];
  }
  else {
    //remove this param if it doesn't exist
    unset($get['stories_only']);
  }  
  $form_state['values']['apachesolr_search']['get'] = json_encode($get);
}

3) Set a filter in your prepare (or modify) hook based on the existence of this parameter:

function apachesolr_example_apachesolr_prepare_query(&$query, &$params, $caller) {    
  if ($_GET['stories_only']) {
    $query->add_filter('type', 'story');            
  }
} 

Please mark closed if this addresses your need. Thanks.

marthinal’s picture

@unexpand :
For me like you wrote doesnt work
1) Form:
function custom_search_form_search_form_alter(&$form, $form_state) {
$form['author']= array(
'#type' => 'textfield',
'#title' => t('author'),
'#maxlength' => 10,
'#weight' => 3,
'#required' => TRUE
);

$form['#validate'][] = 'custom_search_search_form_validate';
}

2) validate:
function custom_search_search_form_validate($form, &$form_state) {
$get = array();
$get = json_decode($form_state['values']['apachesolr_search']['get'], TRUE);
if ($form_state['values']['author']) {
$get['author'] = $form_state['values']['author'];
}
else {
//remove this param if it doesn't exist
unset($get['author']);
}
$form_state['values']['apachesolr_search']['get'] = json_encode($get);
}

3)

function apachesolr_example_apachesolr_prepare_query(&$query, &$params, $caller) {
if ($_GET['author']) {
$query->add_filter('author', $_GET['author']);
}
}

-----------------------------------------------------------------------------------------
if i filter using my text field into my url i have .../&author=tolkien.

The first is this url is not good. If i go here:http://mcgill.worldcat.org/search?q=ti%3Dhobbit+au%3Dtolkien&qt=results_...
DOESNT WORKS

but here :http://mcgill.worldcat.org/search?q=ti%3Ahobbit+au%3Atolkien&qt=notfound...
WORKS

I mean with this way you have &author=tolkien but i need &author:tolkien at the least but also &filters= before it.

Thanks .

cfennell’s picture

The example I gave you was for a checkbox, which is what you originally asked for. Filtering on a (presumably cck) text field will need to be done a little differently. For example, the "author" field in ApacheSolr refers to the node author, which means your code won't work unless Tolkien is authoring nodes on your site ;).

Good luck!

marthinal’s picture

@unexpand :
First at all thanks very much for your time. :)

In #5 i wrote : apachesolr_views is very powerful but one of my options(filters) is an autocomplete that depends of one select field.

Not a checkbox only :) .

Like i wrote in #6 It works.I want to mean i can put the filters field like textfields,checkbox... but like i said before maybe there's a better way because i touch a variable from apachesolr_search.module.

If someone know a different way and want to share...

Thanks very much!

marthinal’s picture

Another thing at the moment dont know how to do is.... http://mcgill.worldcat.org/search?q=ti%3Ahobbit+au%3Atolkien&qt=notfound...

here ti%3Ahobbit+au%3Atolkien theres no "&filter="... want to mean , if i have fields into my index like author or price i need &fq or &filter= into my url to say solr i want to filter using these fields.

marthinal’s picture

@unexpand refering #7:
English isnt my first language i dont want to give you the sense i dont like your other post only i didnt understand that way and to use it with a text field. Thanks :)

cfennell’s picture

@marthinal no worries _at all_. Tu ingles es mejor que me español, es cierto.

I'm not sure I could get a text field to work with core search w/out hacking apachesolr modules. The method above checkbox method depended upon there being something in the search keys box.

Personally, I decided to make use of the ApacheSolr API on a lower level and created my own search interface to handle these sorts of issues (e.g., https://www.ethicshare.org/publications?author=Smith&subject=stem%20cell...). At some point, trying to override the core search implementation becomes more work than simply rolling your own. James McKinney gives a nice overview of how to start rolling your own ApacheSolr search UI here: http://sf2010.drupal.org/conference/sessions/apache-solr-search-mastery, should you decide to take that route (Notice the McGill connection!).

The recommendation to use Views 3 is good one though, I just haven't had a chance to play with it much - perhaps there's a way to get the auto-suggest functionality you want? If I were starting a new search project out, I'd definitely take a good look at building UIs with Views 3.

Good luck.

marthinal’s picture

@unexpand:
Your spanish is good!! :)
Ok i understand, thanks very much for your explanations .

cfennell’s picture

s/Me/Mi <-see? ;)

marthinal’s picture

@unexpand :
Now i understand why i could have &whatiwant= to filter.This code helps me a lot :

$get = array();
$get = json_decode($form_state['values']['apachesolr_search']['get'], TRUE);
if ($form_state['values']['stories_only']) {
$get['stories_only'] = (int) $form_state['values']['stories_only'];
}

after into hook_search i can do operations using $_GET and build solr query passing filters to q.alt(check the query into terminal) without problems.
I have to say THANKS VERY MUCH for your code that helps me a lot !!!
For me we could close this issue at this point .
Thx!

marthinal’s picture

@unexpand :
Now i understand why i could have &whatiwant= to filter.This code helps me a lot :

$get = array();
$get = json_decode($form_state['values']['apachesolr_search']['get'], TRUE);
if ($form_state['values']['stories_only']) {
$get['stories_only'] = (int) $form_state['values']['stories_only'];
}

after into hook_search i can do operations using $_GET and build solr query passing filters to q.alt(check the query into terminal) without problems.
I have to say THANKS VERY MUCH for your code that helps me a lot !!!
For me we could close this issue at this point .
Thx!

cfennell’s picture

Great!

marthinal’s picture

Status: Active » Closed (fixed)