solr-4.4与php通信,结果高亮显示

solr-4.4与php通信的方法是以url的形式,发送符合solr规则的url,用file_get_contents()和curl()获取内容


add()

$data_string = json_encode(array('id'=>8888, 'title' => '测试成功'));

$url = http://127.0.0.1:8080/solr/core0/update?commit=true

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  // 更新需要post提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$solrData = curl_exec($ch);


$data_string = json_encode(Array ( [delete] => Array ( [id] => 8888 ) ));

$url = http://127.0.0.1:8080/solr/core0/update?commit=true

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  // 更新需要post提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$solrData = curl_exec($ch);


$data_string = json_encode(array('id' => 8888,'title'=>array('set'=>'测试2')));

$url = http://127.0.0.1:8080/solr/core0/update?commit=true

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  // 更新需要post提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$solrData = curl_exec($ch);




$data_string = Array ( 

[q] => %E8%8B%B1%E4%BC%A6 ,

[start] => 0 ,

[rows] => 25 ,

[wt] => json ,

[fl] => title 

)

$url = http://127.0.0.1:8080/solr/core0/select?wt=json&q=%E8%8B%B1%E4%BC%A6&start=0&rows=25&fl=title

$result = file_get_contents($url);


以上就是基本的四种操作,如果要达到先分词再查询的效果,就得先获取关键字分词的结果,

再根据获得的分词利用q=’a' or q=‘b’ 的方式查询结果。


分词接口调用

$q = '中华人民共和国';

$url = http://127.0.0.1:8080/solr/core0/analysis/field?wt=json&q=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD&analysis.fieldtype=text_zh

$result = file_get_contents($url);



查询结果高亮显示:

在SolrHome/core0/conf/solrconfig.xml文件中添加配置:

<!-- 高亮显示 -->
  <requestHandler name="search" class="solr.SearchHandler" default="true">  
   <!-- default values for query parameters can be specified, these  
        will be overridden by parameters in the request  
     -->  
    <lst name="defaults">  
      <str name="echoParams">explicit</str>  
      <int name="rows">10</int>  
      <str name="df">title</str>  
   <str name="hl">true</str>  
   <str name="hl.fl">title</str>  
   <str name="f.name.hl.fragsize">50</str>  
   <str name="hl.simple.pre">&lt;font color=red&gt;</str>  
   <str name="hl.simple.post">&lt;/font&gt;</str>  
    </lst>  
 </requestHandler>

<str name="df">title</str>  代表默认高亮的字段

<str name="hl.fl">title</str>  高亮显示字段

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昂的数字之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值