当前位置: 首页 > news >正文

适合新手做的网站静态培训后的收获和感想

适合新手做的网站静态,培训后的收获和感想,台州市建设招标投标网站,唐山网站建设400多少钱在我之前的文章 “Elasticsearch:ES|QL 查询 TypeScript 类型(一)”,我们讲述了如何在 Nodejs 里对 ES|QL 进行查询。在今天的文章中,我们来使用一个完整的例子来进行详细描述。更多有关如何使用 Nodejs 来访问 Elasti…

在我之前的文章 “Elasticsearch:ES|QL 查询 TypeScript 类型(一)”,我们讲述了如何在 Nodejs 里对 ES|QL 进行查询。在今天的文章中,我们来使用一个完整的例子来进行详细描述。更多有关如何使用 Nodejs 来访问 Elasticsearch的知识,请参阅文章 “Elasticsearch:使用最新的 Nodejs client 8.x 来创建索引并搜索”。

在一下的演示中,我将使用 Elastic Stack 8.13.4 来进行展示。

安装

Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考如下的链接来进行安装:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Kibana:如何在 Linux,MacOS 及 Windows上安装 Elastic 栈中的 Kibana

在安装的时候,我们选择 Elastic Stack 8.x 来进行安装。特别值得指出的是:ES|QL 只在 Elastic Stack 8.11 及以后得版本中才有。你需要下载 Elastic Stack 8.11 及以后得版本来进行安装。

在首次启动 Elasticsearch 的时候,我们可以看到如下的输出:

我们需要记下 Elasticsearch 超级用户 elastic 的密码。

我们还可以在安装 Elasticsearch 目录中找到 Elasticsearch 的访问证书:

$ pwd
/Users/liuxg/elastic/elasticsearch-8.13.4/config/certs
$ ls
http.p12      http_ca.crt   transport.p12

在上面,http_ca.crt 是我们需要用来访问 Elasticsearch 的证书。

Nodejs 依赖包

我们可以使用如下的命令来安装最新的 nodejs 客户端包:

yarn add @elastic/elasticsearch或者npm install @elastic/elasticsearch

我们可以通过如下的命令来查看安装的版本:

$ npm -v @elastic/elasticsearch
8.19.2

创建项目目录并拷贝证书

我们在电脑里创建一个目录,并拷贝相应的 Elasticsearch 访问证书到该目录下:

$ pwd
/Users/liuxg/nodejs/esql
$ cp ~/elastic/elasticsearch-8.13.4/config/certs/http_ca.crt .
$ ls http_ca.crt 
http_ca.crt

我们使用如下的命令来安装:

npm install --save-dev @types/node

创建一个叫做 esql.ts 的文件

touch esql.ts

我们使用如下的命令来安装 ts-node:

npm install -g ts-node typescript '@types/node'

在下面我们将使用如下的命令来运行代码:

ts-node esql.ts 

展示

连接到 Elasticsearch

我们编辑 esql.ts 如下:

import { Client } from '@elastic/elasticsearch'
import * as fs from "fs";const client = new Client({node: 'https://localhost:9200',auth: {username: 'elastic',password: '=VnaMJck+DbYXpHR1Fch'},tls: {ca: fs.readFileSync('./http_ca.crt'),rejectUnauthorized: false}})client.info().then((response) => console.log(JSON.stringify(response))).catch((error) => console.error(JSON.stringify(error))); 

在上面,我们使用超级账号 elastic 来进行连接。我们使用证书来访问自签名证书的集群。你需要根据自己的 Elasticsearch 配置修改上面的代码。更多关于如何访问 Elasticsearch 的知识,请阅读文章 “Elasticsearch:使用最新的 Nodejs client 8.x 来创建索引并搜索”。 运行上面的代码,返回:

$ ts-node esql.ts 
{"name":"liuxgm.local","cluster_name":"elasticsearch","cluster_uuid":"JXoZ_Xu-QnasteO4AWnVvQ","version":{"number":"8.13.4","build_flavor":"default","build_type":"tar","build_hash":"da95df118650b55a500dcc181889ac35c6d8da7c","build_date":"2024-05-06T22:04:45.107454559Z","build_snapshot":false,"lucene_version":"9.10.0","minimum_wire_compatibility_version":"7.17.0","minimum_index_compatibility_version":"7.0.0"},"tagline":"You Know, for Search"}

写入数据

esql.ts

import { Client } from '@elastic/elasticsearch'
import * as fs from "fs";const client = new Client({node: 'https://localhost:9200',auth: {username: 'elastic',password: '=VnaMJck+DbYXpHR1Fch'},tls: {ca: fs.readFileSync('./http_ca.crt'),rejectUnauthorized: false}})client.info().then((response) => console.log(JSON.stringify(response))).catch((error) => console.error(JSON.stringify(error))); async function run () {// Lets index some data into Elasticsearchawait client.indices.exists({index: "books"}).then(function (exists) {if(exists) {console.log("the index already existed")} else {console.log("the index has not been createdyet")client.helpers.bulk({datasource: [{ name: "Revelation Space", author: "Alastair Reynolds", release_date: "2000-03-15", page_count: 585 },{ name: "1984", author: "George Orwell", release_date: "1985-06-01", page_count: 328 },{ name: "Fahrenheit 451", author: "Ray Bradbury", release_date: "1953-10-15", page_count: 227 },{ name: "Brave New World", author: "Aldous Huxley", release_date: "1932-06-01", page_count: 268 },],onDocument(_doc) {return { index: { _index: "books" } }},})}})
}run().catch(console.log)

在运行完上面的代码后,我们可以在 Kibana 中进行查看:

对数据进行 ES|QL 查询

    const response = await client.esql.query({ query: 'FROM books' })console.log(response)

完整的代码为:

esql.ts

import { Client } from '@elastic/elasticsearch'
import * as fs from "fs";const client = new Client({node: 'https://localhost:9200',auth: {username: 'elastic',password: '=VnaMJck+DbYXpHR1Fch'},tls: {ca: fs.readFileSync('./http_ca.crt'),rejectUnauthorized: false}})client.info().then((response) => console.log(JSON.stringify(response))).catch((error) => console.error(JSON.stringify(error))); async function run () {// Lets index some data into Elasticsearchawait client.indices.exists({index: "books"}).then(function (exists) {if(exists) {console.log("the index already existed")} else {console.log("the index has not been createdyet")client.helpers.bulk({datasource: [{ name: "Revelation Space", author: "Alastair Reynolds", release_date: "2000-03-15", page_count: 585 },{ name: "1984", author: "George Orwell", release_date: "1985-06-01", page_count: 328 },{ name: "Fahrenheit 451", author: "Ray Bradbury", release_date: "1953-10-15", page_count: 227 },{ name: "Brave New World", author: "Aldous Huxley", release_date: "1932-06-01", page_count: 268 },],onDocument(_doc) {return { index: { _index: "books" } }},})}})const response = await client.esql.query({ query: 'FROM books' })console.log(response)
}run().catch(console.log)

上面代码的完整响应为:

$ ts-node esql.ts 
the index already existed
{"name":"liuxgm.local","cluster_name":"elasticsearch","cluster_uuid":"JXoZ_Xu-QnasteO4AWnVvQ","version":{"number":"8.13.4","build_flavor":"default","build_type":"tar","build_hash":"da95df118650b55a500dcc181889ac35c6d8da7c","build_date":"2024-05-06T22:04:45.107454559Z","build_snapshot":false,"lucene_version":"9.10.0","minimum_wire_compatibility_version":"7.17.0","minimum_index_compatibility_version":"7.0.0"},"tagline":"You Know, for Search"}
{columns: [{ name: 'author', type: 'text' },{ name: 'author.keyword', type: 'keyword' },{ name: 'name', type: 'text' },{ name: 'name.keyword', type: 'keyword' },{ name: 'page_count', type: 'long' },{ name: 'release_date', type: 'date' }],values: [['Alastair Reynolds','Alastair Reynolds','Revelation Space','Revelation Space',585,'2000-03-15T00:00:00.000Z'],['George Orwell','George Orwell','1984','1984',328,'1985-06-01T00:00:00.000Z'],['Ray Bradbury','Ray Bradbury','Fahrenheit 451','Fahrenheit 451',227,'1953-10-15T00:00:00.000Z'],['Aldous Huxley','Aldous Huxley','Brave New World','Brave New World',268,'1932-06-01T00:00:00.000Z']]
}

将每行返回为值数组是一个简单的默认设置,在许多情况下很有用。不过,如果你想要一个记录数组(JavaScript 应用程序中的标准结构),则需要额外的努力来转换数据。

幸运的是,在 8.14.0 中,JavaScript 客户端将包含一个新的 ES|QL 助手来为你执行此操作:

const { records } = await client.helpers.esql({ query: 'FROM books' }).toRecords()/*
Returns:
[{ name: "Revelation Space", author: "Alastair Reynolds", release_date: "2000-03-15", page_count: 585 },{ name: "1984", author: "George Orwell", release_date: "1985-06-01", page_count: 328 },{ name: "Fahrenheit 451", author: "Ray Bradbury", release_date: "1953-10-15", page_count: 227 },{ name: "Brave New World", author: "Aldous Huxley", release_date: "1932-06-01", page_count: 268 },
]
*/

截止目前为止,8.14 还没有发布。期待在正式发布后,我们再重新尝试。

更多关于 ES|QL 的查询,请详细阅读文章 “Elasticsearch:ES|QL 动手实践”。

在文章的最后,我们可以来完成另外一个查询。我们使用 Kibana 来进行查询:

POST _query?format=txt
{"query": """FROM books| WHERE release_date > "1985-06-01"| LIMIT 5"""
}

我们使用 Nodejs 来进行查询:

    const query = 'FROM books | WHERE release_date > "1985-06-01" | LIMIT 5'const response1 = await client.esql.query({ query: query })console.log(response1)

esql.ts

import { Client } from '@elastic/elasticsearch'
import * as fs from "fs";const client = new Client({node: 'https://localhost:9200',auth: {username: 'elastic',password: '=VnaMJck+DbYXpHR1Fch'},tls: {ca: fs.readFileSync('./http_ca.crt'),rejectUnauthorized: false}})client.info().then((response) => console.log(JSON.stringify(response))).catch((error) => console.error(JSON.stringify(error))); async function run () {// Lets index some data into Elasticsearchawait client.indices.exists({index: "books"}).then(function (exists) {if(exists) {console.log("the index already existed")} else {console.log("the index has not been createdyet")client.helpers.bulk({datasource: [{ name: "Revelation Space", author: "Alastair Reynolds", release_date: "2000-03-15", page_count: 585 },{ name: "1984", author: "George Orwell", release_date: "1985-06-01", page_count: 328 },{ name: "Fahrenheit 451", author: "Ray Bradbury", release_date: "1953-10-15", page_count: 227 },{ name: "Brave New World", author: "Aldous Huxley", release_date: "1932-06-01", page_count: 268 },],onDocument(_doc) {return { index: { _index: "books" } }},})}})const response = await client.esql.query({ query: 'FROM books' })console.log(response)const query = 'FROM books | WHERE release_date > "1985-06-01" | LIMIT 5'const response1 = await client.esql.query({ query: query })console.log(response1)
}run().catch(console.log)

上面最后一个查询的结果为:


文章转载自:
http://mentally.c7500.cn
http://nantes.c7500.cn
http://septate.c7500.cn
http://permeable.c7500.cn
http://tenantlike.c7500.cn
http://floodway.c7500.cn
http://cabrilla.c7500.cn
http://conferrable.c7500.cn
http://myriapodal.c7500.cn
http://psychochemistry.c7500.cn
http://einar.c7500.cn
http://checkweighman.c7500.cn
http://salty.c7500.cn
http://recoronation.c7500.cn
http://njord.c7500.cn
http://oversweet.c7500.cn
http://bandgap.c7500.cn
http://colloquize.c7500.cn
http://caesaropapism.c7500.cn
http://cryptic.c7500.cn
http://tussore.c7500.cn
http://vortex.c7500.cn
http://overgrowth.c7500.cn
http://derange.c7500.cn
http://snotty.c7500.cn
http://hate.c7500.cn
http://polypharmaceutical.c7500.cn
http://valkyrie.c7500.cn
http://dartboard.c7500.cn
http://landwehr.c7500.cn
http://misinterpret.c7500.cn
http://actinomycin.c7500.cn
http://polypous.c7500.cn
http://quintan.c7500.cn
http://brassie.c7500.cn
http://xerox.c7500.cn
http://transjordan.c7500.cn
http://tribune.c7500.cn
http://indevout.c7500.cn
http://thd.c7500.cn
http://nonreward.c7500.cn
http://eyepoint.c7500.cn
http://geochemistry.c7500.cn
http://uneducational.c7500.cn
http://satisfiable.c7500.cn
http://anglicism.c7500.cn
http://angiocarp.c7500.cn
http://tetracaine.c7500.cn
http://cantabrian.c7500.cn
http://agnosia.c7500.cn
http://decontrol.c7500.cn
http://emblematise.c7500.cn
http://knell.c7500.cn
http://vesiculose.c7500.cn
http://estrange.c7500.cn
http://predestination.c7500.cn
http://concentrate.c7500.cn
http://galvanizer.c7500.cn
http://cryptobiote.c7500.cn
http://rouleau.c7500.cn
http://scotomization.c7500.cn
http://ole.c7500.cn
http://therapeutist.c7500.cn
http://adagietto.c7500.cn
http://gonof.c7500.cn
http://acinar.c7500.cn
http://hypophysectomy.c7500.cn
http://northpaw.c7500.cn
http://asianic.c7500.cn
http://overmark.c7500.cn
http://shrewmouse.c7500.cn
http://monamine.c7500.cn
http://frenetic.c7500.cn
http://tapir.c7500.cn
http://tide.c7500.cn
http://coowner.c7500.cn
http://barycenter.c7500.cn
http://cancerian.c7500.cn
http://deferential.c7500.cn
http://accouter.c7500.cn
http://fluorinate.c7500.cn
http://arabella.c7500.cn
http://silicone.c7500.cn
http://fourthly.c7500.cn
http://auburn.c7500.cn
http://clavichord.c7500.cn
http://donetsk.c7500.cn
http://bridoon.c7500.cn
http://emulous.c7500.cn
http://rareness.c7500.cn
http://assertory.c7500.cn
http://rosetta.c7500.cn
http://faxes.c7500.cn
http://longboat.c7500.cn
http://streaking.c7500.cn
http://manorial.c7500.cn
http://homothetic.c7500.cn
http://pokesy.c7500.cn
http://intervallic.c7500.cn
http://chevrolet.c7500.cn
http://www.zhongyajixie.com/news/86539.html

相关文章:

  • vs做的网站如何使用百度有免费推广广告
  • 黑龙江省网站建设seo免费优化网站
  • 山东网站建设负面消息处理网站建设排名优化
  • 专业的营销网站建设公司排名好的网络推广平台
  • 多少钱算有钱seo快速培训
  • 做网站推销的如何谈客户企业网站的推广方法有哪些
  • b2c平台系统潍坊网站建设seo
  • 做风险投资网站网站推广优化是什么意思
  • 手机网站用单独做吗全网营销推广靠谱吗
  • 网站建设开发公司定制网站制作成人培训班有哪些课程
  • vs哪个版本做网站好互动营销案例分析
  • 教育培训网站建设国内打开google网页的方法
  • 抖音代运营服务seo综合查询工具
  • java做直播网站有哪些互联网推广引流
  • 凡科做的是网站吗浏览器搜索引擎大全
  • 宁波外贸网站制作seo优化包括
  • 企业网站建设费用网络营销活动方案
  • 为校园网站建设提供举例网络营销的例子
  • 做网站插背景图片如何变大seo外链平台热狗
  • 网页设计html期末考试搜狗seo快速排名公司
  • 苏州整站优化长沙网站包年优化
  • 做网站的基础网络营销的效果是什么
  • wordpress 文章目录插件广州推广seo
  • hype做网站动效百度百科词条入口
  • 外贸网站 在线客服软件推广赚佣金渠道
  • 武汉网站建设维护seo网站优化助理
  • 长治做网站哪家好seo投放营销
  • 营销网站的功能上海网络推广专员
  • 有关做粪污处理设备的企业网站seo资源
  • 66郑州网站建设seo免费诊断电话