Git Product home page Git Product logo

egg-es-plugin's Introduction

egg-es-plugin

NPM version build status Test coverage David deps Known Vulnerabilities npm download

安装插件

$ npm i egg-es-plugin --save

插件开启与配置

开启插件

// config/plugin.js
exports.esPlugin = {
  enable: true,
  package: 'egg-es-plugin',
};

插件配置

// /config/config.default.js
exports.esPlugin = {
  client: {
    host: 'localhost:9200', // elasticsearch 访问地址
    apiVersion: '7.6', // elasticsearch 版本(一个小数点即可,即不要写精确版本,如 7.6.1 需写为 7.6)
  },
};

see config/config.default.js for more detail.

使用案例

案例所示是在 egg service 中调用

async findOneByES(userId) {
  const { ctx } = this;

  try {
    const esData = await ctx.esSearch({
      index: 'user',
      from: 0,
      size: 10,
      body: {
        query: {
          match: {
            id: userId
          },
        },
      },
    });

    return esData.data[0]
  } catch (error) {
    throw error;
  }
}

出入参说明

如上,我们可以通过 ctx.esSearch 方法来对 elasticsearch 中指定的索引检索数据,入参如下:

  1. index:索引名称,必传
  2. from:数据下标,类似数组索引,非必传
  3. size:返回数据的条数,非必传
  4. body:具体的 elasticsearch 查询规则,必传

经过 elasticsearch 查询,我们得到了返回的查询结果 esData,其中结构如下:

{
  total: 3,
  data: [{}, {}, {}]
}
  • total:表示满足查询条件的数据条数
  • data:本次查询返回的数据,data 恒定为数组格式

License

MIT

egg-es-plugin's People

Watchers

 avatar  avatar

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.