2016-10-04 73 views
6

我正在使用apache solr搜索引擎來實現我的網站搜索。我能夠設置Apache Solr Search模塊,現在我的搜索正在按需要工作。現在我試圖在Apache Solr Autocomplete模塊的幫助下實現搜索自動完成,但事情是我試圖將節點標題顯示爲建議列表而不是關鍵字。最後,我發現這tutorial,並試圖說,我似乎正在工作,但現在我陷入了一個AJAX HTTP錯誤(500)。而且我發現4的錯誤訊息,我的Drupal的錯誤日誌: -Apache Solr搜索自動完成

Notice: Undefined index: facet.prefix in apachesolr_autocomplete_suggest() (line 461 of /home/test/webroot/sites/all/modules/apachesolr_autocomplete/apachesolr_autocomplete.module). 

Warning: Invalid argument supplied for foreach() in apachesolr_autocomplete_suggest() (line 470 of /home/test/webroot/sites/all/modules/apachesolr_autocomplete/apachesolr_autocomplete.module). 

Notice: Trying to get property of non-object in apachesolr_autocomplete_suggest() (line 470 of /home/test/webroot/sites/all/modules/apachesolr_autocomplete/apachesolr_autocomplete.module). 

Notice: Undefined property: stdClass::$payslip in apachesolr_autocomplete_suggest() (line 469 of /home/test/webroot/sites/all/modules/apachesolr_autocomplete/apachesolr_autocomplete.module). 

我猜這個教程幾乎做我們找工作,不幸的是他們不再提供支持或評論任何迴應。有人能夠找出一種方法來實現這個很酷的功能嗎?即使有人提出許可證版本,我也很樂意購買。多謝你們。

這是當前Apache Solr Autocomplete的樣子(下圖),在這個建議列表中他們列出了來自搜索索引的搜索關鍵字。我想要做的是在此建議列表中列出節點標題。

enter image description here

+0

您正在使用什麼版本的模塊?你編輯模塊了嗎?在最後一個版本中,第461行對應一個變量賦值($ context)。 – EricLavault

+0

我使用的是「Apache Solr Search 7.x-1.8,Apache Solr Autocomplete 7.x-1.6,solr-4.5.1」。根據教程,我用自己定製的函數替換'function apachesolr_autocomplete_suggest()'。 – nanbatman

+0

我剛剛注意到有這個醜陋的覆蓋,我不會推薦這個教程。此外,由於* label_autocomplete *字段爲'stored = true',因此字段的實際值可以通過查詢檢索,因此作者應該堅持使用小寫過濾器,而不是將保留的小寫標題鏈接到字段中。 – EricLavault

回答

5

這只是一招,沒有不正確的方法,但你可以試試。

首先,您應該捕獲Solr查詢,該查詢在您搜索時執行。

我使用solr級別模塊來捕獲查詢。然後用鉤子創建你自己的模塊。

/** 
* Implements hook_menu(). 
*/ 

function mymodule_menu() { 
    $items = array(); 

    //create a call back for autocomplete search box 
    $items['searchauto/autocomplete'] = array(
    'page callback' => 'mymodule_search_autocomplete', 
    //'access arguments' => array('access search autocomplete'), 
    'type' => MENU_CALLBACK, 
    'access arguments' => array('access content'), 
); 

    return $items; 
} 
/** 

* hook_form_FORM_ID_alter 
* solr search form text box autocomplete 
**/ 
function mymodule_form_search_block_form_alter(&$form, &$form_state, $form_id) { 

    $form['search_block_form'] = array(
    '#type' => 'textfield', 
    '#autocomplete_path' => 'searchauto/autocomplete', 
); 
} 
/** 
a* call back function for autocomplete 
**/ 
function mymodule_search_autocomplete($string) { 

    unset($results); 
    $results = array(); 
    $matches = array(); 

    //replace the space with %20 
    $key = preg_replace('/[[:space:]]+/', '%20', $string); 
    //number of results you want to show 
    $num_result = 15; 
    //your Solr server path 
    $solr_server = "http://locathost/solr"; 

//this is the trick. first you should capture the Solr query, which executed when you hit search. I use the Solr level module to catch the query. you can change parameters if you want, I'm only changing the search keyword and number of results 
$request_url = $solr_server."/select?start=0&rows=$num_result&&spellcheck=true&q=$key&fl=id%2Centity_id%2Centity_type%2Cbundle%2Cbundle_name%2Clabel%2Css_language%2Cis_comment_count%2Cds_created%2Cds_changed%2Cscore%2Cpath%2Curl%2Cis_uid%2Ctos_name%2Czm_parent_entity%2Css_filemime%2Css_file_entity_title%2Css_file_entity_url&mm=1&pf=content%5E2.0&ps=15&hl=true&hl.fl=content&hl.snippets=3&hl.mergeContigious=true&f.content.hl.alternateField=teaser&f.content.hl.maxAlternateFieldLength=256&spellcheck.q=$key&qf=content%5E40&qf=label%5E21.0&qf=tags_h1%5E3.0&qf=tags_h2_h3%5E3.0&qf=tags_inline%5E1.0&qf=taxonomy_names%5E2.0&qf=tos_name%5E3.0&facet=true&facet.sort=count&facet.mincount=1&facet.field=im_field_taxonomy_app_cat&f.im_field_taxonomy_app_cat.facet.limit=50&f.im_field_taxonomy_app_cat.facet.mincount=1&boost=eff_popularity&debugQuery=on&wt=json&json.nl=map"; 
//exit; 
// Retrieve data from the external API 
$response = drupal_http_request($request_url); 

// Check the HTTP response code to see if a valid response was received 
if($response->code >= 200 && $response->code < 300) 
{ 

    //make sure response has values 
    if(isset($response)){ 
     $results = (array) json_decode($response->data); 
    } 
    if(isset($results)){ 
     //dsm($results); 
     //store the values into an array 
     if(isset($results['response']->docs)){ 
     $arrResults = $results['response']->docs; 
     } 
    } 

    //check array count 
    if(count($arrResults) > 0){ 
     //loop the results and add to array for json return data 
     foreach($arrResults as $row){ 
      //dsm($row); 
      //print $row->label; 
      //print "<br>"; 
      $matches[$row->url] = $row->label; 
     } 

    } 
    else{ 
     $matches[''] = "No Results Found!"; 
    } 
} 
else{ 
    $matches[''] = "Check server settings!"; 
} 

drupal_json_output($matches); 

}