1

i'm using :

AngularJs : AngularJS v1.2.22

Bootstrap : Bootstrap v3.1.1 + ui-bootstrap-tpls-0.11.0.js

Elasticsearch.angular : elasticsearch - v2.4.0 - 2014-07-30

I work on a front end with angular and i want to make an autocompleted input using twitter-bootstap typeahead on an elasticsearch document. I can query elasticsearch with angularJs and get the data correctly (by putting them in the scope) but when i try an asynchrone query it failed with an error **"Error: matches is undefined" ( full error her : http://pastebin.com/CJSubYbp )

In angularjs, service for elasticsearch :

interfaceApp.service('elasticQuery', function (esFactory) {
   return esFactory({ host: 'localhost:9200' });
});

my controller : 'use strict';

/* Controllers */
var searchSimpleModules = angular.module('searchSimpleModules', ['ngRoute']);

searchSimpleModules.controller('searchSimpleCtrl', function ($scope, $rootScope, $routeParams, $location, elasticQuery) {
   $scope.simplequery = "";
   $scope.autocomplete = function(value) {
      $scope.simplequery = value;
      var index_p = 'donnees';
      var size_p = 50;
      var body_p = {
         "query": {
            "query_string": { "query" : value +"*" }
         }
      };
      elasticQuery.search({
         index: index_p,
         size: size_p,
         body: body_p
      }).then(function (response) {
         $scope.hits= response.hits.hits;
      });
   };

   $scope.find = function () {
      elasticQuery.search({
         index: 'donnees',
         size: 50,
         body: {    "query": {  "query_string": { "query" : "P000023*" }  }         }
      }).then(function (response) {
         $scope.hits= response.hits.hits;
      });
   };
});

the html input:

 <input required type="text"
       popover="Rechercher un terme sur toute la partie {{simplesearch.domaine}}.Il est possible d'utiliser des *,? et double quote."
       popover-trigger="focus"
       placeholder="recherche globale"
       class="form-control"
       typeahead="entree for entree in autocomplete($viewValue)"
       ng-model="simplequery"
>

In the firebug console i see that i get the error before the "http.get()" made by the service.

I make many search to debug this but i can't get out of it.

I read http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html

Any advice are welcomed. thanks Best regards

AlainIb
  • 4,544
  • 4
  • 38
  • 64

2 Answers2

0

update 1 : i found another solution but not using the elastic.js

<input required type="text"
   popover="Rechercher un terme sur toute la partie {{simplesearch.domaine}}.Il est possible d'utiliser des *,? et double quote."
   popover-trigger="focus"
   placeholder="recherche globale"
   class="form-control search-query"
   ng-model="simplequery"
   typeahead="entree as entree.data for entree in autocomplete($viewValue) | filter:$viewValue | limitTo:15 "
   typeahead-on-select="onSelect($model)"
/>

js:

$scope.simplequery = {id:"",data:""};
$scope.autocomplete = function(val) {
  var body = '{"fields" : [ "O_OCCURRENCEID","C_COLLECTIONID", "C_COLLECTIONCODE", "I_INSTITUTIONID", "I_INSTITUTIONCODE" ], "query" : {"query_string" : { "query" : "*'+val+'*" }},"highlight":{"pre_tags" : ["<strong>"],"post_tags" : ["</strong>"],"fields":{"*": {}}}}';
  return $http.get($rootScope.elastic_host + 'specimens/_search?', {
     params: {      // q: val
        source : body
     }
  }).then(function(res){
     var keywords = [];
     for (var i in res.data.hits.hits) {
        var highlights = (res.data.hits.hits[i]).highlight;
        var ligneTmp ="";
        for(var j in highlights){
          ligneTmp += highlights[j][0] + " ";
        }
        keywords[i]= {id:"id"+i,data:ligneTmp};
     }
     return keywords;
  });
};
$scope.onSelect = function (selection) {
  $scope.simplequery.data = selection.data;
  $scope.simplequery.id = selection.id;
  ...
};
AlainIb
  • 4,544
  • 4
  • 38
  • 64
0

update 2: using elasticsearch-angular.js :

autocomplete/typeahead angularjs bootstrap on elasticsearch

( it was not working because of the return in "return elasticQuery.search({...})" was missing )

Community
  • 1
  • 1
AlainIb
  • 4,544
  • 4
  • 38
  • 64