node vue 实时推送_如何使用Node,Vue和ElasticSearch构建实时搜索引擎

node vue 实时推送

介绍 (Introduction)

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. Elasticsearch is built on top of Apache Lucene, which is a high-performance text search engine library.

Elasticsearch是一个分布式的RESTful搜索和分析引擎,能够解决越来越多的用例。 Elasticsearch建立在Apache Lucene之上, Apache Lucene是一个高性能的文本搜索引擎库。

In this tutorial, you will build a real-time search engine using Node.js, Elasticsearch, and Vue.js. As a result, a basic comprehension of Vue.js and Node.js (Express) is needed to follow this tutorial.

在本教程中,您将使用Node.js ,Elasticsearch和Vue.js构建一个实时搜索引擎。 因此,需要基本的Vue.js和Node.js(Express)理解才能遵循本教程。

第1步-准备环境 (Step 1 — Preparing the Environment)

Let’s get started with setting up the environment for this lesson. Since you will use Node.js, the easiest way to get started is to create a new folder and run npm init. Create a new folder called elastic-node, change directory into the new folder and then run npm init:

让我们开始设置本课的环境。 由于将使用Node.js,因此最简单的入门方法是创建一个新文件夹并运行npm init 。 创建一个名为elastic-node的新文件夹,将目录更改为新文件夹,然后运行npm init

Create a new directory called elastic-node:

创建一个名为elastic-node的新目录:

  • mkdir elastic-node

    mkdir弹性节点

Move into the new folder:

移至新文件夹:

  • cd elastic-node

    cd弹性节点

Run npm init to create a package.json file:

运行npm init创建一个package.json文件:

  • npm init

    npm初始化

The above commands take you through the process of creating a package.json file, which is required to run any Node.js library.

上面的命令将带您完成创建package.json文件的过程,这是运行任何Node.js库所必需的。

Next, you need to install libraries that will be needed for the real-time search engine. Install the libraries with the following command:

接下来,您需要安装实时搜索引擎所需的库。 使用以下命令安装库:

  • npm install express body-parser elasticsearch

    npm install express正文解析器elasticsearch

The express library will run the server, while the body-parser library works with express to parse body requests. elasticsearch is the official Node.js library for Elasticsearch, which is the engine on which the real-time search will be built.

express库将运行服务器,而body-parser库与express一起使用以解析主体请求。 elasticsearch是Elasticsearch的官方Node.js库,该库是构建实时搜索的引擎。

Now the first part of your environment is set up. However, Elasticsearch itself is missing from your setup. You will need to install Elasticsearch which can be done in a few different ways. If you are using a Debian Linux operating system, you can just download the .deb file and install using dpkg.

现在,您的环境的第一部分已设置完毕。 但是,您的设置中缺少Elasticsearch本身。 您将需要安装Elasticsearch,这可以通过几种不同的方式完成。 如果您使用的是Debian Linux操作系统,则只需下载.deb文件并使用dpkg安装。

Run the following command to download the .deb file:

运行以下命令以下载.deb文件:

  • curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.deb

    curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.4.deb

Now install the deb package using dpkg:

现在使用dpkg安装deb软件包:

  • sudo dpkg -i elasticsearch-5.6.4.deb

    须藤dpkg -i elasticsearch-5.6.4.deb

For other distributions/operating systems, you can find a guide on how to install Elasticsearch here.

对于其他发行版/操作系统,您可以在此处找到有关如何安装Elasticsearch的指南。

Elasticsearch is not started automatically after installation. Elasticsearch can be started and stopped using the service command.

安装后,Elasticsearch不会自动启动。 可以使用service命令启动和停止Elasticsearch。

Run the following to start the elasticsearch service:

运行以下命令以启动elasticsearch服务:

  • sudo -i service elasticsearch start

    sudo -i服务elasticsearch开始

You can stop the elasticsearch service with this command:

您可以使用以下命令停止elasticsearch服务:

  • sudo -i service elasticsearch stop

    sudo -i服务elasticsearch停止

To configure Elasticsearch to start automatically when the system boots up, run this to reload the systemctl daemon:

要将Elasticsearch配置为在系统启动时自动启动,请运行以下命令以重新加载systemctl守护程序:

  • sudo /bin/systemctl daemon-reload

    须藤/ bin / systemctl守护进程重新加载

Then enable elasticsearch so it can be called as a service:

然后启用elasticsearch以便可以将其称为服务:

  • sudo /bin/systemctl enable elasticsearch.service

    sudo / bin / systemctl启用elasticsearch.service

After running the command above, you can start Elasticsearch with this command:

运行上述命令后,您可以使用以下命令启动Elasticsearch:

  • sudo systemctl start elasticsearch.service

    sudo systemctl启动elasticsearch.service

Stop it with the following:

使用以下命令停止它:

  • sudo systemctl stop elasticsearch.service

    sudo systemctl停止elasticsearch.service

You can also check the status of Elasticsearch:

您还可以检查Elasticsearch的状态:

  • sudo service elasticsearch status

    sudo服务elasticsearch状态

第2步—在Elasticsearch中索引数据 (Step 2 — Indexing Data in Elasticsearch)

Create a data.js file in your root folder and add the following code:

在您的根文件夹中创建一个data.js文件,并添加以下代码:

data.js
data.js
//require the Elasticsearch librray
const elasticsearch = require('elasticsearch');
// instantiate an Elasticsearch client
const client = new elasticsearch.Client({
   hosts: [ 'http://localhost:9200']
});
// ping the client to be sure Elasticsearch is up
client.ping({
     requestTimeout: 30000,
 }, function(error) {
 // at this point, eastic search is down, please check your Elasticsearch service
     if (error) {
         console.error('Elasticsearch cluster is down!');
     } else {
         console.log('Everything is ok');
     }
 });

This code first requires the Elasticsearch library and then sets up a new Elasticsearch client, passing in an array of a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值