A talk on how to modify a default install of Drupal's Apache Solr integration module to provide a customised search experience.
http://2014.drupalcamplondon.co.uk/drupalcamp-london-2014/session/apache-solr-beyond-search-page
2. Who are you again?
I work for Aroq - an online publisher.
We provide daily information in 4 industries: Auto,
Food, Drink, Clothing.
Auto is where I spend most of my time, working on
QUBE: a site for research and intelligence, rather than
news. QUBE uses Solr extensively, with ~ 65000
documents in its index.
3. What’s this about then?
How to use Solr for things that aren’t just the bog
standard search page:
1. Content admin page
2. Data visualisation
You can use these techniques to enhance normal
search pages too.
13. Dynamic Fields
So any field you’ve added through Field API can be shown in solr
without changing the schema, dynamic fields are used. These have
their properties defined by their prefix.
EG:
•
im_foo => integer, multi-valued. EG Taxonomy term IDs.
•
sm_bar => string, multi-valued. EG Taxonomy term names.
•
bs_grill => boolean, single. EG Checkbox fields.
Most fields are added for you, but you can always add your own. In
fact, you’ll probably have to.
15. Adding extra data.
hook_apachesolr_index_documents_alter(array &$documents,
$entity, $entity_type, $env_id) {
$document = reset($documents);
$document->setField('ss_foo', $entity->field_foo…);
}
Most field data is included for you already (with a few exceptions). Data you add
here can be anything you like though.
17. Modifying queries.
Add extra fields to the results:
hook_apachesolr_query_alter($query) {
$query->addParam('fl', ‘sm_foo’);
}
Changing the query fields:
hook_apachesolr_query_alter($query) {
$query->replaceParam('qf', array(
‘label^2.0',
'content^1.0',
));
}
18. Modifying queries.
Changing the number of results to return:
hook_apachesolr_query_alter($query) {
// Solr doesn't have a value for “unlimited"
// so we'll pass in a Very Large Number.
$query->addParam('rows', 999999);
}
Changing the sort order (setting a default in this case):
hook_apachesolr_query_alter($query) {
if (!isset($_GET['solrsort']) && ($query->getParam('q') == '')) {
$query->setSolrSort('ds_changed', 'desc');
}
}
19. Adding sorts
Override SolrBaseQuery. apachesolr_query_class variable controls which is instantiated.
class MySolrBaseQuery extends SolrBaseQuery {
protected function defaultSorts() {
$sorts = parent::defaultSorts();
// Add in core changed property. Missing by default.
$sorts['ds_changed'] =
array('title' => t('Updated Date'), 'default' => 'desc');
}
}
apachesolr_sort module can be used to manage sorts:
•
Choose a default sort & order.
•
Enable / Disable the available sorts.
20. Bringing it all together.
Create a search page.
Apply the limits you need. Eg entity type, bundles, etc.
Add the data you need to solr documents.
Alter the query to add any additional fields you want to
display.
Place any facet blocks you want.
Theme it.
23. Swopping out theme functions.
function hook_apachesolr_search_page_alter(&$build, $search_page) {
if ($search_page['page_id'] == 'pldb') {
// If a timeseries view was requested, switch the theme function to one we define that
draws timeseries.
if (qube_pldb_search_results_view() == 'timeseries') {
$env_id = 'solr';
if (apachesolr_has_searched($env_id)) {
$query = apachesolr_current_query($env_id);
if (qube_pldb_search_results_view_timeseries_allowed($query)) {
$build['search_results']['#theme'] = 'search_results_timeseries';
}
….