在AngularJS中使用elasticsearch.js查询

Using elasticsearch.js queries in AngularJS

本文关键字:elasticsearch js 查询 AngularJS      更新时间:2023-09-26

我试图在AngularJS控制器中使用elasticsearch.js查询,但无法在http请求中将它们作为params传递,也无法在elasticsearch()方法中使用它们。建议的语法是什么?

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'];

我的问题是我忘记了搜索对象上的键"body":

    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);
      });
    };
  }