0

I'm trying to implement an autocomplete feature using Elasticsearch, angularJS and bootstrap.

I've got inspired by this solution : autocomplete/typeahead angularjs bootstrap on elasticsearch

This is my Angular code:

angular.module('cineAngularApp')
     .service('client', function (esFactory) {
        return esFactory({
            host: 'localhost:9200',
            apiVersion: '2.2',
            log: 'trace'
        });
     });

 angular.module('cineAngularApp')
 .controller('AutocompleteCtrl', function ($scope,client) {

    $scope.getResult = function(val){

        return client.search({
            index: 'autocomplete_test',
            fields: 'city', 
            q: 'city:'+val
        }).then(function (resp) {
            var keywords = [];
            for(var i in resp.hits.hits){
                var fields = (resp.hits.hits[i]).fields["city"];
                keywords.push(fields);
            }
            return keywords;
        }, function (err) {
            console.trace(err.message);
        });
    };
 });

Here is my problem

The above code works fine when I use a simple query, but as soon as I change the query by adding body it doesn't work.

 angular.module('cineAngularApp')
 .controller('AutocompleteCtrl', function ($scope,client) {

    $scope.getResult = function(val){

        return client.search({
            index: 'autocomplete_test',
            fields: 'city', 
            body: {
                query: {
                    match: {
                        city: val
                    }
                }
            }
        }).then(function (resp) {
            var keywords = [];
            for(var i in resp.hits.hits){
                var fields = (resp.hits.hits[i]).fields["city"];
                keywords.push(fields);
            }
            return keywords;
        }, function (err) {
            console.trace(err.message);
        });
    };
 });

I don't know if it can help but I've also noticed when debugging that it's not a POST request anymore but it's an OPTION one.

Thanks in advance for your help.

2
  • in your code you have a sintax error in "query": "title:"+var. I don't know that this is the problem. you change this to "query": "title"+val and try it. Commented May 2, 2016 at 13:21
  • The OPTIONS request is your preflight CORS request. If it's failing you need to respond with proper CORS headers to those requests Commented May 5, 2017 at 21:15

1 Answer 1

0

Try with this:

return client.search({
    index: 'movies',
    "fields": [ "title" ], 
    "body": { // Use body field on elasticsearch client library
        "query": {
            "query_string": {
               "fields": ["title"],
               "query": "title:"+val
            }
        }
    }
}).then(function (resp) {
    // ....
})
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried your code, but it didn't work. When I add body, I don't have results at all. The only query that works for me is : return client.search({ index: 'movies', fields: 'title', q: 'title:'+val}).then(function (resp) { // .... })

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.