es安装
composer require elasticsearch/elasticsearch
<?php
namespace app\admin\controller;
use app\BaseController;
use Elasticsearch\ClientBuilder;
use think\App;
use think\facade\View;
class Es extends BaseController
{
/**
*添加
*/
public function add()
{
$es=new \app\common\lib\ES("es");
$params = [
'index' => "es",
'type' => "article",
"body" => [
"title" => "张一鸣创建今日头条,2012年8月发布第一个版本。",
"desn" => "今日头条是北京字节跳动科技有限公司开发的一款基于数据挖掘的推荐引擎产品,为用户推荐信息、提供连接人与信息的服务的产品。由张一鸣于2012年3月创建,2012年8月发布第一个版本。",
]
];
dump($es->add_doc($params));
}
/**
* 获取一条数据
*/
public function get()
{
$es=new \app\common\lib\ES("es");
$data=$es->get_doc("4VYFxngBlczRx1VkoN3U","es","article");
print_r($data);
}
/**
* 修改
*/
public function update()
{
$es=new \app\common\lib\ES("es");
$params = [
'index' => "es",
'type' => "article",
'id' => "4lYYxngBlczRx1Vk591y",
"body" => [
"doc" => [
"title" => "6100万颗心的共同记忆 再次C位亮相,闪耀全球!",
"desn" => "刚刚过去的这个清明节,与往年一样,有人凭寄哀思,有人缅怀忠魂。但也有一些瞬间,让人记起久久不能释怀,给这个特殊节气增添了一些格外不同的味道。"
]
]
];
dump($es->update_doc($params));
}
/**
* 删除
*/
public function delete()
{
$es=new \app\common\lib\ES("es");
$data=$es->delete_doc("4VYFxngBlczRx1VkoN3U","es","article");
dump($data);
}
/***
* 搜索
* @return \think\response\Json
*/
public function search()
{
$es=new \app\common\lib\ES("es");
$body = [
'query' => [
'bool' => [
'should' => [
[
'match' => [
'title' => [
'query' => "张一鸣",
'boost' => 4, // 权重大
]
]
],
[
'match' => [
'desn' => [
'query' => "张一鸣",
'boost' => 3,
]
]
]
],
],
],
'from' => 0,
'size' => 2
];
return json($es->search_doc("es","article",$body));
}
/**
* 查询所有数据
* @return \think\response\View
*/
publi