在AngularJS中使用elasticsearch.js查询

时间:2015-11-17 15:39:52

标签: javascript angularjs elasticsearch

我试图在我的AngularJS控制器中使用elasticsearch.js查询,但是还没有能够在http请求中将它们作为参数传递,或者在elasticsearch search()方法中使用它们。推荐的语法是什么?

import elasticsearch from "elasticsearch";

export default class SearchDirectiveController {
  constructor($scope, $http, SearchDirectiveService) {
    this.es = new elasticsearch.Client({
      host: 'http://192.168.99.100:9200',
      log: 'trace'
    });

    this.search = () => {
      this.es.search({
        index: 'voter',
        "query": {
            "match": {
              "first": "roger"
            }
          }
        }
      }).then((resp) => {
          var hits = resp.hits.hits;
          this.users = hits;
          $scope.$digest();
      },(err) => {
          console.trace(err.message);
      });
    }

    this.fullTextSearch = () => {
      $http({
        method: 'GET',
        url: 'http://192.168.99.100:9200/voter/voter,address/_search'
      }).then(function successCallback(resp) {
          console.log(resp.data.hits.hits);
          let hits = resp.data.hits.hits;
          $scope.users = hits;
      }, function errorCallback(err) {
          console.trace(err.message);
      }, {
        params: {
          "query": {
              "match": {
                "first": "roger"
              }
            }
          }
        });
      }
  }

}

SearchDirectiveController.$inject = ['$scope', '$http', 'SearchDirectiveService'];

1 个答案:

答案 0 :(得分:0)

我的问题是我忘了钥匙"身体"在搜索对象上:

    this.searchQuery = (field) => {
      return {
        'query': {
          'wildcard': {
            '_all': '*' + this.all + '*'
          }
        }
      };
    };

    this.search = (field) => {
      this.es.search({
        index: 'voter',
        body:  this.searchQuery(field)
      }).then((resp) => {
        const hits = resp.hits.hits;
        this.users = hits;
        $scope.$digest();
      }, (err) => {
        console.trace(err.message);
      });
    };
  }