Step 4 of 4

How to Create your own Search Engine with Python Language and Laravel Framework

Andika Pratama
Analytics Vidhya
Published in
6 min readJan 29, 2020

--

Step 4 : Implement Index and Query From Python Language to Laravel Framework

https://belajarphp.net/wp-content/uploads/2019/03/upload-file-laravel-840x430.jpg

What is Laravel ?

From wikipedia :

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony.

So basically laravel is open source PHP web framework with MVC concept. MVC or Model View Controller is a software approach that separates applications based on components.

  • Model : The model represents of data structure
  • View : is the part that regulates the display to the user
  • Controller : The controller is the part that bridges the model and view. but basically we use the controller as a back end

In this tutorial we will not use controller as bridges, because we will not implement the model in our application. Why not implement model ? you know the model represent of data structure like database, but in our case we have created our own data structure using python language from previous tutorial.

Prerequisites

So like the previous part to follow along with this part tutorial you should have :

  • Composer
  • Laravel 6.x.x
  • NPM and Node.js
  • JQuery (we will use ajax from JQuery)
  • Query script and Index file from Previous tutorial
  • Code Editor (in this tutorial I will use Visual Studio Code, Optional)

Instalation Laravel

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

you can install Laravel by issuing the Composer create-project command in your terminal:

$ composer create-project --prefer-dist laravel/laravel SearchBook

SearchBook” is a project name that I made , so you can change with your own.

If instalation succes you will see directory with your project name. Open your project with your Code Editor

Note: If you have Npm and Node Js instaled in your machine , you can just skip to “Npm Setup”

Instalation Npm and Node Js

Before install Npm and Node js, Verify that the Node.js and npm were installed in your machine by printing their versions:

$ npm -v$ node -v

if yo do not have npm and node js, you can just follow these steps: :

  1. Enable the NodeSource repository by running the following curl as a user with sudo privileges:
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

The command will add the NodeSource signing key to your system, create an apt sources repository file, install all necessary packages and refresh the apt cache.

If you need to install another version, for example 12.x, just change setup_10.x with setup_12.x

2. Once the NodeSource repository is enabled, install Node.js and npm by typing:

$ sudo apt install nodejs

The nodejs package contains both the node and npm binaries.

3. Verify that the Node.js and npm were successfully installed by printing their versions

Npm Setup

make sure you are in your project directory, if not run this command

$ cd <project_name>

Setup npm in your project with this command

$ npm install

wait a minute installation is done and run this command :

$ npm run dev

this command to load your npm modules installed in your project

Bootrap 4 Install with Npm

To install bootrao just type this command in your terminal project :

$ npm install bootstrap

and fill app.scss in directory “resource/sass/app.scss” with this code :

// bootsrap
@import "node_modules/bootstrap/scss/bootstrap";

load your module with run dev command

$ npm run dev

Start Your Laravel

to start your laravel in your project directory terminal run this command :

$ php artisan serve//Output
Laravel development server started: http://127.0.0.1:8000

just open the started server “http://127.0.0.1:8000” in your browser

Make view Search page in Laravel

to make view add a file with name “landing.blade.php” in your directory “resources/views/landing.blade.php”. make sure add blade in your name

fill the view landing.blade.php with this code

landing.blade.php

and change your default route in file “routes/web.php” to this code :

Route::get('/', function () {
return view('landing');
});

if you refresh your project in browser, you will see :

Search Engine Interface

we will use ajax to display a data in real time process without refresh the page, so you can just add this code in your “lading.blade.php” view :

script ajax search book

Make Controller Search in Laravel

so in ajax code you will see, we run “/search?q=**&rank=**” in our ajax if search button on click. so in your route file add this code :

Route::get('search', [
'as' => 'search', 'uses' => 'LandingController@search'
]);

this code make a get route to our function “search” in controller “LandingController”. so we will make a controller with name “LandingController”, just run this command :

$ php artisan make:controller LandingController

It will create a controller file in your project, and open the file controller in “app/Http/Controllers/LandingController.php”. fill the controller with this code :

LandingController.php

in the code you will see a “search” function code, this code it will executed by ajax when the search button is pressed by the user

$query = $request->input('q');
$rank = $request->input('rank');

That code to get your query and rank value from your view code

to run python code in php, we will use this code :

$process = new Process("
python3 query.py indexdb {$rank} \"{$query}\"
");
$process->run();

it will execute the query.py in our public directory. so before I am forget, copy your query script and index file to public directory in your laravel project

Query script and Indexdb file

and you will get output process :

$list_data = array_filter(explode("\n",$process->getOutput()));

That code it will get your process output in terminal and change it to list array data. list data. data list will be in json format so you have to decode it first, to encode I use this code :

$dataj =  json_decode($book, true);

So you just fill a $data variable with php code it will display in your view with data you get from query. and the last send your array variable to view from ajax with this code :

echo json_encode($data);

in your ajax view landing.blade.php you can see this code :

$.ajax({
url:'/search?q='+cari+'&rank='+rank,
dataType : "json",
success: function(data){
$('#content').html(data);
},
error: function(data){
alert("Please insert your command");
}
});

In the code you can see, I just change the id “#content” page html to our data it will get from search function in LandingController

So your search engine application basically can be used now

Screenshoot Demo Process

Screenshoot test search process

Reference :

code : https://github.com/Andika7/searchbook

Closing Remark !

I hope you’ve found the last part of this tutorial helpful. We learn how to make your own Search Engine with laravel framework and python language

So with this part, the tutorial “How to Create your own Search Engine with Python Language and Laravel Framework“ also ends. Thank you for reading to the end

Please refer to this link for another part.

Part 1 : https://medium.com/analytics-vidhya/how-to-create-your-own-search-engine-with-python-language-and-laravel-framework-step-1-of-4-f25e5ba1ab92

Part 2 : https://medium.com/analytics-vidhya/how-to-create-your-own-search-engine-with-python-language-and-laravel-framework-step-2-of-4-1e356e0efb90

Part 3 : https://builtin.com/machine-learning/index-based-search-engine-python

--

--

Andika Pratama
Analytics Vidhya

Fresh Graduate of Computer Science at Universitas Syiah Kuala, Software Engineer. Check my github on github.com/Andika7