联想加索引mapping
curl -XPUT 'http://192.168.1.1:9200/xiaodu/question/_mapping' -d '{
"properties": {
"asid": {
"type" : "long"
},
"ask_time": {
"type" : "long"
},
"title": {
"type" : "string",
"analyzer": "ik",
"searchAnalyzer": "ik"
},
"keywords": {
"type" : "string",
"analyzer": "ik",
"searchAnalyzer": "ik"
},
"keywords_array": {
"type" : "string",
"index": "not_analyzed"
},
"gold": {
"type" : "long"
},
"solve": {
"type" : "long"
},
"answer_count": {
"type" : "long"
},
"ask_uid": {
"type" : "long"
},
"fenshu": {
"type" : "long"
},
"ask_uname": {
"type" : "string",
"index": "not_analyzed"
},
"content": {
"type" : "string",
"analyzer": "ik",
"searchAnalyzer": "ik"
},
"answer_time": {
"type" : "long"
},
"answer_time": {
"type" : "long"
},
"answer_uid": {
"type" : "long"
},
"answer_uname": {
"type" : "string",
"index": "not_analyzed"
}
}
}'
curl -XPUT 'http://192.168.1.1:9200/xiaodu/searchSuggest/_mapping' -d '
{
"searchSuggest": {
"properties": {
"key_id": {
"type" : "long"
},
"key_score": {
"type" : "long"
},
"keyword": {
"type" : "string",
"analyzer": "ik",
"searchAnalyzer": "ik"
},
"keyword_suggest": {
"type": "completion",
"analyzer": "ik",
"search_analyzer": "ik",
"payloads": false
},
"keyword_pinyin_suggest": {
"type": "completion",
"analyzer": "pinyin_analyzer",
"search_analyzer": "pinyin_analyzer",
"payloads": false
}
}
}
}'
php索引数据:
<?php
/**
* Created by PhpStorm.
* User: nihuan
* Date: 16-12-15
* Time: 下午2:51
*/
date_default_timezone_set('Asia/Shanghai');
require dirname(__FILE__).'/EsPHP/vendor/autoload.php';
//require '/alidata/webroot/elasticsearch/xiaodu/EsPHP/vendor/autoload.php';
class indexSearch
{
protected $index = 'demoL';
protected $db;
protected $state;
protected $es;
protected $size = 500;
protected $bulk_template;
public function __construct()
{
//数据库链接
$config = array(
'host'=>'127.0.0.1',
'user'=>'root',
'password'=>'root',
'database' => 'wenda'
);
$connect = mysqli_connect($config['host'],$config['user'],$config['password'],$config['database']);
if($connect !== false){
$this->db = $connect;
mysqli_query($this->db,"SET NAMES utf8");
}else {
echo 'Db Error:' . mysqli_error($connect) . PHP_EOL;
}
$hosts = [
'127.0.0.1:9200',
];
//连接ES服务器
$this->es = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$this->bulk_template = [
'asid' => 0,
'title' => '',
'ask_time' => 0,
'gold' => 0,
'solve' => 0,
'answer_count' => 0,
'ask_uid' => 0,
'fenshu'=>0,
'ask_uname' => '',
'content' => '',
'answer_time' => 0,
'answer_uid' => 0,
'keywords' => '',
'answer_uname' => ''
];
}
/**
* 索引数据主体操作
*/
public function doIt(){
$this->get_data();//索引问题列表
$this->complation_keyword();
mysqli_close($this->db);
}
/**
* 搜索问题列表
* @Version 1.0
*/
protected function get_data(){
$last_id = 0;
echo '开始统计数据' . PHP_EOL;
$sql = "SELECT * FROM think_ask WHERE asid > {$last_id} ORDER BY asid ASC";
$query = mysqli_query($this->db,$sql);
$row = mysqli_num_rows($query);
$pages = ceil($row/$this->size);
if($pages > 0){
for($i = 1;$i<=$pages;$i++){
$bulk = ['index' => 'xiaodu', 'type' => 'question'];
$searchsql = "SELECT * FROM think_ask WHERE asid > {$last_id} ORDER BY asid ASC LIMIT " . $this->size;
$query = mysqli_query($this->db,$searchsql);
while($item = mysqli_fetch_assoc($query)){
$bulk_template = $this->bulk_template;
$bulk_template['asid'] = $item['asid'];
$bulk_template['title'] = $item['content'];
$bulk_template['ask_time'] = $item['time'];
$bulk_template['gold'] = $item['gold'];
$bulk_template['solve'] = $item['solve'];
$bulk_template['answer_count'] = $item['answer'];
$bulk_template['ask_uid'] = $item['uid'];
$bulk_template['ask_uname'] = $item['uname'];
// echo '问题:' . $item['content'] . PHP_EOL;
// echo '|-----提问时间:' . date('Y-m-d H:i:s',$item['time']) . PHP_EOL;
// echo '|-----所属用户:' . $item['uname'] . PHP_EOL;
// echo '|-----内容获取中...' . PHP_EOL;
//答案
$answer = "SELECT * FROM think_answer WHERE asid = {$item['asid']}";
$ansRes = mysqli_query($this->db,$answer);
$result = mysqli_fetch_assoc($ansRes);
if(!$result){
echo '|-----此问题没有内容:' . date('Y-m-d H:i:s ',$item['asid']) . PHP_EOL;
}else
{
// echo '|-----回答时间:' . date('Y-m-d H:i:s',$result['time']) . PHP_EOL;
// echo '|-----答复用户:' . $result['uname'] . PHP_EOL;
// echo '|-----回答内容:' . $result['content'] . PHP_EOL;
$bulk_template['content'] = $result['content'];
$bulk_template['answer_time'] = $result['time'];
$bulk_template['answer_uid'] = $result['uid'];
$bulk_template['answer_uname'] = $result['uname'];
//关键词
$keyword = "SELECT keyword,fenshu FROM think_keyword WHERE answerid = {$result['anid']}";
$keyRes = mysqli_query($this->db,$keyword);
//$resultq = mysqli_fetch_assoc($keyRes);
// if ($resultq != false) {
// echo '|-----回答时间:' . $resultq['fenshu'] . PHP_EOL;
// $bulk_template['fenshu'] = $resultq['fenshu'];
$keywords = [];
$keywordsArray = [];
while ($keyRow = mysqli_fetch_assoc($keyRes)) {
$tempItem = array();
$tempItem['keyword'] = $keyRow['keyword'];
$tempItem['fenshu'] = $keyRow['fenshu'];
$keywordsArray[] = $tempItem;
$keywords[] = $keyRow['keyword'];
}
$bulk_template['keywords'] = implode(',', $keywords);
$bulk_template['keywords_array'] = json_encode($keywordsArray);
// //分类
// $category = "SELECT t.cname FROM think_category t LEFT JOIN think_ques_cate AS ca ON ca.cid = t.cid WHERE ca.asid = {$result['anid']}";
// $cateQue = mysqli_query($this->db, $category);
// while ($cateRow = mysqli_fetch_assoc($cateQue)) {
// $keywords[] = $cateRow['cname'];
// }
// $bulk_template['keywords'] = implode(',',$keywords);
// }
}
$bulk['body'][]=array(
'index' => array(
'_id'=>$item['asid']
),
);
$bulk['body'][]=$bulk_template;
$last_id = $item['asid'];
}
$bulkRes = $this->es->bulk($bulk);
if($bulkRes){
echo '第' . $i . '页数据导入成功' . PHP_EOL;
}else{
echo '第' . $i . '页数据导入失败' . PHP_EOL;
}
}
}
}
/**
* 自动提示记录
* @Version 1.0
*/
protected function complation_keyword(){
$last_id = 0;
echo '问题列表获取' . PHP_EOL;
$sql = "SELECT * FROM think_ask WHERE asid > {$last_id} ORDER BY asid ASC";
$query = mysqli_query($this->db,$sql);
$row = mysqli_num_rows($query);
$pages = ceil($row/$this->size);
if($pages > 0){
for($i = 1;$i<=$pages;$i++){
$searchsql = "SELECT * FROM think_ask WHERE asid > {$last_id} ORDER BY asid ASC LIMIT " . $this->size;
$query = mysqli_query($this->db,$searchsql);
while($item = mysqli_fetch_assoc($query)){
$body = $suggest = [];
// echo '问题:' . $item['content'] . PHP_EOL;
// echo '|-----提问时间:' . date('Y-m-d H:i:s',$item['time']) . PHP_EOL;
//答案
$answer = "SELECT * FROM think_answer WHERE asid = {$item['asid']}";
$ansRes = mysqli_query($this->db,$answer);
$result = mysqli_fetch_assoc($ansRes);
if($result != false){
//关键词
$keyword = "SELECT keyword FROM think_keyword WHERE answerid = {$result['anid']}";
$keyRes = mysqli_query($this->db,$keyword);
$keywords = [];
while($keyRow = mysqli_fetch_assoc($keyRes)){
$keywords[] = $keyRow['keyword'];
}
// //分类
// $category = "SELECT t.cname FROM think_category t LEFT JOIN think_ques_cate AS ca ON ca.cid = t.cid WHERE ca.asid = {$result['anid']}";
// $cateQue = mysqli_query($this->db,$category);
// while($cateRow = mysqli_fetch_assoc($cateQue)){
// $keywords[] = $cateRow['cname'];
// }
$keywords[] = $item['content'];
$suggest['input'] = $keywords;
$suggest['output'] = $item['content'];
$body['keyword'] = $keywords;
$body['keyword_suggest'] = $body['keyword_pinyin_suggest'] = $suggest;
$res = $this->es->index(array('index'=>'xiaodu','type'=>'searchSuggest','body'=>$body,'id'=>md5($item['asid'])));
if($res){
echo '问题:<<' . $item['content'] . '>>数据导入成功' . PHP_EOL;
}else{
echo '问题:<<' . $item['content'] . '>>数据导入失败' . PHP_EOL;
}
}
$last_id = $item['asid'];
}
}
}
}
}
$search = new indexSearch();
$search->doIt();

浙公网安备 33010602011771号