<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Models\models\exam\Laugh;
use App\Models\models\exam\Weather;
use Elasticsearch\ClientBuilder;
use Illuminate\Http\Request;
use function Psy\debug;
class LaughController extends Controller
{
private $client;
public function __construct()
{
$this->client=ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
public function createSearch(){
$params = [
// 生成索引的名称
'index' => 'newair',
// 类型 body
'body' => [
'settings' => [
// 分区数
'number_of_shards' => 5,
// 副本数
'number_of_replicas' => 1
],
'mappings' => [
// 字段 类似表字段,设置类型
'properties' => [
//字段
'date' => [
'type' => 'text',// 数据类型
//standard 相当于数据查询是的 = 张三你好,必须找到张三你好
//ik_max_word 中文分词 张三你好 张三 你好 张三你好
'analyzer' => 'ik_max_word',// 分析器
'search_analyzer'=> 'ik_max_word'
],
'weather' => [
'type' => 'text'
],
'temperature' => [
'type' => 'text'
],
'direct' => [
'type' => 'text'
],
'id'=>[
'type'=>'integer'
]
]
]
]
];
$response = $this->client->indices()->create($params);
dump($response);
exit();
}
//高亮显示
public function heightList(Request $request){
$name=$request->get('name');
$params = [
'index' => 'weather',
'body' => [
'query' => [
'term' => [
'direct' => "$name"
]
],
'highlight' => [
'pre_tags' => ["<em>"],
'post_tags' => ["</em>"],
'fields' => [
"direct" => new \stdClass()
]
]
]
];
$response = $this->client->search($params);
foreach ($response['hits']['hits'] as $key=>$value){
$arr[$key]['date']=$value['_source']['date'];
$arr[$key]['weather']=$value['_source']['weather'];
$arr[$key]['temperature']=$value['_source']['temperature'];
// $arr[$key]['direct']=$value['_source']['direct'];
$arr[$key]['highlight']=$value['highlight']['direct'][0];
}
print_r($arr);
}
//向索引中添加数据
public function addSearchs(){
$user=new Weather();
$arr=$user->get()->toArray();
foreach ($arr as $k=>$v){
$params=[
'index'=>'weather',
'id'=>$v['id'],
'body'=>[
'date'=>$v['date'],
'weather'=>$v['weather'],
'temperature'=>$v['temperature'],
'direct'=>$v['direct'],
],
];
$this->client->index($params);
}
return '添加成功';
exit();
}
//查询所有的数据
public function searchData(Request $request){
$id=$request->get('id');
$params = [
'index' => 'weather',
'body' => [
'query' => [
'match' => [
'weather' => "eI-bJngBh6Rwn29EEfwT",
]
]
]
];
$results = $this->client->search($params);
print_r($this->client->search($params)) ;
}
//访问笑话的接口
public function weather()
{
$key = config("juhe.key");
$city="上海";
$key="";
$url="http://apis.juhe.cn/simpleWeather/query?city=$city&key=$key";
$result = $this->geturl($url);
// print_r($result['result']['future']);exit();
foreach ($result['result']['future'] as $key=>$v){
$arr['date']=$v['date'];
$arr['weather']=$v['weather'];
$arr['temperature']=$v['temperature'];
$arr['direct']=$v['direct'];
Weather::insert($arr);
}
echo $this->addsearchs();
echo "<br>";
echo 1;
}
function geturl($url)
{
$headerArray = array("Content-type:application/json;", "Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output, true);
return $output;
}
//
}