Apache Solr搜索自动完成

编程入门 行业动态 更新时间:2024-10-26 00:28:37
本文介绍了Apache Solr搜索自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用apache solr搜索引擎实现我的网站搜索。我可以设置

解决方案

这只是一个伎俩,不是不正确的方式,但可以试试。

首先,您应该捕获当您搜索时执行的Solr查询。

我使用solr级别模块来捕获查询。然后用钩子创建自己的模块。

/ ** *实现hook_menu()。 * / 函数mymodule_menu(){ $ items = array(); //为自动填充搜索框创建一个回调 $ 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搜索表单文本框自动填充 ** / 函数mymodule_form_search_block_form_alter(& $表单,& $ form_state,$ form_id){ $ form ['search_block_form'] = array('#type'=>'textfield',' autocomplete_path'=>'searchauto / autocomplete',); } / ** a *自动完成的回调函数 ** / 函数mymodule_search_autocomplete($ string){ unset $结果); $ results = array(); $ matches = array(); //用%20 $ key = preg_replace('/ [[:space:]] + /','%20',$ string)替换空格; //要显示的结果数 $ num_result = 15; //您的Solr服务器路径 $ solr_server =http:// locathost / solr; //这是窍门。首先,您应该捕获当您搜索时执行的Solr查询。我使用Solr级别模块来捕获查询。你可以根据需要更改参数,我只是更改搜索关键字和结果数 $ request_url = $ solr_server。/ select?start = 0& rows = $ num_result&& spellcheck = true& q = $密钥安培; 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&安培;毫米= 1&安培; PF =含量%5E2.0&安培; PS = 15&安培; HL =真安培; hl.fl =含量&安培; hl.snippets = 3及hl.mergeContigious =真安培; f.content.hl.alternateField =挑逗&安培; f.content.hl.maxAlternateFieldLength = 256安培; spellcheck.q = $键和放大器; QF =含量%5E40和放大器; QF =标签%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.sort =计数&安培; facet.mincount = 1&安培; facet.field = im_field_taxonomy_app_cat&安培; f.im_field_taxonomy_app_cat.facet.limit = 50&安培; f.im_field_taxono my_app_cat.facet.mincount = 1&安培;升压= eff_popularity&安培; debugQuery = ON&安培;重量= JSON&安培; json.nl =地图; // exit; //从外部API检索数据 $ response = drupal_http_request($ request_url); //检查HTTP响应代码以查看是否收到有效的响应 if($ response-> code> = 200&& $ response-> code< ; $) { //确保响应有值 if(isset($ response)){ $ results =(array)json_decode($ response - >数据); } if(isset($ results)){ // dsm($ results); //将值存储到数组 if(isset($ results ['response'] - > docs)){ $ arrResults = $ results ['response'] - > ;文档; } } //检查数组数 if(count($ arrResults)> 0){ //循环结果并添加数组json返回数据 foreach($ arrResults as $ row){ // dsm($ row); // print $ row-> label; // print< br>; $ matches [$ row-> url] = $ row-> label; } } else { $ matches [''] =找不到结果! } } else { $ matches [''] =检查服务器设置! } drupal_json_output($ matches);

}

I'm using apache solr search engine to implement my site search. I able to setup the Apache Solr Search module and now my search is working as I need. Now I trying to implement an search autocomplete with the help of Apache Solr Autocomplete module, but the thing is I'm trying to show the node titles as suggestion list rather than keywords. Finally I found this tutorial and tried that, I seems to be working but now I'm stucked with an AJAX HTTP error (500). And I found 4 error messages in my drupal error log:-

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

I guess this tutorial almost doing the job we looking for, unfortunately they no longer providing support or any response to the comments. Is anyone able to figure out a way to implement this cool functionality? Even if anyone come up with a licence version, I'm happy to buy. Thanks guys.

This is the current Apache Solr Autocomplete look like (below image), In this suggestion list they listing the search keywords from the search index. What I'm trying to do is listing the node titles in this suggestion list.

解决方案

This is just a trick, not the not the correct way, but you can try.

First you should capture the Solr query, which executed when you hit search.

I use the solr level module to catch the query. Then create your own module with hooks.

/** * 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 = "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);

}

更多推荐

Apache Solr搜索自动完成

本文发布于:2023-11-30 00:33:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动完成   Apache   Solr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!