PHP调用ElasticSearch服务
<?php
namespace App\Services;
use App\Models\Article;
use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
protected $client;
public $index = 'itxxx';
public $hosts = ['127.0.0.1:9190'];
const TYPE = 'articles';
public function __construct()
{
$this->client = ClientBuilder::create()->setHosts($this->hosts)->build();
}
public function createIndex()
{
$indexParams = [
'index'=>$this->index,
];
$response = $this->client->indices()->exists($indexParams);
if($response){
return false;
}
$indexParams['body'] = [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0
]
];
return $this->client->indices()->create($indexParams);
}
public function createMappings() {
$params = [
'index' => $this->index,
'type' => self::TYPE,
'body' => [
self::TYPE => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer', // 整型
'index' => 'not_analyzed',
],
'title' => [
'type' => 'string', // 字符串型
'index' => 'analyzed', // 全文搜索
'analyzer' => 'ik_max_word'
],
'content' => [
'type' => 'string',
'index' => 'analyzed',
'analyzer' => 'ik_max_word'
],
'description' => [
'type' => 'string',
'index' => 'analyzed',
'analyzer' => 'ik_max_word'
],
'keywords' => [
'type' => 'string',
'index' => 'analyzed',
'analyzer' => 'ik_max_word'
],
'category_id'=>[
'type'=>'integer',
],
'show_id'=>[
'type'=>'string',
],
'author'=>[
'type'=>'string',
],
'click'=>[
'type'=>'string',
],
'status'=>[
'type'=>'integer',
],
'is_top'=>[
'type'=>'integer',
],
'created_at'=>[
'type'=>'date',
],
]
]
]
];
$response = $this->client->indices()->putMapping($params);
return $response;
}
public function initInsert()
{
$indexParams = [
'index'=>$this->index,
];
$this->client->indices()->open($indexParams);
$total = Article::count();
$totalPage = ceil($total/10);
for ($i = 1;$i<=$totalPage;$i++){
$articles = Article::offset($i)->limit(20)->get()->toArray();
echo $i."完成\n";
if($articles){
$params = ['body'=>[]];
foreach ($articles as $article){
unset($article['markdown'],$article['cover']);
$params['body'][] = [
'index'=>[
'_index' =>$this->index,
'_type' =>self::TYPE,
'_id'=>$article['id'],
]
];
$params['body'][] = $article;
}
$this->client->bulk($params);
}
}
return true;
}
public function search($filter, $page = 1, $pageSize = 10,$orderBy = [])
{
$index['index'] = $this->index;
//return $this->client->indices()->delete($index);
$index['type'] = self::TYPE;
$index['body']['size'] = $pageSize;
$index['body']['from'] = ($page-1) * $pageSize;
// $index['body']['highlight'] =['require_field_match'=>false,'fields'=>['*'=>[]]];
// '{"require_field_match": false,"fields": {"*" : { }}'
$keywords = $filter['keytitle'] ?? '';
if($keywords){
$index['body']['query'] =
[
'bool' => [
'must' => [
[ 'match' => [ 'title' => [
'query' => $keywords,
'boost' => 4, // 权重大
"minimum_should_match"=> "100%"
]]],
// [ 'match' => [ 'content' => [
// 'query' => $keywords,
// 'boost' => 2,
// ]]],
[ 'match' => [ 'description' => [
'query' => $keywords,
'boost' => 3,
"minimum_should_match"=> "100%"
]]],
],
],
];
}else{
if(empty($orderBy)){
$index['body']['sort'] = ['id'=>['order'=>'desc']];
}else{
foreach ($orderBy as $key=>$val){
$index['body']['sort'][$key] = ['order'=>$val];
}
}
}
$index['body']['query']['bool']['filter'] = [
"term" => [ "status" => 1]
];
if(isset($filter['category_id']) && $filter['category_id']){
$index['body']['query']['bool']['filter'] = [
"term" => [ "category_id" => $filter['category_id']]
];
}
$result = $this->client->search($index);
//var_dump( $result);
$list['total_count'] = $result['hits']['total'];
$list['list'] = [];
foreach ($result['hits']['hits'] as $key=>$row){
$tem = $row['_source'];
unset($tem['content'],$tem['id']);
$list['list'][] = $tem;
}
return $list;
}
public function update($id,$data = [])
{
if(!$data){
$data = Article::where('id',$id)->first()->toArray();;
}
if(!$data){
return false;
}
unset($data['markdown'],$data['cover']);
$params = [
'index'=>$this->index,
'type'=>self::TYPE,
'id'=>$data['id'],
'body'=>[
'doc'=>$data
]
];
$response = $this->client->update($params);
return $response;
}
public function insert($id,$data = [])
{
if(!$data){
$data = Article::where('id',$id)->first();
}
if(!$data){
return false;
}
unset($data['markdown'],$data['cover']);
$params = [
'index'=>$this->index,
'type'=>self::TYPE,
'id'=>$data['id'],
'body'=>$data->toArray()
];
$response = $this->client->index($params);
return $response;
}
public function delete($id)
{
if (!$id) {
return false;
}
$params = [
'index' => $this->index,
'id' => $id,
'type' => self::TYPE,
];
$response = $this->client->delete($params);
return $response;
}
public function getMapping() {
$params = [
'index' => $this->index,
'type' => self::TYPE
];
$response = $this->client->indices()->getMapping($params);
return $response;
}
public function __call($method,$parameters)
{
return $this->client->indices()->$method(...$parameters);
}
}
有话要说