I am trying to add a custom field to solr index and then implement search that can find values of the custom field. Here is how I try to do it in solr_custom module but it's not working:

function solr_custom_apachesolr_update_index(&$document, $node) {
  // Index field_lastname as a separate field
      $document->ss_field_lastname = "some value for lastname"; // in real life I get this value dynamically of course
}

function solr_custom_apachesolr_modify_query(&$query, &$params, $caller) {
  $params['fl'] .= ',ss_field_lastname';
}

When I index the site, the ss_filed_lastname gets into the index. But when I try to do solr search it's not able to find any custom values. If I do the search searching for example node titles then the search works. So it is my custom field that does not work.

Any help?

I used this blogpost as my starting point: http://www.drupalconnect.com/blog/steve/adding-custom-fields-apache-solr...

Comments

pwolanin’s picture

Status: Active » Fixed

"s" stands for STRING. You cannot do full-text search against a string - you can only match the full string exactly. String fields are appropriate for faceting on fixed values, or sorting.

Perhaps you should try indexing as a text field instead.

Also, you need to specify fields to search as the qf param if you want to search the text. Take a look at Robert's recent commits for CCK fields in the 6.x-2.x branch.

finn lewis’s picture

I was having the same issue as wpanssi.

With pwolanin's pointer and a little experimenting, I managed to get it working, by modifying the example code as follows:

function solr_custom_apachesolr_update_index(&$document, $node) {
  // Index field_lastname as a separate field using ts_ prefix to indicate text and single value 
  $document->addField('ts_field_lastname', "some value for lastname");
}

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

  // tell the dismax query handler that we want to search on our custom field and give it a boost of 4.0
  $params['qf'][] = 'ts_field_lastname^4.0';

  // add in other default fields
  $params['qf'][] = 'title^5.0';
  $params['qf'][] = 'body^40.0';

  ... //add all other required fields - (name, tags, etc)

}

I initially only added the new custom field to the params['qf'] array, but found that searches on the default body field stopped working. It seems we need to add all fields we want to search on, not just the new custom fields. I understand this could be achieved in the solrconfig.xml by modifying the following line to include our custom field(s):

<str name="qf">
  body^40.0 title^5.0 name^3.0 taxonomy_names^2.0 tags_h1^5.0 tags_h2_h3^3.0 tags_h4_h5_h6^2.0 tags_inline^1.0
</str>

I opted to make the changes in my custom module as it is already in version control.

pwolanin’s picture

Thanks for the writeup.

The only change I might suggest is that you won't need to add the standard qf fields if you submit the admin page at ?q=admin/settings/apachesolr/query-fields. By hard coding all the default values, I'm not sure what will happen if you change the settings on that page.

Or, the most robust option might be to add in the defaults as above only if $params['qf'] is empty.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

tjmoyer’s picture

For those of you, like me, who come across this old post trying to figure out how to add a field (perhaps a hidden one), or how to set exactly which fields are exposed to search, you should try adding a Search index display to the content type. See this post post on Exposing Fields to Solr for some more info.

autopoietic’s picture

Issue summary: View changes

For those of you, like me, that came across this old post (even later) looking for advice on how to add custom fields to solr, this is an excellent article:

https://www.deeson.co.uk/labs/part-1-apache-solr-creating-custom-fields