Planet Drupal

Syndicate content
Drupal.org - aggregated feeds in category Planet Drupal
Updated: 10 min 3 sec ago

Deeson Online: Part 1: Apache Solr - Creating Custom Fields

Fri, 08/29/2014 - 10:04
div class=field field-name-body field-type-text-with-summary field-label-hiddendiv class=field-itemsdiv class=field-item even property=content:encodedpThis is the first of two blog posts. In this one I will show you how to create a custom search index in Apache Solr. Part 2 will go into how you can then manually set the field bias of your custom field so that you can control it through the settings with the Apache Solr module./p h2Creating a custom field/h2 pAdding custom fields to Apache Solr is often something that you can end up needing to do for a project. The Apache solr module makes this easy to do with: hook_apachesolr_index_document_build()./p pspan class=geshifiltercode class=php geshifilter-phpspan style=color: #009933; font-style: italic;/** * Implements hook_apachesolr_index_document_build(). *//span span style=color: #000000; font-weight: bold;function/span MY_MODULE_apachesolr_index_document_buildspan style=color: #009900;#40;/spanApacheSolrDocument span style=color: #000088;$document/spanspan style=color: #339933;,/span span style=color: #000088;$entity/spanspan style=color: #009900;#41;/span span style=color: #009900;#123;/span span style=color: #000088;$document/spanspan style=color: #339933;-amp;/spangtspan style=color: #339933;;/spanaddFieldspan style=color: #009900;#40;/spanspan style=color: #339933;amp;/spanspan style=color: #666666; font-style: italic;#39;ss_my_fieldamp;#39;, amp;#39;amp;#39;); } /span/code/span/p pWhen defining the field you will notice that this is prefixed with strong'ss_'/strong which is very important as it tells Apache Solr what type of field it is./p pThis prefix can be two or three characters long, with the first character defining the data type of field (e.g. string (s), boolean (b), date (d) etc.) and the last character defines if it is a single (s) or multi-valued (m) field./p pIf you have a look at the emschema.xml/em file that comes with the ApacheSolr module you will see a section that details the standard prefixes for field indexes. Here is a snippet from the file:/p pspan class=geshifiltercode class=php geshifilter-phpspan style=color: #339933;lt;!--/span We span style=color: #000000; font-weight: bold;use/span long span style=color: #b1b100;for/span integer since span style=color: #cc66cc;64/span bit ints are now common in PHPspan style=color: #339933;./span span style=color: #339933;--gt;lt;/spandynamicfield indexedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span multivaluedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;falsequot;/span namespan style=color: #339933;=/spanspan style=color: #0000ff;quot;is_*quot;/span storedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span typespan style=color: #339933;=/spanspan style=color: #0000ff;quot;longquot;/spanspan style=color: #339933;gt;/span span style=color: #339933;lt;/spandynamicfield indexedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span multivaluedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span namespan style=color: #339933;=/spanspan style=color: #0000ff;quot;im_*quot;/span storedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span typespan style=color: #339933;=/spanspan style=color: #0000ff;quot;longquot;/spanspan style=color: #339933;gt;/span span style=color: #339933;lt;!--/span a href=http://www.php.net/listspan style=color: #990000;List/span/a of floats can be saved in a regular float field span style=color: #339933;--gt;lt;/spandynamicfield indexedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span multivaluedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;falsequot;/span namespan style=color: #339933;=/spanspan style=color: #0000ff;quot;fs_*quot;/span storedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span typespan style=color: #339933;=/spanspan style=color: #0000ff;quot;floatquot;/spanspan style=color: #339933;gt;/span span style=color: #339933;lt;/spandynamicfield indexedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span multivaluedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span namespan style=color: #339933;=/spanspan style=color: #0000ff;quot;fm_*quot;/span storedspan style=color: #339933;=/spanspan style=color: #0000ff;quot;truequot;/span typespan style=color: #339933;=/spanspan style=color: #0000ff;quot;floatquot;/spanspan style=color: #339933;gt;/span span style=color: #339933;lt;//spandynamicfieldspan style=color: #339933;gt;lt;//spandynamicfieldspan style=color: #339933;gt;lt;//spandynamicfieldspan style=color: #339933;gt;lt;//spandynamicfieldspan style=color: #339933;gt;/span/code/span/p pHaving defined your new index you will need to tell Apache Solr about it. To do this all you have to do is do a full re-index of your content which will register your custom field with Solr. You can check that your field has been index correctly by checking the Solrs search index report - /admin/reports/apachesolr./p pHaving now indexed your new field you can now alter the query to make sure of this new field using hook_apachesolr_query_alter()./p pspan class=geshifiltercode class=php geshifilter-phpspan style=color: #009933; font-style: italic;/** * Implements hook_apachesolr_query_alter(). *//span span style=color: #000000; font-weight: bold;function/span MY_MODULE_apachesolr_query_alterspan style=color: #009900;#40;/spanDrupalSolrQueryInterface span style=color: #000088;$query/spanspan style=color: #009900;#41;/span span style=color: #009900;#123;/span span style=color: #000088;$query/spanspan style=color: #339933;-amp;/spangtspan style=color: #339933;;/spanaddParamspan style=color: #009900;#40;/spanspan style=color: #339933;amp;/spanspan style=color: #666666; font-style: italic;#39;sortamp;#39;, amp;#39;ss_my_field ascamp;#39;); } /span/code/span/p pYou will now see that this is changing the results of your search based upon you new field./p pNow you've created your customer field, my next post will show you how you can define it so that you can manually set the field bias within the Apache Solr admin section when a search is performed./p pInterested to get feedback on part one though - so share your comments below!/p /div/div/divdiv class=field field-name-node-link field-type-ds field-label-hiddendiv class=field-itemsdiv class=field-item evena href=/labs/part-1-apache-solr-creating-custom-fieldsRead more/a/div/div/divdiv class=field field-name-title field-type-ds field-label-hiddendiv class=field-itemsdiv class=field-item even property=dc:titleh2Part 1: Apache Solr - Creating Custom Fields/h2/div/div/divdiv class=field field-name-meta field-type-ds field-label-hiddendiv class=field-itemsdiv class=field-item evenBy a href=/category/author/mike-davisMike Davis/a | 29th August 2014/div/div/div