Phần 1: Apache Solr - Hướng dẫn Creating Custom Fields

Phần 1: Apache Solr - Hướng dẫn Creating Custom Fields

This 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.

Creating a custom field

Adding 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().

/** 
 * Implements hook_apachesolr_index_document_build(). 
 */ 
function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity) { 
  $document->addField('ss_my_field', ''); 
} 

When defining the field you will notice that this is prefixed with'ss_' which is very important as it tells Apache Solr what type of field it is.

This 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.

If you have a look at the schema.xml 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:

<!-- We use long for integer since 64 bit ints are now common in PHP. -->
<dynamicfield indexed="true" multivalued="false" name="is_*" stored="true" type="long" /> 
<dynamicfield indexed="true" multivalued="true" name="im_*" stored="true" type="long" /> 
<!-- List of floats can be saved in a regular float field -->
<dynamicfield indexed="true" multivalued="false" name="fs_*" stored="true" type="float" /> 
<dynamicfield indexed="true" multivalued="true" name="fm_*" stored="true" type="float" /> 

Having 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.

Having now indexed your new field you can now alter the query to make sure of this new field using hook_apachesolr_query_alter().

/** 
 * Implements hook_apachesolr_query_alter(). 
 */
function MY_MODULE_apachesolr_query_alter(DrupalSolrQueryInterface $query) { 
  $query->addParam('sort', 'ss_my_field asc'); 
} 

You will now see that this is changing the results of your search based upon you new field.

Now 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.

Interested to get feedback on part one though - so share your comments below!

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Hơn 60% ứng dụng trên App Store chưa từng được tải về lần nào

Hơn 60% ứng dụng trên App Store chưa từng được tải về lần nào

Số liệu thống kê từ một bên thứ ba cho biết, có tới hơn 400.000 ứng dụng trên App Store chưa từng được một lần người dùng iOS tải về máy. Theo Phonearena, thống kê trên được đưa ra bởi hãng nghiên cứu Adeven dựa trên số liệu có được từ các ứng dụng iOS

Tên miền .xxx của hàng loạt website lớn nhất Việt Nam đã bị đầu cơ

Tên miền .xxx của hàng loạt website lớn nhất Việt Nam đã bị đầu cơ

Dù có giá cao gấp 10 lần tên miền. com, tên miền "người lớn" của hàng loạt các báo điện tử, doanh nghiệp, trang thương mại điện tử lớn nhất tại Việt Nam

Apple đang lập kế hoạch sản xuất máy tính bảng “lai” laptop

Apple đang lập kế hoạch sản xuất máy tính bảng “lai” laptop

Apple gần đây đã đăng ký một bằng sáng chế về máy tính bảng “lai” với laptop. Máy tính bảng “lai” sẽ được dùng như là cầu nối giữa máy tính bảng và máy tính xách tay trong tương lai.

Công ty diệt chuột T&C

 

Diet con trung