If you need to expose custom fields (whether created with Fields or in a module) to Solr, first determine which of the following applies:

  1. You need to make sure Solr indexes a hidden field so it can be searched along with the other fields (like body, title, etc).
  2. You need to expose a field to Solr so you can create a custom search against that field alone.
  3. You want to force search to look at one field, not the overall content of nodes.

Use Case #1: Exposing Fields to Search (or customizing what's exposed to search)

If you picked #1, the answer is much simpler. The best way to get Solr to index fields is to make sure they're displayed so that Solr can see them. But if you don't want a field to display to users, you can add a custom display that is specifically for search. It's called "Search index".

To add this display, go to Admin > Structure > Content Types > [Your content type] > Manage Display, then scroll down to the bottom of the Default display page and select the checkbox in front of "Search index" in "Custom Display Settings" and click Save. This will create the Search index display, which you can then customize to your heart's content. Whatever you expose in this display is what will be indexed for search. Make sure all fields you want indexed are enabled for this display. Then re-index your nodes.

Note: You'll also notice a number of other handy custom displays at your fingertips here including Print, RSS, Tokens, Author Preview, and Search results, which may come in handy for customizing your user experiences.

Once you have an 'index' and a 'server' configured you will need configure which fields are exposed to Solr

admin/config/search/search_api > 'Edit' on the index > Fields

Use Cases #2 & #3: Exposing a field to Solr for a Custom Search

If you want to create a custom search specifically on a field, perhaps in an advanced search, you need to use hook_apachesolr_index_document_build() (which replaced hook_apachesolr_update_index()).

Try something like this:

function customsolr_apachesolr_index_document_build(&$document, $entity, $entity_type) {
  if ($entity_type == 'node' && $entity->type == 'custom_node_type') {
    $document->addField('ts_field_text_field_name', $entity->field_text_field_name['und'][0]['value']);
    $document->addField('is_field_integer_field_name', $entity->field_integer_field_name['und'][0]['value']);
  }
}

For more information on defining field types using 'ts', 'is', 'tm', etc., see the dynamic field definitions in the schema.xml that ships with Drupal's Apache Solr module.

Comments

Johann Wagner’s picture

For the author and posting information, even when their display is deactivated in Admin > Structure > Content Types > [Your content type] > Edit, they do appear in the search result snippets.

Johann Wagner’s picture

Finally, it was a theme related bug using Rubik beta 8 as administration theme; more about it : http://drupal.org/node/1199648#comment-7471890. I used hook_process() to clean the theme injected data.

arnoldbird’s picture

#1 will seem to not work if you search under the Content tab (node search) rather than under the Site tab (solr). You can deactivate the node search at /admin/config/search/settings.