我正在尝试连接Elasticsearch服务器以在我的本地主机(real-estate-laravel.test)上工作。我下载并激活了 flex 搜索,并在http://localhost:9200/上运行了 flex 搜索服务器,就像这样

{
"name" : "DESKTOP-F9QAVC4",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "5br4u4tTTR-xzg7FBeInPg",
 "version" : {
  "number" : "7.3.0",
  "build_flavor" : "default",
  "build_type" : "zip",
  "build_hash" : "de777fa",
  "build_date" : "2019-07-24T18:30:11.767338Z",
  "build_snapshot" : false,
  "lucene_version" : "8.1.0",
  "minimum_wire_compatibility_version" : "6.8.0",
  "minimum_index_compatibility_version" : "6.0.0-beta1"
 },
"tagline" : "You Know, for Search"
}

我在我的composer.json中添加了
"require": {
    "elasticsearch/elasticsearch": "^7.0"
}

并输入 Composer 更新现在,我对如何将Elasticsearch服务器与本地主机连接感到困惑,因此当我在 Controller 中进行一些搜索查询时,它们将通过elasticsearch进行搜索。任何帮助表示赞赏。这是我的 Controller 。

CategoryController.php
<?php
namespace App\Http\Controllers;

use App\User;
use App\Category;
use App\Property;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Elasticsearch\ClientBuilder;

class CategoryController extends Controller
{
    public function search(Category $category, Property $property, User $user)
    {
        $client = ClientBuilder::create()->build();
        //dd($client);
        if(Auth::check())
        {
        }
    }
}

在这种情况下,if(Auth::check())将进行某些查询。另外当我转储$ client时,我得到了这个
Client {#351 ▼
  +transport: Transport {#349 ▶}
  #params: null
  #indices: IndicesNamespace {#352 ▶}
  #cluster: ClusterNamespace {#353 ▶}
  #nodes: NodesNamespace {#354 ▶}
  #snapshot: SnapshotNamespace {#355 ▶}
  #cat: CatNamespace {#356 ▶}
  #ingest: IngestNamespace {#357 ▶}
  #tasks: TasksNamespace {#358 ▶}
  #remote: RemoteNamespace {#359 ▶}
  #endpoints: Closure($class) {#350 ▶}
  #registeredNamespaces: []
}

最佳答案

您可能想使用cviebrock/laravel-elasticsearch软件包。

它使您可以轻松地在.env文件中设置主机信息,并将ClientBuilder用作当前代码。

09-18 06:27