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"><font color=red></str>
<str name="hl.simple.post"></font></str>
</lst>
</requestHandler>
<str name="df">title</str> 代表默认高亮的字段
<str name="hl.fl">title</str> 高亮显示字段