./node_modules/@elastic/elasticsearch/lib/Connection.js 模块中的错误未找到:错误:无法解析“http”

Ica*_*ova 5 elasticsearch typescript angular angular6

我正在处理一个 angular 6 项目,我将服务器上的 elasticsearch 版本从 6 更新到 7,在将旧 API 的代码更改为使用新 API 后,出现了这些错误,有没有人知道如何纠正?

angular 应用程序是在 Visual Studio 代码上制作的,elasticsearch 在 linux 服务器上。

import { Injectable } from '@angular/core';
// import { Clientex } from 'elasticsearch-browser';
import { Client } from '@elastic/elasticsearch';
// import * as elasticsearch from 'elasticsearch-browser';
// import { concatMapTo } from 'rxjs/operators';
// import { stringify } from '@angular/core/src/render3/util';


@Injectable({
  providedIn: 'root'
})

export class ElasticsearchService {

  private client: Client;
  // private clientex: Clientex;

  constructor() {
    if (!this.client) {
      this.connect();
    }
  }

  private connect() {
    /*this.client = new Client(
      {node: 'http://localhost:9200'}
    );*/
    this.client = new Client();
  }

  isAvailable(): Promise<any> {
    return this.client.ping({
    });
  }


  // tslint:disable-next-line:member-ordering
  private queryalldocs = {
    'query': {
      'match_all': {}
    }
  };

  getAllDocuments(_index, _type): any {
    this.client.search({
      index: _index,
      body: this.queryalldocs
    }, {
      ignore: [404]
    }, (err, { body, statusCode, headers, warnings }) => {
      if (err) { console.log(err); }
    });
}

  getAllDocumentsByKey(_index, _type, campo, valor) {
    const string = 'let match = {' + campo + ':' + valor + '}';
    return this.client.search({
      index: _index,
      type: _type,
      body: {
           // tslint:disable-next-line:no-shadowed-variable
           eval(string) {}
      }
    });
  }

  createDocument(value: any) {
    // return this.client.index(value);
    this.client.create(value);
  }

}
Run Code Online (Sandbox Code Playgroud)

错误:

./node_modules/@elastic/elasticsearch/lib/Connection.js 模块中的错误未找到:错误:无法解析“C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@”中的“http”弹性\弹性搜索\lib'

./node_modules/@elastic/elasticsearch/lib/Connection.js 模块中的错误未找到:错误:无法解析“C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@”中的“https”弹性\弹性搜索\lib'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Transport.js Module not found: Error: Can't resolve 'os' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/decompress-response/index.js Module not found: Error: Can't resolve 'stream' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

ERROR in ./node_modules/@elastic/elasticsearch/lib/Transport.js Module not found: Error: Can't resolve 'zlib' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules@elastic\elasticsearch\lib'

ERROR in ./node_modules/decompress-response/index.js Module not found: Error: Can't resolve 'zlib' in 'C:\Users\ICARO\Desktop\projetos\pastas\xtr-social\node_modules\decompress-response'

小智 1

根据弹性搜索文档(https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/5.x/writing-changes.html),所有浏览器客户端均通过elasticsearch-browser处理npm 包:

不再支持浏览器,因为它将通过另一个模块 @elastic/elasticsearch-browser 进行分发。该模块仅适用于 Node.js。