function solrmlt_suggestions($block_id, $nid)
{
    try {
        $solr = apachesolr_get_solr();
        $fields = array('mlt.mintf', 'mlt.mindf', 'mlt.minwl', 'mlt.maxwl', 'mlt.maxqt', 'mlt.boost', 'mlt.qf');
        $block = apachesolr_mlt_load_block($block_id);
        $params = array('qt' => 'mlt', 'fl' => 'nid,title,url', 'mlt.fl' => implode(',', $block['mlt_fl']));
        foreach ($fields as $field) {
            $drupal_fieldname = str_replace('.', '_', $field);
            if (!empty($block[$drupal_fieldname])) {
                $params[$field] = check_plain($block[$drupal_fieldname]);
            }
        }
        $query = apachesolr_drupal_query('id:' . apachesolr_document_id($nid));
        // This hook allows modules to modify the query and params objects.
        apachesolr_modify_query($query, $params, 'apachesolr_mlt');
        if (empty($query)) {
            return;
        }
        $response = $solr->search($query->get_query_basic(), 0, $block['num_results'], $params);
        if ($response->response) {
            $docs = (array) end($response->response);
        }
        return $docs;
    } catch (Exception $e) {
        watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
    }
}
Example #2
0
 /**
  * Performs a search against the Solr index.
  */
 public static function index($query = '*:*', $page = 0, $fields = '', $params = array(), $sparams = array())
 {
     $res = array('items' => array(), 'facets' => array());
     $rows = isset($params['rows']) ? $params['rows'] : 20;
     if ($rows > 200) {
         $rows = 200;
     }
     $sparams = $sparams + array('start' => $page * $rows, 'rows' => $rows, 'qt' => 'standard');
     $rows = $sparams['rows'];
     // Get field list
     if (empty($fields)) {
         $fields = array('nid', 'type', 'title');
     } else {
         if (is_string($fields)) {
             $fields = split(',', $fields);
         }
     }
     $sparams['fl'] = join($fields, ',');
     // Get facet params
     $facet_queries = array();
     $facets = array();
     if (isset($params['facets'])) {
         $facets = split(',', $params['facets']);
         $sparams['facet'] = 'true';
         $sparams['facet.sort'] = 'true';
         $sparams['facet.limit'] = !empty($params['facet_limit']) ? intval($params['facet_limit']) : 20;
         $sparams['facet.mincount'] = 1;
         $sparams['facet.field'] = $facets;
         if (isset($params['facet_queries'])) {
             $facet_queries = split(',', $params['facet_queries']);
             $sparams['facet.query'] = $facet_queries;
         }
     }
     // Validate sort parameter
     if (isset($params['sort']) && preg_match('/^([a-z0-9_]+ (asc|desc)(,)?)+$/i', $params['sort'])) {
         $sparams['sort'] = $params['sort'];
     }
     $solr = apachesolr_get_solr();
     $response = $solr->search($query, $sparams['start'], $sparams['rows'], $sparams, 'POST');
     $res['total'] = $response->response->numFound;
     $res['page_size'] = $sparams['rows'];
     if ($res['total']) {
         foreach ($response->response->docs as $doc) {
             $item = array();
             foreach ($doc->getFieldNames() as $field) {
                 $item[$field] = $doc->getField($field);
                 $item[$field] = $item[$field]['value'];
             }
             $res['items'][] = $item;
         }
     }
     if (isset($response->facet_counts->facet_fields)) {
         foreach ($response->facet_counts->facet_fields as $facet_field => $counts) {
             if (!empty($counts)) {
                 $res['facets'][$facet_field] = get_object_vars($counts);
                 switch ($facet_field) {
                     case 'tid':
                         self::getTermInfo($res['facets'][$facet_field]);
                         break;
                     case 'im_simple_geo_area':
                         self::getAreaInfo($res['facets'][$facet_field]);
                         break;
                         // TODO: Maybe the facet describing process should be extensible?
                 }
             }
         }
     }
     if (isset($response->facet_counts->facet_queries)) {
         foreach ($response->facet_counts->facet_queries as $query => $counts) {
             $res['query_facets'][$query] = $counts;
         }
     }
     return $res;
 }
/**  ?? _solrToExcel_QuerySolrServer()
 *  This function will look at $_GET (and $_POST data (via $_REQUEST)) and build the nessesary
 *  Solr-query to hit the Solr-search-server with, and return Solr's responce.
 */
function _solrToExcel_QuerySolrServer()
{
    $solr = apachesolr_get_solr();
    $solrsort = '';
    $params = unserialize($_POST['params_serialized']);
    $selectedOption = $_POST['dc'];
    if ($selectedOption == 'range') {
        $pageFrom = intval($_POST['rangefrom']);
        $pageTo = intval($_POST['rangeto']);
        $params["start"] = 10 * ($pageFrom - 1);
        $params["rows"] = 10 * ($pageTo - $pageFrom + 1);
    } else {
        if (strpos($selectedOption, '_')) {
            $pageNumber = intval(str_replace('current_', '', $selectedOption));
            $params["start"] = $pageNumber >= 1 ? $pageNumber * 10 : 0;
            $params["rows"] = 10;
        } else {
            $params["start"] = 0;
            $params["rows"] = intval($selectedOption);
        }
    }
    $query1 = apachesolr_drupal_query('apachesolr', $params);
    apachesolr_search_add_boost_params($query1);
    $response = $query1->search();
    return $response;
}
<?php

/**
 * Search API allows users to query for a page of results.
 *
 */
$solr = apachesolr_get_solr();
$solrsort = '';
$params = array();
$params['q'] = $_REQUEST['query'];
$params['fl'] = "entity_type,bundle,label,ds_created,path,url,teaser";
// Some solr defaults
$params['mm'] = array(1);
$params['pf'] = array('content^2.0');
$params['ps'] = array(15);
$params['hl'] = TRUE;
$params['hl.fl'] = 'content';
$params['hl.snippets'] = 3;
$params['hl.mergeContigious'] = array(TRUE);
$params['f.content.hl.alternateField'] = array('teaser');
$params['f.content.hl.maxAlternateFieldLength'] = array(256);
$params['qf'] = array('content^40', 'label^5.0', 'tags_h1^5.0', 'tags_h2_h3^3.0', 'tags_h4_h5_h6^2.0', 'tags_inline^1.0', 'taxonomy_names^2.0', 'tos_name^3.0');
$params['facet'] = TRUE;
$params['facet.sort'] = array('count');
$params['facet.mincount'] = 1;
$params['facet.field'] = array('{!ex=bundle}bundle');
$params['f.bundle.facet.limit'] = array(50);
$params['f.bundle.facet.mincount'] = array(1);
$params["start"] = 0;
$params["rows"] = 10;
$query = apachesolr_drupal_query('apachesolr', $params);
Example #5
0
 /**
  * Implements Drupal_SolrDevel_Adapter::analyzeQuery().
  */
 public function analyzeQuery($keys, $page_id, $entity_id, $entity_type)
 {
     $search_page = apachesolr_search_page_load($page_id);
     $conditions = apachesolr_search_conditions_default($search_page);
     $solr = apachesolr_get_solr($search_page->env_id);
     // Sets default parameters.
     $params = array('q' => $keys, 'fq' => isset($conditions['fq']) ? $conditions['fq'] : array(), 'rows' => 1);
     $params['fq'][] = 'id:' . apachesolr_document_id($entity_id, $entity_type);
     $results = apachesolr_search_run('apachesolr', $params, '', '', 0, $solr);
     return isset($results[0]) ? $results[0] : array();
 }
Example #6
0
 /**
  * Create a new Solr API object.
  *
  * Typically, this is invoked transparently by <code>solrq()</code>.
  * 
  * By default, this sets the following:
  * - highlighting is turned off
  * - faceting is turned off
  * - spellcheck is turned off
  * - the query parser is set to 'lucene'
  * - the list of returned fields is set to 'id,nid,title,comment_count,type,created,changed,score,path,url,uid,name'
  *
  * @param $query
  *  A query string or a Drupal_Solr_Query_Interface object.
  * @param $solr
  *  A solr server object. If not provided, one will be fetched.
  * @see query()
  * @see solrq()
  * @see solr()
  * @see apachesolr_get_solr()
  */
 public function __construct($query = '', Apache_Solr_Service $solr = NULL)
 {
     $this->solr = is_object($solr) ? $solr : apachesolr_get_solr();
     if ($query instanceof Drupal_Solr_Query_Interface) {
         $this->extractDrupalSolrInfo($query);
     } else {
         $this->query = $query;
     }
     // Set default params.
     $this->params = array('fl' => 'id,nid,title,comment_count,type,created,changed,score,path,url,uid,name', 'hl' => 'false', 'spellcheck' => 'false', 'facet' => 'false', 'mlt' => 'false', 'defType' => 'lucene');
     // We don't do this anymore, since the default type is now Lucene, and
     // Lucene does not use default query fields.
     // Set default query fields:
     //$this->defaultQueryFields();
 }