Searching on only one SOLR field

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

I would like to query ApacheSOLR to return results based on one field in a node but I do not know how to do this. For example, instead of searching for the word 'dog' in a node's title, body, created date, etc, I only want to search the node title for the word 'dog'.

Can I do this using by implementing hook_apachesolr_modify_query? Any feedback on this would be much appreciated.

Comments

I have even done this hoping

karma.code's picture

I have even done this hoping that only title would remain to be searched and this still did not work:

function hook_apachesolr_modify_query(&$query, &$params, $caller){
  if($caller == 'solr_ui_autocomplete'){
    $fields =& apachesolr_get_solr()->getFields();
    foreach($fields as $key => $value){
      if($key !== 'title'){
        // remove everything except for the title field to search on
        unset($fields->$key);
      }
    }
  }
}

Am I getting warmer? colder?

Warmer.

robertdouglass's picture

In the same hook, you want to override the three following parameters:

<?php
        $params
['fl'] = $field_name;
       
$params['qf'][] = "{$field_name}^1.0";
       
$params['hl.fl'] = "$field_name";
?>

That example (slightly modified) is taken from the contrib/apachesolr_text module (available in the 6.x-2.x branch), which I suggest you study.

Thank you Robert. I am glad I

karma.code's picture

Thank you Robert. I am glad I came back to this post. I ended up figuring out to override the element awhile back. I can also do:

<?php
$query
->add_filter('title', $value);
$params['fl'] = 'title';
?>

Custom Scoring Each node

jaffarcheckout's picture

We are building a customized Scoring module for the Apache Solr Results.

The Custom function reads:

function hook_apachesolr_modify_query(&$query, &$params, $caller) {

$params['bf'][] = "recip(rord(is_field_mytestval),1000,1000,1000)^200";

}

In the above function

we assigned score to this is_field_mytestval ....

There is so difference tin the results, the above Query doesnot get exact result.

Lucene, Nutch and Solr

Group organizers

Group categories

Projects

Group notifications

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