/** * * @param type $word * @return type */ public function letterHotWordsBatch($word) { //获取字母所对应的数据 $letter_list = range('A', 'Z'); //根据随机的记录id获取需要展示的记录数组 $queries = []; $max_page = 100; for ($i = 1; $i <= 26; $i++) { $queries[] = array('word' => $word, 'indexer' => 'index_9939_com_v2_keywords_all', 'offset' => 0, 'size' => $max_page, 'condition' => array(array('filter' => 'filter', 'args' => array('pinyin_initial_sn', array($i))))); } $all_word_ids = SearchHelper::batchSearch($queries); $arr_ids = array(); foreach ($all_word_ids as $k => $ret) { if (!empty($ret['matches'])) { foreach ($ret['matches'] as $kk => $kv) { $arr_ids[] = $kk; } } } $wd_obj = new KeyWords(); $result = $wd_obj->List_ByIds($arr_ids); $return_list = []; if (!empty($result)) { $cache_rand_words = []; //把查找出的所有结果按字母放入数组 foreach ($result as $k => $v) { $caption = $v['pinyin_initial']; if (!isset($cache_rand_words[$caption])) { $cache_rand_words[$caption] = []; } $cache_rand_words[$caption][] = $v; } $len = count($letter_list); $max_dis_length = 28; for ($i = 0; $i < $len; $i++) { $wd = strtoupper($letter_list[$i]); $ret = isset($cache_rand_words[$wd]) ? $cache_rand_words[$wd] : array(); if (count($ret) > 0) { $rand_num = count($ret) > $max_dis_length ? $max_dis_length : count($ret); $rand_keys = array_rand($ret, $rand_num); if (is_array($rand_keys)) { foreach ($rand_keys as $k) { $return_list[$wd][] = $ret[$k]; } } else { $return_list[$wd][] = $ret[0]; } } else { $return_list[$wd] = array(); } } } $result['letter'] = $letter_list; $result['words'] = $return_list; return $result; }
private function getStillFindDatas($diseaseName) { $queries[] = array('word' => $diseaseName, 'indexer' => 'index_9939_com_v2_keywords_all', 'offset' => 0, 'size' => 12, 'condition' => array(array('filter' => 'filter', 'args' => array('typeid', array(99))))); $all_word_ids = SearchHelper::batchSearch($queries); $arr_ids = array(); foreach ($all_word_ids as $k => $ret) { if (!empty($ret['matches'])) { foreach ($ret['matches'] as $kk => $kv) { $arr_ids[] = $kk; } } } $wd_obj = new KeyWords(); $result = $wd_obj->List_ByIds($arr_ids); return $result; }
/** * * @param type $wd * @param type $offset * @param type $size * @param type $explainflag * $conditions = array( array( 'filter'=>'filter_range', 'args'=>array(column_id,array(1)) ) ); * * @return type */ public static function search_disease_symptom_merge($wd, $offset, $size, $explainflag = 0, array $conditions = array(), $explain_ext_config = array()) { $total = 0; $ret_list = array(); $explain_words = array($wd); if (!empty($wd)) { $indexer_name = 'index_9939_com_jb_disease,index_9939_com_jb_symptom'; $ret = SearchHelper::Search($wd, $indexer_name, $offset, $size, $conditions, $explainflag, $explain_ext_config); return self::parse_search_data($ret, $indexer_name); } return array('list' => $ret_list, 'total' => $total, 'explain_words' => $explain_words); }
/** * 疾病首页 下的 文章解读缓存部分 * @author gaoqing * @date 2016-04-15 * @param sting $cacheKey 缓存的唯一标识 * @param array $diseaseid 疾病id * @return array 文章解读数据集 */ public static function readArticleByDisease($cacheKey, $disease, $forceCache = false) { $cache = \Yii::$app->cache_data_file; $cacheFileName = $cacheKey . '_' . $disease['id']; $data = $cache->get($cacheFileName); if ($forceCache) { $data = false; } if (!isset($data) || empty($data)) { $conditions = array(array('word' => $disease['name'], 'indexer' => 'index_9939_com_jb_art', 'offset' => 0, 'size' => 4, 'condition' => array(array('filter' => 'filter', 'args' => array('tmp_type_id', array(2, 7))))), array('word' => $disease['name'], 'indexer' => 'index_9939_com_jb_art', 'offset' => 0, 'size' => 4, 'condition' => array(array('filter' => 'filter', 'args' => array('tmp_type_id', array(1, 3, 4))))), array('word' => $disease['name'], 'indexer' => 'index_9939_com_jb_art', 'offset' => 0, 'size' => 4, 'condition' => array(array('filter' => 'filter', 'args' => array('tmp_type_id', array(5))))), array('word' => $disease['name'], 'indexer' => 'index_9939_com_jb_art', 'offset' => 0, 'size' => 4, 'condition' => array(array('filter' => 'filter', 'args' => array('tmp_type_id', array(6, 8)))))); $diseaseArtids = SearchHelper::batchSearch($conditions); $result = array(); $keys = ['病因是什么', '症状有哪些', '怎么治疗', '如何护理']; foreach ($diseaseArtids as $key => $ret) { $indexer_name = $ret['indexer']; $sphinx_result = Search::parse_search_data($ret, $indexer_name); $ret_list = []; if (!empty($sphinx_result['list'])) { $ret_list = $sphinx_result['list']; } $result[$keys[$key]] = $ret_list; } if (!empty($result)) { $data = $result; $cache->set($cacheFileName, $result, self::DURATION); } } return $data; /* $cache = \Yii::$app->cache_data_file; $cacheFileName = $cacheKey . '_' . $disease['id']; $data = $cache->get($cacheFileName); if ($forceCache){ $data = false; } if (!isset($data) || empty($data)){ $article = new DArticle(); $allReads = []; $moduleMap = [ '病因是什么' => [2, 7], '症状有哪些' => [1, 3, 4], '怎么治疗' => [5], '如何护理' => [6, 8], ]; foreach ($moduleMap as $key => $types){ $preList = $article->getListByDiseaseid(['diseaseid' => $disease['id'], 'type' => $types], 0, 4); $allReads[$key] = $preList; } if (!empty($allReads)){ $data = $allReads; $cache->set($cacheFileName, $allReads); } } return $data; */ }
/** * 首页右侧热门关注 * sphinx调用疾病文章查询 * @return array latestFucus 对应的数据集合 */ public function latestFucus() { $cache = \Yii::$app->cache_file; $cache_key = 'frontend_seek_right_rmgz'; //疾病搜索页面_右侧_热门关注 $data = $cache->get($cache_key); if (!empty($data)) { return $data; } else { $queries = array(array('word' => '高血压', 'indexer' => 'index_9939_com_jb_art'), array('word' => '心脏病', 'indexer' => 'index_9939_com_jb_art'), array('word' => '包皮过长', 'indexer' => 'index_9939_com_jb_art'), array('word' => '乳腺增生', 'indexer' => 'index_9939_com_jb_art'), array('word' => '胃溃疡', 'indexer' => 'index_9939_com_jb_art'), array('word' => '宫颈癌', 'indexer' => 'index_9939_com_jb_art'), array('word' => '癫痫病', 'indexer' => 'index_9939_com_jb_art'), array('word' => '阳痿', 'indexer' => 'index_9939_com_jb_art'), array('word' => '哮喘', 'indexer' => 'index_9939_com_jb_art'), array('word' => '龋齿', 'indexer' => 'index_9939_com_jb_art')); $result = []; $ret = \librarys\helpers\utils\SearchHelper::batchSearch($queries); foreach ($ret as $kk => $ret) { $indexer_name = $ret['indexer']; $sphinx_result = Search::parse_search_data($ret, $indexer_name); $ret_list = $sphinx_result['list']; $kw = $queries[$kk]['word']; $result[$kw] = $ret_list; } $cache->set($cache_key, $result, 24 * 3600); return $result; } }
/** * 首页 疾病资讯 inc_mid_news文件里的数据缓存 * @param string $cacheKey * @param type $parms * @return */ public static function cacheIndexNewsBySphinx($cacheKey, $parms) { $newWords = $parms['newWords']; $art_obj = new \common\models\disease\Article(); $cache = \Yii::$app->cache_data_file; $key = $cacheKey; $data = $cache->get($key); if (!isset($data) || empty($data)) { //1、根据不同的词拼出sphinx批量查询的条件 $conditon = []; $n = 0; foreach ($newWords as $k => $v) { $w = $n; $conditon[$w] = ['word' => $k, 'indexer' => 'index_9939_com_jb_art']; $n++; } // print_r($conditon);exit; //2、根据sphinx批量查询得到记录 $sphinxRecords = SearchHelper::batchSearch($conditon); $result = array(); foreach ($sphinxRecords as $kk => $ret) { $indexer_name = $ret['indexer']; $sphinx_result = Search::parse_search_data($ret, $indexer_name); $ret_list = $sphinx_result['list']; $kw = $conditon[$kk]['word']; $result[$kw] = $ret_list; } // //7、设置缓存 // $cache->set($key, $result); return $result; } else { return $data; } }
function get_scws_words($disease_article) { $keywords_scws = array(); if (empty($keywords_scws) && !empty($disease_article->keywords)) { $keywords_scws = \librarys\helpers\utils\SearchHelper::scws($disease_article->keywords); } if (empty($keywords_scws) && !empty($disease_article->title)) { $keywords_scws = \librarys\helpers\utils\SearchHelper::scws($disease_article->title); } return $keywords_scws; }
/** * 分诊 * @param type $askinfo 问答的信息 */ public function triage($askinfo) { $db_v2sns = self::getDB(); $db_jbv2 = \Yii::$app->db_jbv2; $wd_keshi_ret_default = array('class_level1' => '15', 'class_level2' => '0', 'class_level3' => '0'); $content = $askinfo['content']; $explain_result = SearchHelper::scws($content); $callback = array($this, 'filter'); //获取过滤数据 $explain_result = array_filter($explain_result, $callback); // print_r($explain_result);exit; //如果无法从用户输入的内容里分出有意义的词,返回默认科室 if (!isset($explain_result[0])) { return $wd_keshi_ret_default; } $all = array(); foreach ($explain_result as $k => $v) { $all[$v['attr']][] = $v; } $wd_keshi_ret['class_level3'] = '0'; //默认三级疾病为0 // //Ⅰ //如果有疾病词 if (isset($all['XB'][0])) { //第一个病的词的名称 $disease_name = $all['XB'][0]['word']; //通过疾病名查疾病id $disease_sql = "select id from 9939_disease where name='{$disease_name}'"; $disease_ret = $db_jbv2->createCommand($disease_sql)->queryOne(); $disease_id = $disease_ret['id']; //9725 //先直接查疾病表是否有该疾病 //如果有直接返回查询结果 //如果没有,通过从症状表查 //通过查wd_disease表得到disease_id $disease_name_sql = "select id,name from 9939_disease where id={$disease_id}"; $disease_name_ret = $db_jbv2->createCommand($disease_name_sql)->queryOne(); $disease_name = $disease_name_ret['name']; $class_level3_sql = "select class_level1,class_level2,id from wd_disease where name='{$disease_name}'"; $class_level3 = $db_v2sns->createCommand($class_level3_sql)->queryOne(); if (isset($class_level3['id'])) { $wd_keshi_ret = $class_level3; $wd_keshi_ret['class_level3'] = $class_level3['id']; unset($wd_keshi_ret['id']); return $wd_keshi_ret; } else { //通过疾病id查科室 $disease_condition = 'disease' . $disease_id; $department_sql = "select depart.name from 9939_department depart,9939_depart_rel_merge merge where merge.source_flag=1 and merge.unique_key='{$disease_condition}' and merge.departmentid=depart.id"; $department_ret = $db_jbv2->createCommand($department_sql)->queryOne(); $department_name = $department_ret['name']; //通过科室名称进入wd_keshi查询最终的 $wd_keshi_sql = "select class_level1,class_level2 from wd_keshi where name='{$department_name}'"; $wd_keshi_ret = $db_v2sns->createCommand($wd_keshi_sql)->queryOne(); //如果查不到数据放到默认科室里 if (!isset($wd_keshi_ret['class_level1'])) { $wd_keshi_ret = $wd_keshi_ret_default; } else { if (!$wd_keshi_ret['class_level3']) { $class_level3_sql = "select id from wd_disease where id='{$wd_keshi_ret['class_level3']}'"; $class_level3 = $db_v2sns->createCommand($class_level3_sql)->queryOne(); } } return $wd_keshi_ret; } } //Ⅱ //如果没有疾病词,症状词,只有部位词 if (!isset($all['XZ'][0]) && isset($all['XS'][0])) { //第一个部位的词的名称 $condition_part_name = $all['XS'][0]['word']; //通过部位名查部位id $condition_part_sql = "select id from 9939_part where name='{$condition_part_name}'"; $condition_part_ret = $db_jbv2->createCommand($condition_part_sql)->queryOne(); $condition_part_id = $condition_part_ret['id']; //39 //通过部位查找所有的疾病 $part_disease_sql = "SELECT unique_key FROM 9939_part_rel_merge WHERE source_flag=1 AND partid={$condition_part_id}"; $part_disease_ret = $db_jbv2->createCommand($part_disease_sql)->queryOne(); $disease_id = str_replace('disease', '', $part_disease_ret['unique_key']); //7211 //先直接查疾病表是否有该疾病 //如果有直接返回查询结果 //如果没有,通过从症状表查 //通过查wd_disease表得到disease_id $disease_name_sql = "select id,name from 9939_disease where id={$disease_id}"; $disease_name_ret = $db_jbv2->createCommand($disease_name_sql)->queryOne(); $disease_name = $disease_name_ret['name']; $class_level3_sql = "select class_level1,class_level2,id from wd_disease where name='{$disease_name}'"; $class_level3 = $db_v2sns->createCommand($class_level3_sql)->queryOne(); if (isset($class_level3['id'])) { $wd_keshi_ret = $class_level3; $wd_keshi_ret['class_level3'] = $class_level3['id']; unset($wd_keshi_ret['id']); return $wd_keshi_ret; } else { //通过疾病id查科室 $disease_condition = 'disease' . $disease_id; $department_sql = "select depart.name from 9939_department depart,9939_depart_rel_merge merge where merge.source_flag=1 and merge.unique_key='{$disease_condition}' and merge.departmentid=depart.id"; $department_ret = $db_jbv2->createCommand($department_sql)->queryOne(); $department_name = $department_ret['name']; //通过科室名称进入wd_keshi查询最终的 $wd_keshi_sql = "select class_level1,class_level2 from wd_keshi where name='{$department_name}'"; $wd_keshi_ret = $db_v2sns->createCommand($wd_keshi_sql)->queryOne(); //如果查不到数据放到默认科室里 if (!isset($wd_keshi_ret['class_level1'])) { $wd_keshi_ret = $wd_keshi_ret_default; } return $wd_keshi_ret; } } //Ⅲ //如果没有疾病词,部位词,只有症状词 if (!isset($all['XS'][0]) && isset($all['XZ'][0])) { //症状的词的名称 $symtpom_name = $all['XZ'][0]['word']; //通过症状名查症状id $symtpom_sql = "select id from 9939_symptom where name='{$symtpom_name}'"; $symtpom_ret = $db_v2jb->createCommand($symtpom_sql)->queryOne(); $symtpom_id = $symtpom_ret['id']; //6521 //通过症状id查关联疾病,取第一个病 // $rel_disease_sql = "SELECT diseaseid FROM 9939_disease_symptom_rel WHERE symptomid ={$symtpom_id}"; $rel_disease_sql = "SELECT 9939_disease.id,9939_disease.`name`,9939_disease_symptom_rel.diseaseid FROM 9939_disease LEFT JOIN 9939_disease_symptom_rel ON 9939_disease_symptom_rel.diseaseid=9939_disease.id WHERE symptomid = {$symtpom_id}"; $rel_disease_ret = $db_v2jb->createCommand($rel_disease_sql)->queryOne(); //获取所有症状关联的疾病id $rel_disease_id = $rel_disease_ret['diseaseid']; //取第一个病 $rel_disease_name = $rel_disease_ret['name']; //取第一个病 //先直接查疾病表是否有该疾病 //如果有直接返回查询结果 //如果没有,通过从症状表查 //通过查wd_disease表得到disease_id $class_level3_sql = "select class_level1,class_level2,id from wd_disease where name='{$rel_disease_name}'"; $class_level3 = $db_v2sns->createCommand($class_level3_sql)->queryOne(); if (isset($class_level3['id'])) { $wd_keshi_ret = $class_level3; $wd_keshi_ret['class_level3'] = $class_level3['id']; unset($wd_keshi_ret['id']); return $wd_keshi_ret; } else { //通过疾病id查科室 $disease_condition = 'disease' . $rel_disease_id; $department_sql = "select depart.name from 9939_department depart,9939_depart_rel_merge merge where merge.source_flag=1 and merge.unique_key='{$disease_condition}' and merge.departmentid=depart.id"; $department_ret = $db_v2jb->createCommand($department_sql)->queryOne(); $department_name = $department_ret['name']; //通过科室名称进入wd_keshi查询最终的 $wd_keshi_sql = "select class_level1,class_level2 from wd_keshi where name='{$department_name}'"; $wd_keshi_ret = $db_v2sns->createCommand($wd_keshi_sql)->queryOne(); //如果查不到数据放到默认科室里 if (!isset($wd_keshi_ret['class_level1'])) { $wd_keshi_ret = $wd_keshi_ret_default; } return $wd_keshi_ret; } } //Ⅳ //如果没有疾病词,有症状词和部位词 if (isset($all['XZ'][0]) && isset($all['XS'][0])) { //**************1、根据症状取出所有的疾病************** foreach ($all['XZ'] as $k => $v) { //症状的词的名称 $symtpom_name = $v['word']; //通过症状名查症状id $symtpom_sql = "select id from 9939_symptom where name='{$symtpom_name}'"; $symtpom_ret = $db_v2jb->createCommand($symtpom_sql)->queryOne(); $symtpom_id = $symtpom_ret['id']; //6521 //通过症状id查关联疾病,取第一个病 $rel_disease_sql = "SELECT diseaseid FROM 9939_disease_symptom_rel WHERE symptomid ={$symtpom_id}"; $rel_disease_ret = $db_v2jb->createCommand($rel_disease_sql)->queryAll(); //获取所有症状关联的疾病id if (isset($rel_disease_ret[0])) { break; } } foreach ($rel_disease_ret as $k => $v) { $rel_disease_id_arr[] = $v['diseaseid']; } //**************2、根据部位取出所有的疾病************** //第一个部位的名称 $condition_part_name = $all['XS'][0]['word']; //通过部位名查部位id $condition_part_sql = "select id from 9939_part where name='{$condition_part_name}'"; $condition_part_ret = $db_v2jb->createCommand($condition_part_sql)->queryOne(); //3、如果根据部位查不到疾病,设置疾病id为根据症状查出来的第一个疾病 if (!$condition_part_ret) { $last_disease_id = $rel_disease_id_arr[0]; } else { $condition_part_id = $condition_part_ret['id']; //67 //3.1、通过部位查找所有的疾病 $part_disease_sql = "SELECT unique_key FROM 9939_part_rel_merge WHERE source_flag=1 AND partid={$condition_part_id}"; $part_disease_ret = $db_v2jb->createCommand($part_disease_sql)->queryAll(); foreach ($part_disease_ret as $k => $v) { $part_disease_id_arr[] = str_replace('disease', '', $v['unique_key']); } //**************3.2、取二者的交集的第一个疾病作为最终疾病************** $last_disease_id_arr = array_intersect($rel_disease_id_arr, $part_disease_id_arr); //如果没有交集时: if (empty($last_disease_id_arr)) { if (!empty($rel_disease_id_arr)) { $last_disease_id_arr[0] = $rel_disease_id_arr[0]; } else { if (!empty($part_disease_id_arr)) { $last_disease_id_arr[0] = $part_disease_id_arr[0]; } } } sort($last_disease_id_arr); $last_disease_id = $last_disease_id_arr[0]; } //先直接查疾病表是否有该疾病 //如果有直接返回查询结果 //如果没有,通过从症状表查 //通过查wd_disease表得到disease_id $disease_name_sql = "select id,name from 9939_disease where id={$last_disease_id}"; $disease_name_ret = $db_v2jb->createCommand($disease_name_sql)->queryOne(); $disease_name = $disease_name_ret['name']; $class_level3_sql = "select class_level1,class_level2,id from wd_disease where name='{$disease_name}'"; $class_level3 = $db_v2sns->createCommand($class_level3_sql)->queryOne(); if (isset($class_level3['id'])) { $wd_keshi_ret = $class_level3; $wd_keshi_ret['class_level3'] = $class_level3['id']; unset($wd_keshi_ret['id']); return $wd_keshi_ret; } else { //通过疾病id查科室 $disease_condition = 'disease' . $last_disease_id; $department_sql = "select depart.name from 9939_department depart,9939_depart_rel_merge merge where merge.source_flag=1 and merge.unique_key='{$disease_condition}' and merge.departmentid=depart.id"; $department_ret = $db_v2jb->createCommand($department_sql)->queryOne(); $department_name = $department_ret['name']; //通过科室名称进入wd_keshi查询最终的 $wd_keshi_sql = "select class_level1,class_level2 from wd_keshi where name='{$department_name}'"; $wd_keshi_ret = $db_v2sns->createCommand($wd_keshi_sql)->queryOne(); //如果查不到数据放到默认科室里 if (!isset($wd_keshi_ret['class_level1'])) { $wd_keshi_ret = $wd_keshi_ret_default; } return $wd_keshi_ret; } } //未考虑到的情形,直接返回默认科室 return $wd_keshi_ret_default; }