Thursday, February 18, 2016

Elasticsearch with Angularjs

Hi All,
I have created this blog for Elasticsearch with angularJs, it is very rare to get combination of these both with configuration and examples you can download with free of cost.
I have done researched from past one month with this combination, i have got few programs but the function are abstract , to retrieve this function we need to pay.It is not free of cost, we can get only by using Master / Visa cards to retrieve this function.
I have developed this , as a quick reference with this combination hope it will beuseful for all.Here we can do come impmentation for Add /Delete /Modify from the database... let me know if you need any so that i can also help you out.
  • Advantages of Elastic search
  • Supports Restful services
  • Search using advanced queries
  • API call.
we can able to search quickly
Configuring Elastic serach on windows:
please follow the below steps to configure Elasticserach 1.4.1,
1. Download Elastic Search from (http://www.elasticsearch.org/overview/elkdownloads/).
2. Set class path for JAVA_HOME.
3. Change plugin.bat file inside (\\elasticsearch-1.4.1\bin) if necessary for update JAVA_HOME please refer the image below.
4. Open command prompt and run elasticsearch.bat file, please refer the image below.
5. Open the browser to check Elastic search is installed.(http://localhost:9200).
6. If the status shows 200, Elastic search is successfully installed on your machine.
Configuring Elastic serach on Linux:
1. sudo wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
2. edit the file "/etc/apt/sources.list", add the below line(select your version of ES)
deb http://packages.elasticsearch.org/elasticsearch/1.3/debian stable main
3. sudo apt-get update
4. sudo apt-get install elasticsearch
5. Now execute the "sudo apt-get install elasticsearch" command, it will install.
6. Now start the ES "sudo /etc/init.d/elasticsearch start"
7. Install head "plugin --install mobz/elasticsearch-head"
8. Install marvel "sudo bin/plugin -i elasticsearch/marvel/latest"
9. Install river-jdbc "sudo bin/plugin -install river-jdbc -url http://bit.ly/1dKqNJy"
10.install mysql-connector "sudo curl -o mysql-connector-java-5.1.28.zip -L 'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.28.zip/from/http://cdn.mysql.com/"
11. unzip the mysql-connector folder using "sudo unzip mysql-connector-java-5.1.28.zip -d mysql-connector"
12.copy the jar and paste inside elasticsearch/plugins/river-jdbc "sudo cp mysql-connector/mysql-connector-java-5.1.28/mysql-connector-java-5.1.28-bin.jar ./plugins/river-jdbc/"
13. Restart the ES using "sudo /etc/init.d/elasticsearch restart"
For start, stop,logs and restart the ES. please find the below commands,
  • sudo /etc/init.d/elasticsearch start
  • sudo /etc/init.d/elasticsearch stop
  • sudo /etc/init.d/elasticsearch restart
  • tail -f /var/log/elasticsearch/elasticsearch.log
                                                          Configure ES On Windows:


Run ES ON Browser:


Start ES on Command Prompt:



Plugin.bat and Java_HOME:






Create Index and Types:

open link: http://192.168.0.226:9200/_plugin/marvel/sense/index.html
command for creating index and type,
PUT /_river/divakar/_meta
{
"type" : "jdbc",
"jdbc" : {
"url": "jdbc:mysql://192.168.0.226:3306/test",
"user": "root",
"password": "root123",
"sql": "select *,sID as _id from USER_MASTERs",
"strategy" : "simple",
"schedule": "0 0/1 * * * ?" ,
"autocommit" : true,
"versioning" : true,
"digesting" : true,
"index" : "tcs",
"type" : "user_tcs"
}
}
Checking the index and types in head
open the url: http://192.168.0.226:9200/_plugin/head/

Sample program:

Below code to serach,
angular.module('controllers', [])
.controller('SearchCtrl', function($scope, ejsResource, $http,$window) {
var val0,val1,val2;
$http.post('/mysearchconfig').
success(function(data, status, headers, config) {
val0 = data[0];
val1 = data[1];
var ejs = ejsResource(val0);
var index = val1;
/* var highlightPost = ejs.Highlight(["text"])
.fragmentSize(150, "text")
.numberOfFragments(10, "text")
.preTags("<b>", "text")
.postTags("</b>", "text");*/
var userIndex = ejs.Request()
.indices(index)
.types(val2);
var passqueryVal = sessionStorage.getItem("SessionVal");
//$scope.searchUser = function() {
//$scope.Visible = ! $scope.Visible;
$scope.results = userIndex
.query(ejs.MatchQuery('_all', passqueryVal))
.doSearch().then(function (resp) {
$scope.resultUser = resp.hits.hits;
$scope.searchresult=passqueryVal;
$scope.countuser = resp.hits.total;//alert(JSON.stringify(resp.hits.hits[0]._source));

if($scope.resultUser == "")
$scope.errorMessageUser = "No Results Found";
//$scope.count = resp.hits.total;
}, function (err) {
console.trace(err.message);
});
//};
});
Create Index and Type: