0

I am using AngularJS with ElasticSearch.

Until now I have been playing around with ElasticSearch using the curl command in my terminal.

Now, I wish to perform a search in AngularJS on my elastic search indices. How do I do it? I assume using $http.get() but I could not find an example anywhere.

Basically, how do I convert the following:

curl -XGET 'http://localhost:9200/twitter/mark/_search?pretty=true&size=100' -d '{
    "term": {
        "tag": "comedy"
    }
}'

to an Angular request? That is how do I achieve the above in AngularJS inside my controller?

1
  • 3
    Take a look at elastic.js. They have created an angular service to wrap calls to elastic engine (tutorial fullscale.co/blog/2013/02/28/… ) Commented Apr 6, 2013 at 17:38

1 Answer 1

1

Elasticsearch has released an official JS client. You can find it on github: https://github.com/elasticsearch/elasticsearch-js

I found the following example https://github.com/elasticsearch/elasticsearch-js/issues/19 to be helpful in setting up the client instance. Once you have the client set up, (client is 'es' and queryTerm would be mapped to a search box or similar) you can perform searches like so:

esApp.controller('SearchCtrl', function($scope, es) {
es.cluster.health(function (err, resp) {
    if (err) {
        $scope.data = err.message;
    } else {
        $scope.data = resp;
    }
});

$scope.search = function() {
    es.search({
        index: 'your_index',
        size: 50,
        body: {
            query: {
                query_string: {
                    default_field: 'title', 
                    query: ($scope.queryTerm || '*')
                }
            }
        }
    }).then(function (resp) {
        $scope.results = resp.hits.hits;
        $scope.hitCount = resp.hits.total;
        }, function (err) {
            $scope.results(err.message);
    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.