Drupal 7 Apache solr search integration module is most useful to integrate Apache Solr with drupal. Apache solr search integration module will index all the content of your sites at solr which is full with advance search feature. You can exclude some of content types from being index under setting of this module. When you index your content at solr it will index most of the field of your content type but some of the custom field will not be indexed but Apache solr integration module provides number of hooks which you can use to add custom field to solr index, to alter the query before it sent to apache solr or alter the search result before being displayed.
For Solr Indexing:
You need to use hook_apachesolr_index_document_build. This hook is fairly new in the Solr module (since version 7.x-1.0-beta14) and it replaces the deprecated hook_apachesolr_update_indexhook. Here’s how to use it in your custom module:
<?php
function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
$price = $entity->field_price;
$document->addField('ss_product_price', $price / 100);
}
?>
Here notice i added the field the name ss_product_price. This is to follow the apache solr naming convention for dynamic field. Here ss means string single field. The first ‘s’ means its string value field, this field will hold only string value, and second ‘s’ means this is single value field. When we will search for some terms then solr will expect to search for single string in this field. If we store text (containing more string) value to this field then search will not work as expected. If the field you are indexing can have more then one value then loop over each value and us sm_ prefix with the field name. If you adding text field then try ts_ and tm_ for text single and text multi value respectively. Drupal Apache solr integration module support to index dynamic fields. For detail read the Dynamic field section in schema.xml shipped with apache solr module.
This tells Solr to add the new field into the set of returned fields.
For Sorl Searching:
Now you’ve got the new field in Solr’s search index. By default this new field doesn’t get returned when you search from Solr and therefore you need to use the hook_apachesolr_query_alter hook.
<?php
function MY_MODULE_form_apachesolr_query_alter($query) {
$query->addParam('fl', 'ss_product_price');
}
?>
Here i added ‘fl’ which stands for field in addParam function to add parameter to query.
End result:
Using these two hooks you can add custom field to solr index and can query to solr to search in that custom field. These two hooks will only work for Drupal 7. For drupal 6 follow this post.
Update
If adding custom field in solr index works but search within that field doesn’t work then there is chance that you indexed the field as String field and you are trying to search complete text within that field in this case search will not work. You need to index field as text field with ts_ prefix in custom field name and altering the query by adding ‘qf’ parameter in query like this:
function MY_MODULE_form_apachesolr_query_alter($query) {
$query->addParam('qf', 'ts_product_price');
}
Have a look at this post on drupal.org for more clarification.