1.用composer下载elasticsearch
es库地址:https://packagist.org/packages/elasticsearch/elasticsearch
2.连接es
use Elasticsearch\ClientBuilder;
require 'vendor/autoload.php';
$host = array(
'127.0.0.1:9200',
'127.0.0.1:9201',
'127.0.0.1:9202',
);
$esClient = ClientBuilder::create()->setHosts($host)->build();
3.创建es索引以及type
$params = [
'index'=>'article_index'
];
$response = $esClient->indices()->create($params);
$params = [
'index' => 'article_index',
'type' => 'article_content',
'body' => [
'article_content' => [
'_source' => [
'enabled' => true
],
'properties' => [
'article_content_id' => [
'type' => 'integer'
],
'article_category_id' => [
'type' => 'text',
'analyzer' => 'keyword',
],
'article_category_id_arr'=>[
'type' => 'nested'
],
'article_subject_id'=>[
'type'=>'text',
'analyzer' => 'keyword',
],
'article_subject_id_arr'=>[
'type'=>'nested'
],
'content_order' => [
'type' => 'integer',
],
'content_is_image' => [
'type' => 'integer'
],
'content_is_video'=>[
'type'=>'integer'
],
'content_focus_image'=>[
'type'=>'integer'
],
'content_title_image'=>[
'type'=>'integer'
],
'content_time_publish'=>[
'type'=>'integer'
],
'content_count_access'=>[
'type'=>'integer',
],
'content_thief' =>[
'type'=>'integer',
]
]
]
]
];
$response = $esClient->indices()->putMapping($params);
4.增删改查操作
$params = [
'index'=>'article_index',
'type'=>'article_content',
'body'=>[
'id'=>'1',
'article_content_id'=>1,
'article_category_id'=>'00100400g008000000000000000000000000000000000',
'article_category_id'=>[
'00100400g008000000000000000000000000000000000',
'001003001008000000000000000000000000000000000',
'001002001008000000000000000000000000000000000'
],
'content_order' => 10,
'content_is_image' => 0,
'content_is_video' => 0,
'content_focus_image' => 0,
'content_title_image' => 0,
'content_time_publish' => 1582710322,
'content_count_access' => 50,
'content_thief' => 0,
]
];
$response = $esClient->index($params);
$params = [
'index'=>'article_index',
'type' =>'article_content',
'body' => [
'query'=>[
'match'=>[
'article_content_id'=>1,
]
]
],
];
$response = $esClient->deleteByQuery($params);
$condition = [
'article_category_id' =>['00100400g008000000000000000000000000000000000']
'has_image' => 1,
'title_image' => 0,
'focus_image' => 1,
'flv_video' => 0,
'content_thief' => 0,
]
$where = [
'match'=>[
'article_category_id'=>$condition['article_category_id'][0],
]
];
$notWhere = [];
$query = [
'bool'=>[
'must'=>$where,
'must_not'=>$notWhere,
'filter'=>[]
],
];
$condition['has_image'] ? array_push($query['bool']['filter'],['term'=>['content_is_image'=>1]]):"";
$condition['title_image'] ? array_push($query['bool']['filter'],['term'=>['content_title_image'=>1]]):"";
$condition['focus_image'] ? array_push($query['bool']['filter'],['term'=>['content_focus_image'=>1]]):"";
$condition['flv_video'] ? array_push($query['bool']['filter'],['term'=>['content_is_video'=>1]]):"";
$condition['content_thief'] ? array_push($query['bool']['filter'],['term'=>['content_thief'=>1]]):"";
$params = [
'index' => 'article_index',
'type' => 'article_content',
'_source_include'=> 'article_content_id',
'body' =>[
'from'=>0,
'size'=>2,
'query'=>$query,
]
];
$response = $esClient->search($params);
if ($response){
$data = [];
foreach($response['hits']['hits'] as $k => $v){
$data[] = $v['_source'];
}
}
var_dump($data)
$where = [
'match'=>[
'article_content_id'=>1,
]
];
$params = [
'index' => 'article_index',
'type' => 'article_content',
'body' => [
'query' => [
'bool' => [
'must' => $where
],
],
'script'=>[
"inline"=> "ctx._source.content_title_long=params.content_title_long",
'params'=>['content_title_long'=>"更新测试标题"],
'lang'=>'painless'
]
]
];
$response= $esClient->updateByQuery($params);