https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/index.html
安装https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/installation.html
composer require elasticsearch/elasticsearch:~6.0
配置https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/configuration.html
7.0版本的可以设置密码
参考这篇文章https://blog.csdn.net/weixin_43315211/article/details/99677072
配置文件详解https://www.cnblogs.com/zhiqiangzhang/p/11425473.html
我们这里简单配置即可
vi /etc/elasticsearch/elasticsearch.yml
network.host:0.0.0.0
修改了后重启
任选一个
$hosts = [
'192.168.1.1:9200', // IP + Port
'192.168.1.2', // Just IP
'mydomain.server.com:9201', // Domain + Port
'mydomain2.server.com', // Just Domain
'https://localhost', // SSL to localhost
'https://192.168.1.3:9200' // SSL to IP + Port
];
//创建一个连接
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/index_management.html
初始化操作
//创建索引
route:get('/init',function(){
$hosts = [
'192.168.1.1:9200', // IP + Port
'192.168.1.2', // Just IP
'mydomain.server.com:9201', // Domain + Port
'mydomain2.server.com', // Just Domain
'https://localhost', // SSL to localhost
'https://192.168.1.3:9200' // SSL to IP + Port
];
//创建一个连接
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'my_index',//索引名
'body' => [
'settings' => [
'number_of_shards' => 1,//number_of_shards 是数据分片数,默认为5,有时候设置为3
'number_of_replicas' => 0//number_of_replicas 是数据备份数,如果只有一台机器,设置为0
],
'mappings' => [
'my_type' => [
'_source' => [
'enabled' => true
],
'properties' => [
//字段
'title' => [
'type' => 'text',
//分词器模式
'analyzer' => 'ik_max_word'
],
'desc' => [
//'type' => 'integer'
'type' => 'text',
'analyzer' => 'ik_max_word'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
})
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/indexing_documents.html
添加文档
$hosts = [
'192.168.0.89:9200', // IP + Port
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'article',
'type' => '_doc',
'id' => $article->id,//唯一的id
'body' => [
'title' => $article->title,//内容
'desc' => $article->content//内容
]
];
// Document will be indexed to my_index/my_type/my_id
$response = $client->index($params);
修改
$hosts = [
'192.168.0.89:9200', // IP + Port
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'article',
'type' => '_doc',
'id' => $id,
'body' => [
'title' => $request->input('title'),
'desc' => $request->input('content')
]
];
// Document will be indexed to my_index/my_type/my_id
$response = $client->index($params);
https://www.elastic.co/guide/en/elasticsearch/client/php-api/6.x/search_operations.html
搜索
$where = [];
if ($request->title){
$where["match_phrase"] = ['title' =>[
"query"=> $request->title,
"slop"=>100
]];
}
if ($request->desc){
$where["match_phrase"] = ['title' =>[
"query"=> $request->desc,
"slop"=>100
]];
}
//dd($where);
//单字段查询
/*$params = [
'index' => 'article',
'type' => '_doc',
'body' => [
'query' => [
'match_phrase' => $where
]
]
];*/
$params = [
'index' => 'article',
'type' => '_doc',
'body' => [
'query' => [
'bool' => [
'must' =>
$where
]
],
'highlight'=>[
"require_field_match"=> false,
"fields"=>[
"*"=>[
"pre_tags"=> [ "<font color='red'>" ],
"post_tags"=> [ "</font>" ]
]
]
]
]
];
$hosts = [
'192.168.0.89:9200', // IP + Port
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$results = $client->search($params);
dump($results);exit();
-----------------
取id
$ids = [];
foreach($results['hits']['hits'] as $v){
$ids[] = $v['_id'];
}