123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- class Solr{
- /**
- * Solr资源串
- * @var string
- */
- public $dsn = 'http://59.110.214.36:8983/solr/';
- /**
- * Solr集合
- * @var string
- */
- public $core = 'food_list';
- /**
- * 设置集合
- * @param string $core 集合
- * @return Solr
- */
- function setCore($core)
- {
- $this->core = $core;
- return $this;
- }
- /**
- * 获取集合
- * @return string
- */
- function getCore()
- {
- return $this->core;
- }
- /**
- * Solr 搜索
- * @param string $action 方法
- * @param array $data 数据
- * @return array
- */
- function post($action, $data = array(), $redo = 5)
- {
- if (strpos($action, '?')) {
- $action .= '&wt=json';
- $data = $data ? json_encode($data, JSON_UNESCAPED_UNICODE) : null;
- } elseif ($data) {
- $action .= '?wt=json&' . http_build_query($data, null, '&');
- $data = null;
- }
- $url = $this->dsn . $this->core . '/' . $action;
- $ch = curl_init($url);
- if ($data) {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data))
- );
- $rs = curl_exec($ch);
- $rs = json_decode($rs, true);
- if (empty($rs) == false) {
- if (isset($rs['responseHeader']['status']) && 0 == $rs['responseHeader']['status']) {
- $rs = array('code' => 1, 'info' => '操作成功', 'data' => $rs);
- } else {
- $rs = array('code' => 0, 'info' => '操作失败', 'error' => $rs);
- }
- } else {
- if (0 < $redo) {
- $args = func_get_args();
- --$args[2];
- sleep(1);
- if ('cli' == PHP_SAPI && false !== strpos($action, 'update')) {
- echo "sleep\n";
- }
- return $this->post($args[0], $args[1], $args[2]);
- }
- $rs = array('code' => 0, 'info' => '网络错误,服务器繁忙');
- }
- if (('cli' == PHP_SAPI && false !== strpos($action, 'update')) || (isset($_GET['debug']) && 2 == $_GET['debug'])) {
- var_dump($url, $action, $data, $rs);
- }
- return $rs;
- }
- /**
- * 搜索数据
- * @param array $data
- * array(
- * 'fl' => '*,title', // 表示索引显示那些 field(*表示所有 field, score 是 solr 的一个匹配热度)
- * 'q.op' => 'AND', // 表示 q 中 查询语句的 各条件的逻辑操作 AND(与) OR(或)
- * 'start' => '0', // 开始返回条数
- * 'rows' => '16', // 返回多少条
- * 'hl' => 'true', // 是否高亮
- * 'hl.fl' => 'title', // 高亮 field
- * 'hl.snippets' => '3', // 不太清楚(反正是设置高亮 3 就可以了)
- * 'hl.simple.pre' => '<span style="color:#ff0000">', // 高亮前面的格式
- * 'hl.simple.post' => '</span>', // 高亮后面的格式
- * 'facet' => 'true', // 是否启动统计
- * 'facet.field' => 'sort', // 统计field
- * 'q' => '白兔', // 查询语句(类似 SQL) 相关详细的操作还需 lucene 的 query 语法
- * 'sort' => 'sort desc', // 排序
- * );
- * @return array
- */
- function select($data)
- {
- if (empty($data['q']) == true) {
- $result = array('code' => 0, 'info' => '关键词不能为空');
- } else {
- $method = 'select';
- $result = $this->post($method, $data);
- }
- return $result;
- }
- /**
- * 分词
- * @param array $data $data['q'] 关键词
- * @return array
- */
- function analysis($data)
- {
- if (empty($data['q']) == true) {
- $result = array('success' => 0, 'info' => '关键词不能为空');
- } else {
- $parame = array();
- $parame['q'] = urlencode($data['q']);
- $method = 'analysis/field';
- $result = $this->post($method, $parame);
- }
- return $result;
- }
- /**
- * 搜索建议
- * @param array $data $data['q'] 关键词
- * @return array
- */
- function suggest($data)
- {
- if (empty($data['q']) == true) {
- $result = array('success' => 0, 'info' => '关键词不能为空');
- } else {
- $parame = array();
- $parame['spellcheck.q'] = urlencode($data['q']);
- $parame['spellcheck'] = 'true';
- $method = 'suggest';
- $result = $this->post($method, $parame);
- }
- return $result;
- }
- /**
- * 相似搜索
- * @param array $data $data['q'] 关键词
- * @return array
- */
- function moreLike($data)
- {
- if (empty($data['q']) == true) {
- $result = array('success' => 0, 'info' => '关键词不能为空');
- } else {
- if (isset($data) == false) {
- $data = array();
- }
- $data['command'] = 'status';
- $method = 'dataimport';
- $result = $this->post($method, $data);
- }
- return $result;
- }
- /**
- * 全量导入索引
- * @param array $data
- * $data['clean'] 可选参数,为true时删除原有索引,false不删除,默认值为true
- * $data['entity'] 可选参数,document下面的标签(data-config.xml),使用这个参数可以有选择的执行一个或多个entity。如果不选择此参数那么所有的都会被运行
- * $data['commit'] 可选参数,选择是否在索引完成之后提交。默认为true
- * $data['optimize'] 可选参数,默认为true
- * $data['debug'] 可选参数,是否以调试模式运行,如果以调试模式运行,那么默认不会自动提交,请加参数“commit=true”
- * @return array
- */
- function fullImport($data = null)
- {
- if (isset($data) == false) {
- $data = array();
- }
- $data['command'] = 'full-import';
- $method = 'dataimport?';
- $result = $this->post($method, $data);
- return $result;
- }
- /**
- * 增量更新索引
- * @param array $data
- * @return array
- */
- function deltaImport($data = null)
- {
- if (!$data) {
- $data = array();
- }
- $data['command'] = 'delta-import';
- $method = 'dataimport';
- $result = $this->post($method, $data);
- return $result;
- }
- /**
- * 查看当前dataimport索引更新状态
- * @param array $data
- * @return array
- */
- function statusImport($data = null)
- {
- if (isset($data) == false) {
- $data = array();
- }
- $data['command'] = 'status';
- $method = 'dataimport';
- $result = $this->post($method, $data);
- return $result;
- }
- /**
- * 终止当前执行的dataimport任务
- * @param array $data
- * @return array
- */
- function abortImport($data = null)
- {
- if (isset($data) == false) {
- $data = array();
- }
- $data['command'] = 'abort';
- $method = 'dataimport';
- $result = $this->post($method, $data);
- return $result;
- }
- /**
- * 更新数据
- * @param array $datas 格式
- * 批量更新$datas=array(array('id'=>xx, 'app_name'=>array('set'=>'测试')))),id为索引主键字段,必须包含主键值
- * 单个更新$datas=array('id'=>xx, 'app_search_text'=>array('add'=>'测试'), 'look_count'=>array('inc'=>10))
- * set 设置或替当前值,null清空当前值
- * add 如果字段属性为multi-valued,添加一个值
- * inc 设置自增加值
- * @return array
- */
- function update($datas)
- {
- if (!$datas) {
- return false;
- }
- if (isset($datas[0]) == false || is_array($datas[0]) == false) {
- $datas = array($datas);
- }
- $method = 'update?commit=true';
- $result = $this->post($method, $datas);
- return $result;
- }
- /**
- * 删除数据
- * @param array $data $data=array('id'=>xx),id为索引主键字段
- * array('query'=>'xx:yy')
- * @return array
- */
- function delete($data)
- {
- if (!$data) {
- return false;
- }
- $data = array('delete' => $data);
- $method = 'update?commit=true';
- $result = $this->post($method, $data);
- return $result;
- }
- /**
- * 清空
- */
- function flush()
- {
- $this->delete(array('query' => '*:*'));
- }
- /**
- * 添加索引(不建议在程序中调用 ),注意:如果主键值相同,则会先删除旧索引,再添加新数据
- * @param array $datas
- * 批量添加$datas=array(array('id'=>xx, 'app_name'=>'测试测试'),id为索引主键字段,必须包含主键值
- * @return array
- */
- function add($datas)
- {
- if (!$datas) {
- return false;
- }
- // print_r($dates);
- if (isset($datas[0]) == false || is_array($datas[0]) == false) {
- $datas = array($datas);
- }
- $method = 'update?commit=true';
- $result = $this->post($method, $datas);
- return $result;
- }
- }
- // $q = $this->_POST("q","");
- // $data = array("fl"=>"id,title,url,tags","q.op"=>"AND","start"=>0,
- // "rows"=>20,"q"=> 'tags:"早餐"');
- // $solr = new Solr();
- // $res = $solr->select($data);
- // print_r($res);
-
|