You are here

function apachesolr_search_mlt_suggestions in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr_search.module \apachesolr_search_mlt_suggestions()
  2. 7 apachesolr_search.module \apachesolr_search_mlt_suggestions()

Performs a moreLikeThis query using the settings and retrieves documents.

Parameters

$settings: An array of settings.

$id: The Solr ID of the document for which you want related content. For a node that is apachesolr_document_id($node->nid)

$solr: The solr environment you want to query against

Return value

An array of response documents, or NULL

2 calls to apachesolr_search_mlt_suggestions()
apachesolr_search_block_view in ./apachesolr_search.module
Implements hook_block_view().
DrupalApacheSolrNodeAccess::testIndexing in apachesolr_access/tests/apachesolr_access.test
Tests indexing and check if it adds the correct grants for those specific users

File

./apachesolr_search.module, line 637
Provides a content search implementation for node content for use with the Apache Solr search application.

Code

function apachesolr_search_mlt_suggestions($settings, $id, $solr = NULL, $context = array()) {
  try {
    $fields = array(
      'mlt_mintf' => 'mlt.mintf',
      'mlt_mindf' => 'mlt.mindf',
      'mlt_minwl' => 'mlt.minwl',
      'mlt_maxwl' => 'mlt.maxwl',
      'mlt_maxqt' => 'mlt.maxqt',
      'mlt_boost' => 'mlt.boost',
      'mlt_qf' => 'mlt.qf',
    );
    $params = array(
      'q' => 'id:' . $id,
      'qt' => 'mlt',
      'fl' => array(
        'entity_id',
        'entity_type',
        'label',
        'path',
        'url',
      ),
      'mlt.fl' => $settings['mlt_fl'],
      'start' => 0,
      'rows' => $settings['num_results'],
    );

    // We can optionally specify a Solr object.
    $query = apachesolr_drupal_query('apachesolr_mlt', $params, '', '', $solr, $context);
    foreach ($fields as $form_key => $name) {
      if (!empty($settings[$form_key])) {
        $query
          ->addParam($name, $settings[$form_key]);
      }
    }
    $type_filters = array();
    if (is_array($settings['mlt_type_filters']) && !empty($settings['mlt_type_filters'])) {
      $query
        ->addFilter('bundle', '(' . implode(' OR ', $settings['mlt_type_filters']) . ') ');
    }
    if ($custom_filters = $settings['mlt_custom_filters']) {

      // @todo - fix the settings form to take a comma-delimited set of filters.
      $query
        ->addFilter('', $custom_filters);
    }

    // This hook allows modules to modify the query object.
    drupal_alter('apachesolr_query', $query);
    if ($query->abort_search) {
      return NULL;
    }
    $response = $query
      ->search();
    if (isset($response->response->docs)) {
      return (array) $response->response->docs;
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
  }
}