Connect With Me

Contact Me If You Getting Any Problem on fb and twitter
facebook twitter

Thursday, 16 July 2015

elastic search php ubuntu

Here I Am Giving You How To Install Elastic Search and In Second Step I Am giving how to Integrate It In php
 (using ubuntu)
Installation:

Installing from the Debian software package

wget  https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.7.deb

dpkg -i elasticsearch-0.90.7.deb


This results in Elasticsearch being properly installed in /usr/share/elasticsearch.


Recall that installing from the Debian package also installs an init script in /etc/elasticsearch that starts the Elasticsearch server running on boot. The server will also be immediately started after installation


To Post Data:

curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'




"tutorial" is index of the data in Elasticsearch.
"helloworld" is the type.
"1" is the id of our entry under the above index and type

To Get Data:

Other Way To  Post Data:

curl -X PUT 'http://localhost:9200/clients/client/5' -d '{"id":"5", "name":"Jannet"}'

To search data using prefix:

curl -X GET 'http://localhost:9200/clients/_search?pretty' -d '{
"query" : {
"prefix" : {
"name" : "j",
"rewrite" : "constant_score_boolean"
}
}
}'

How To use Elastic Search:
There are two method to use elastic search in ubuntu

1 Use terminal with curl install
ctrl+alt+T

2 Use chrome web browser plugin called Sense
Search and download from here

Here I am using sense

Now Installation and basic part is over Now Below step is how we can integrate it into php



Installing Composer


1 Go To Directory first  of your project through terminal
anoop@HP-Mini-210-1100:/var/www/html/elastict$

2 Create file name composer.json in your project and add below text

{
    "require": {
       "elasticsearch/elasticsearch": "~1.0"
    }
}

3 Run This-   curl -sS https://getcomposer.org/installer | php (this command should be run when you are in your project directory)
4 Again Run This-   
php composer.phar install
5 When you run a folder will be created name vendor


Showing you file structure of my project:


elastic (Main Project)
    vendor (folder)
    composer.json(file)
    composer.phar(file)
    composer.lock(file)
    add.php(file) (this I have created to add data to elastic search)
    index.php(file) (this I have created to search data from elastic search)



All the files you will get when you install composer ,I am giving you code for the two files which I have made:




add.php


………………………………………………………………………………….

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

require 'vendor/autoload.php';

$client = new Elasticsearch\Client();

if(!empty($_POST)){
   if(isset($_POST['title'],$_POST['body'],$_POST['keywords'])){
     
      echo $title=$_POST['title'];
      $body=$_POST['body'];
      $keywords=explode(',',$_POST['keywords']);
     
      $indexed=$client->index([
      'index' =>'articles',
        'type'=>'article',
        'body'=>[
              'title'=>$title,
              'body'=>$body,
              'keywords'=>$keywords
              ]
          ]);
         
          if($indexed){
              print_r($indexed);
          }
   }
}

?>
<html>

<body>
<form action="add.php" method="post" autocomplete="off">
<label>
Title<input type="text" name="title">
</label>
<label>
Body<textarea name="body" rows="8">
   </textarea>
</label>
<label>
Keywords<input type="text" name="keywords" placeholder="comma,seprated">
</label>
<input type="submit" name="Add">
</form>

</body>
</html>



………………………………………………………………………………………………
index.php

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

require 'vendor/autoload.php';

$client = new Elasticsearch\Client();


   if(isset($_GET['q'])){
     
      echo $q=$_GET['q'];
      $query=$client->search([
      'body' =>[
      'query' =>[
      'bool' =>[
              'should'=> [
              'match'=>['title'=>$q],
              'match'=>['body'=>$q]
             
          ]
      ]
   ]
   ]
   ]);
   
   
   
   if($query['hits']['total']>=1){
      $result=$query['hits']['hits'];
   }
   
   }
     
?>
<html>

<body>
<form action="index.php" method="get" autocomplete="off">
<label>
search for somethingh
<input type="text" name="q">
</label>
<input type="submit" name="Search">
</form>
<?php
if(isset($result)){
   foreach($result as $r){
      ?>
      <div>
      <?php echo $r['_source']['title']; ?>
      </div>
      <?php
     
   }
   
}

?>

</body>
</html>









 

No comments:

Post a Comment