Thursday, June 9, 2016

How to use elastic search suggester to perform auto complete

elastic-search suggester 

Use kibana sense to create your mappings 
Note:Dont forget to add suggest: filed with your mapping as shown below 

eg:
{
  "mappings": {
    "User": {
      "properties": {
        "fName": {
          "type": "string",
          "index": "not_analyzed"
        },  
         "suggest" : {
           "input": [ "Nevermind", "Nirvana" ],//on these input you will get the suggestions 
           "output": "Nirvana - Nevermind",//this will be your auto complete return result
           "payload" : { "artistId" : 2321 },//you can include the payload too with return resule
           "weight" : 34 //preference 
        }
     }
   }
}

  

Then to use the suggester
you need to do like shown below
On the head object like in this case "music" is parent object which has "songs"
you need to add suggester and point to suggest field


curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
    "song-suggest" : {
        "text" : "n",
        "completion" : {
            "field" : "suggest"
        }
    }
}'
To add fuzzy suggester
curl -X POST 'localhost:9200/music/_suggest?pretty' -d '{
    "song-suggest" : {
        "text" : "n",
        "completion" : {
            "field" : "suggest",
            "fuzzy" : {
                "fuzziness" : 2
            }
        }
    }
}'


that's it you will get your suggester working

NOTE: this search might  also show you deleted documents .
if you dont want this to happen then you need to index your page every time you add or update the data

As shown below :
$ curl -XPOST 'http://localhost:9200/_optimize?only_expunge_deletes=true'


for more info: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html


How to trigger events based on "Storage events" on Google Cloud


Create a storage:
https://cloud.google.com/storage/docs/quickstart-console

Add a watch
https://cloud.google.com/storage/docs/object-change-notification#_Notification_Types

Create a function that triggers on watch
https://cloud.google.com/functions/calling#google_cloud_storage

Tuesday, June 7, 2016

Dynamic Image Resize using google cloud storage(GCS) and getServing URL

you need to have 
Google cloud storage bucket name
and
following Code



Simply just use my GIT repo as follows  just change your projectID and bucket name 

USE this git repo

https://github.com/koolkarni/resize-image-google-cloud-service-GetServingURL-PHP.git

OR

index.php:


<?php
//var_dump($_FILES['uploaded_files']['tmp_name']);
syslog(LOG_WARNING, "Request came");
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
syslog(LOG_WARNING, "Imported Cloud Storage Tools");
//var_dump( $_GET);
$object_url=$_GET["image"];
$size=intval($_GET["size"]);
syslog(LOG_WARNING, "Object URL $object_url");
syslog(LOG_WARNING, "Size $size");

$bucket="gs://YOUR-PROJECT-ID.appspot.com/bucket_name/";
$object_image_url = CloudStorageTools::getImageServingUrl($object_url,['size' => $size, 'crop' => false]);
syslog(LOG_WARNING, "Output Url $object_image_url");
header("location: $object_image_url");

closelog();
?>



app.yaml:

runtime: php55
api_version: 1

handlers:
- url: /.*
script: index.php



upload these files in GIT hub repo 

once you have the code in git download it into your google cloud development-->Repo  by following the steps below:


Step1















 Step2





























Step3














Step4













Step5


























Step6











Once your code is in repo. open the GC console then 
run the following command in GC shell

 go to the project dir (i.e the folder with projectID)

gcloud source repos clone image1 --project=Prj1234


where image1: is your repo name Prj1234 is your project code

now traverse inside your image1 folder then follow the following steps


gcloud preview app deploy app.yaml







to check your output hit the URL like this where image1.jpg is file from 
your cloud storage