npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-es-plugin

v1.0.0

Published

egg 连接 elasticsearch

Downloads

5

Readme

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