How to use elasticsearch from scratch in laravel 5?

How to use elasticsearch from scratch in laravel 5?

In this post we will give you information about How to use elasticsearch from scratch in laravel 5?. Hear we will give you detail about How to use elasticsearch from scratch in laravel 5?And how to use it also give you demo for it if it is necessary.

When i was working on my ecommerce website on laravel, i was need to apply search engine for my website. I was confused and thinking what i have to use, but my senior suggested to me elasticsearch for search engine. But i had no experience elasticsearch. I need to learn from starting but i found how to install elasticsearch and use in our laravel application.

Why we choose elasticsearch for search engine because elasticsearch provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and JSON documents. So, in this example i going to give you full example from scratch, after this example you can find bellow preview.

Preview

Step 1: Install Elasticsearch

in first step you should install elasticsearch in your local machine. If you haven’t installed then you can install from here: ElasticSearch Docs.

If you are working on ubuntu system then you can install easily from here : How to confige Elasticsearch in our local system.

Step 2: Package Installation

In this step we will install elasticquent/Elasticquent package for use elasticsearch api. so first fire bellow line add in your composer.json file and then update composer:

"elasticquent/elasticquent": "dev-master"

After install elasticquent/elasticquent package, we need to add provider path and alias path in config/app.php file so open that file and add bellow code.

config/app.php

return [

......

'provides' => [

......

......,

ElasticquentElasticquentServiceProvider::class,

],

'aliases' => [

......

......,

'Es' => ElasticquentElasticquentElasticsearchFacade::class,

],

]

Now we need to generate configration file for elastichsearch so fire bellow command in your terminal:

php artisan vendor:publish --provider="ElasticquentElasticquentServiceProvider"

Step 3: Create items table and model

In this step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateItemsTable extends Migration

{


public function up()

{

Schema::create('items', function (Blueprint $table) {

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}


public function down()

{

Schema::drop("items");

}

}

After craete “items” table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:

app/Item.php

namespace App;


use IlluminateDatabaseEloquentModel;

use ElasticquentElasticquentTrait;


class Item extends Model

{

use ElasticquentTrait;


public $fillable = ['title','description'];


}


Step 4: Route and Controller

In this step you have to add some route in your route file. so first copy bellow route and put in your file.

app/Http/routes.php

Route::get('ItemSearch', 'ItemSearchController@index');

Route::post('ItemSearchCreate', 'ItemSearchController@create');

Ok, now we should create new controller as ItemSearchController in this path app/Http/Controllers/ItemSearchController.php. this controller will manage all search from elastichsearch and add to index:

app/Http/Controllers/ItemSearchController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;

use AppHttpRequests;

use AppItem;


class ItemSearchController extends Controller

{

/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function index(Request $request)

{

if($request->has('search')){

$items = Item::search($request->input('search'))->toArray();

}

return view('ItemSearch',compact('items'));

}


/**

* Display a listing of the resource.

*

* @return IlluminateHttpResponse

*/

public function create(Request $request)

{

$this->validate($request, [

'title' => 'required',

'description' => 'required',

]);


$item = Item::create($request->all());

$item->addToIndex();


return redirect()->back();

}

}

Step 5: Create View

This is a last step and you have to create ItemSearch.blade.php file for manage listing on search function so just copy and paste this file :

ItemSearch.blade.php

@extends('layouts.app')


@section('content')

<div >

<div >

<h1 style="text-align: center;">Laravel 5 Search Using Elasticsearch</h1>

</div>

</div>


<div >

<div >

<div >

<div >

<div >

{!! Form::open(array('method'=>'get','class'=>'')) !!}

<div >


<input name="search" value="{{ old('search') }}" type="text" placeholder="Search for...">

<span >

<button type="submit">Go!</button>

</span>


</div><!-- /input-group -->

{!! Form::close() !!}

</div><!-- /.col-lg-6 -->

</div><!-- /.row -->

</div>

<div >


<div >

<div >

@if(!empty($items))

@foreach($items as $key => $value)

<h3 >{{ $value['title'] }}</h3>

<p>{{ $value['description'] }}</p>

@endforeach

@endif

</div>

<div >

<div >

<div >

Create New Items

</div>

<div >


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::open(array('url' => 'ItemSearchCreate','autocomplete'=>'off')) !!}

<div >

<div >

<div >

<strong>Title:</strong>

{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}

</div>

</div>

<div >

<div >

<strong>Description:</strong>

{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}

</div>

</div>

</div>


<div >

<button type="submit" >Submit</button>

</div>


{!! Form::close() !!}


</div>

</div>

</div>

</div>


</div>

</div>

</div>

@endsection

Now you can check in your project and also you can get more information from here : elasticquent/Elasticquent.

Hope this code and post will helped you for implement How to use elasticsearch from scratch in laravel 5?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US