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.

Community
  • 1
  • 1
Mahdi DIF
  • 159
  • 1
  • 10
  • 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. – skal88 May 02 '16 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 – Mike Miller May 05 '17 at 21:15

1 Answers1

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) {
    // ....
})
skal88
  • 464
  • 2
  • 10
  • 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) { // .... })` – Mahdi DIF May 02 '16 at 14:11