Part 2: Apache Solr - Manually Controlling a Custom Fields Bias
In part one of this post I showed you how to create a custom field to be added to Apache Solr index and altering the search based on this field. In this second part I will be showing you how you can define a custom field to be listed within the 'field bias' admin settings in the Apache Solr module.
Setting the field biases
Most of the time just setting a custom field to be added to your index is all you will need. But sometimes there is the need to be able to control the bias of your custom fields when Solr does the search.
Typically you would manage this in the Apache Solr admin pages by going to /admin/config/search/apachesolr/settings. This will show you a list of your Solr instances that you have configured for the site. To change the biases, click on the ‘bias’ link against the relevant instance.
Within this section if you choose the ‘Field Biases’ tab on the left hand site you can set the bias for fields within the site. This will affect the what is more relevant when Apache Solr does a search.
This is fine if you just want to set the bias on the standard fields that are available, but what if you want to be able to control the bias of the new custom field that you have just created?
Allowing manual field biases control on your custom field
Looking at the Apachesolr module in more detail shows that this list of bias fields is made up of ‘default’ fields (e.g. content, h1, h2 etc.) and any fields that are ‘text’ fields.
If you have created a custom field and your field is a string (e.g. 'ss_my_field') then you would think that this would then show up as it is a string field which is text - right?
Well no ..... after looking at the field definitions in more details (as shown in part 1), string fields are different to text fields, which is why the custom string field wasn't showing in the list.
Therefore in order for your custom field to appear in the list of 'bias fields', you need to define your custom field as a 'text' field rather than a 'string' field (e.g. ‘ts_my_field’). Having done this, re-index the content and the custom field now shows in this list of field biases.
Hooray I hear you shout .... well almost. The field shows in the list but the label of this custom field shows as the field name (e.g. ‘ts_my_field’) - not very readable or friendly.
Helpfully there is a hook provided by the ApacheSolr module to map fields to a label for display:
/**
* Implements hook_apachesolr_field_name_map_alter().
*/
function MY_MODULE_apachesolr_field_name_map_alter(&$map) {
$map['ts_my_field'] = t('This is the label for my custom field');
}
Now your new custom field shows in the list of 'bias fields' and has a nice friendly label for it. So you can now set the relevant value you would like for it to alter your search - happy days!
Read morePart 2: Apache Solr - Manually Controlling a Custom Fields BiasBy Mike Davis | 9th September 2014