下载windows版 Elasticsearch​ https://www.elastic.co/cn/downloads/elasticsearch​​ 找到bin下的elasticsearch.bat启动

启动后访问试试

windows laravel中使用ElasticSearch详情_php

一、快速开始

laravel6 安装es

composer require elasticsearch/elasticsearch

环境配置 .env

ELASTIC_HOST=127.0.0.1:9200 # 这里是你的 ElasticSearch 服务器 IP 及端口号
ELASTIC_LOG_INDEX=bf_log # ElasticSearch 索引
ELASTIC_LOG_TYPE=log # ElasticSearch 类型

Es 配置文件 /config/elasticsearch.php

<?php

return [
'hosts' => [
env('ELASTIC_HOST')
],
'log_index' => env('ELASTIC_LOG_INDEX'),
'log_type' => env('ELASTIC_LOG_TYPE'),
];

route/api.php

//测试路由
Route::any('get/index', 'Api\indexController@index')->name('index');

方法中使用

<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Elasticsearch\ClientBuilder;

class indexController extends Controller {
public $client = null;

public function __construct() {
$this-> client = ClientBuilder::create()->build();
}

//创建
public function index(){
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];

$response = $this->client->index($params);
print_r($response);
}

}

执行 域名/api/get/index, 返回

Array(
[_index] => my_index
[_type] => my_type
[_id] => my_id
[_version] => 1 #每刷新一次version值会+1,从1递增
[result] => created #第一次执行result值是created,第二次刷新是update
[_shards] => Array(
[total] => 2
[successful] => 1
[failed] => 0)
[_seq_no] => 0 #每刷新一次,seq_no会+1,从0递增
[_primary_term] => 1)

下面连上数据库


//创建 public function index() { $id = 3; $info = User::where(['id' => 3])->first()->toArray(); $params = [ 'index' => 'name', 'type' => 'title', 'id' => $id, 'body' => ['testField' => $info] ]; $response = $this->client->index($params); dd($response); } //创建 public function mysql() { $client = ClientBuilder::create()->build(); $data = [ 'type' => 'title' ]; $response = $client->search($data); return $response['hits']['hits']; }


windows laravel中使用ElasticSearch详情_php_02