Giter Club home page Giter Club logo

midway-elasticsearch's Introduction

简介

这个是 midway 3.0 elasticsearch 组件 模块

安装依赖

$ npm i midway-elasticsearch --save

引入组件

首先,引入 组件,在 src/configuration.ts 中导入

import { Configuration } from '@midwayjs/decorator';
import * as elasticsearch from 'midway-elasticsearch';
import { join } from 'path';

@Configuration({
  imports: [
    // ...
    elasticsearch, // 导入 elasticsearch 组件
  ],
  importConfigs: [join(__dirname, 'config')],
})
export class ContainerLifeCycle {}

配置

单客户端配置

// src/config/config.default.ts
export default {
  // ...
  config.elasticsearch = {
    client: {
      node: {
        url: new URL('http://xxxx:1200'),
      },
      auth: {
        username: 'elastic',
        password: 'changeme',
      },
    },
  };
};

更多参数可以查看 elasticsearch 文档。

使用

我们可以在任意的代码中注入使用。

import { Provide, Controller, Inject, Get } from '@midwayjs/decorator';
import { ElasticsearchService } from 'midway-elasticsearch';

@Provide()
export class UserService {
  @Inject()
  elasticsearchService: ElasticsearchService;

  //创建或更新索引中的文档,如果目标是索引并且文档已经存在,则请求更新文档并增加其版本
  async index() {
    await this.elasticsearchService.index({
      index: 'students',
      type: '_doc',
      body: {
        name: 'John Doe',
        age: 17,
        hobby: 'basketball',
      },
    });
  }

  // 返回匹配查询的结果
  async search() {
    await this.elasticsearchService.search({
      index: 'students',
      type: '_doc',
    });
  }

  // 使用脚本或部分文档更新文档。
  async update() {
    await this.elasticsearchService.update({
      index: 'students',
      type: '_doc',
      id: '1',
      body: {
        doc: {
          hobby: 'swimming',
        },
      },
    });
  }

  // 删除文档
  async del() {
    await this.elasticsearchService.delete({
      index: 'students',
      type: '_doc',
      id: '1',
    });
  }
}

midway-elasticsearch's People

Contributors

ddzyan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

chief-fei

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.