0

I am trying to connect elasticsearch server to work on my localhost (real-estate-laravel.test). I downloaded and activated elasticsearch and have elastichsearch server running on http://localhost:9200/ , like this

{
"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"
}

And I added in my composer.json

"require": {
    "elasticsearch/elasticsearch": "^7.0"
}

and entered composer update. I am now confused on how to connect elasticsearch server with my localhost, so when I make some search queries in my controller they will search through elasticsearch. Any help is appreciated. Here is my 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()) 
        {
        }
    }
} 

In this if(Auth::check()) will go certain queries. Also when I dump $client I get this

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: []
}

1 Answer 1

1

You might want to use cviebrock/laravel-elasticsearch package instead.

It allows you to setup your host information at .env file easily and use ClientBuilder as your current code.

Sign up to request clarification or add additional context in comments.

4 Comments

Aha, I see. And do I just add these lines ELASTICSEARCH_HOST=localhost ELASTICSEARCH_PORT=9200 in env or do I replace them with current localhost, password etc. ?
After that you need to change your builder part like $client = ClientBuilder::create([env('ELASTICSEARCH_HOST') . ':' . env('ELASTICSEARCH_PORT')])->build();
@hkulekci I changed it. So now, how can I test this to see if elasticsearch works?
Create an index. then save (index) some documents. Then search on this index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.