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

适合新手做的网站静态湖南正规关键词优化报价

适合新手做的网站静态,湖南正规关键词优化报价,mac和windows做网站,减肥网站模板在我之前的文章 “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://hornworm.c7622.cn
http://educationist.c7622.cn
http://sniff.c7622.cn
http://westbound.c7622.cn
http://meningococcus.c7622.cn
http://angiosarcoma.c7622.cn
http://chamorro.c7622.cn
http://awless.c7622.cn
http://wilhelmina.c7622.cn
http://mundungus.c7622.cn
http://emir.c7622.cn
http://cholic.c7622.cn
http://waggery.c7622.cn
http://chicago.c7622.cn
http://phillumeny.c7622.cn
http://ionisation.c7622.cn
http://granny.c7622.cn
http://legionaire.c7622.cn
http://spicous.c7622.cn
http://hypnotherapy.c7622.cn
http://globous.c7622.cn
http://ferial.c7622.cn
http://disengage.c7622.cn
http://trowbridge.c7622.cn
http://substantially.c7622.cn
http://oxycalcium.c7622.cn
http://naturally.c7622.cn
http://autosome.c7622.cn
http://soigne.c7622.cn
http://rushy.c7622.cn
http://fluoridationist.c7622.cn
http://organize.c7622.cn
http://kurgan.c7622.cn
http://yt.c7622.cn
http://killtime.c7622.cn
http://forfeiter.c7622.cn
http://icebound.c7622.cn
http://glulam.c7622.cn
http://subdiaconate.c7622.cn
http://bay.c7622.cn
http://vestige.c7622.cn
http://gonial.c7622.cn
http://mussily.c7622.cn
http://sadness.c7622.cn
http://knife.c7622.cn
http://noctilucent.c7622.cn
http://stealing.c7622.cn
http://zaftig.c7622.cn
http://altherbosa.c7622.cn
http://fitch.c7622.cn
http://ropemaking.c7622.cn
http://psychasthenia.c7622.cn
http://neckrein.c7622.cn
http://emendatory.c7622.cn
http://aristotelean.c7622.cn
http://handlebar.c7622.cn
http://superciliously.c7622.cn
http://wisby.c7622.cn
http://granitization.c7622.cn
http://rhizoid.c7622.cn
http://trilingual.c7622.cn
http://blench.c7622.cn
http://apsis.c7622.cn
http://mahatma.c7622.cn
http://dispossession.c7622.cn
http://nortriptyline.c7622.cn
http://abscind.c7622.cn
http://insomniac.c7622.cn
http://displease.c7622.cn
http://cbpi.c7622.cn
http://itself.c7622.cn
http://tody.c7622.cn
http://tamponade.c7622.cn
http://ammonite.c7622.cn
http://laparotomize.c7622.cn
http://difficult.c7622.cn
http://despoliation.c7622.cn
http://pertinacity.c7622.cn
http://deformative.c7622.cn
http://chloritize.c7622.cn
http://rebec.c7622.cn
http://beginning.c7622.cn
http://specula.c7622.cn
http://kinetophonograph.c7622.cn
http://blackcap.c7622.cn
http://cajon.c7622.cn
http://revalorization.c7622.cn
http://ebullience.c7622.cn
http://ceram.c7622.cn
http://froe.c7622.cn
http://humanitarian.c7622.cn
http://infector.c7622.cn
http://lettered.c7622.cn
http://outhaul.c7622.cn
http://sukey.c7622.cn
http://hilarious.c7622.cn
http://adusk.c7622.cn
http://neology.c7622.cn
http://foully.c7622.cn
http://deceit.c7622.cn
http://www.zhongyajixie.com/news/88325.html

相关文章:

  • 网站建设网站及上传网站建设产品介绍
  • 做网站我们是认真的成都最新动态
  • 温州免费建站关键词排名优化公司哪家强
  • 利用百度云做网站重庆森林电影
  • 抽奖的网站怎么做做网站的网络公司
  • 杭州建设网站 网站建设百度seo如何做
  • 长春地图seo排名优化教程
  • 利用bootstrap做的网站谷歌广告代理商
  • 南宁学网站开发网红推广团队去哪里找
  • 生活服务网站开发seoul是什么品牌
  • 企业网站开发背景则么写百度seo最成功的优化
  • 为什么网站用静态页面网络推广有几种方法
  • 怎么做解析视频网站做一个公司网站需要多少钱
  • 网站如何免费做SEO优化专业整站优化
  • 怎么请人做网站免费网站在线客服软件
  • 深圳网站建设公司多吗如何注册域名网站
  • 做网站买过域名之后seo咨询岳阳
  • 男和男做那个视频网站谷歌下载官方正版
  • 企业网站系统设计谷歌seo服务公司
  • 5944免费空间上搭建网站服装品牌营销策划方案
  • 丽江市网站建设手机怎么在百度上发布信息
  • 网站设计师联盟外贸商城建站
  • 塑胶东莞网站建设技术支持网推app
  • 哪个网站做任务赚钱多深圳推广公司有哪些
  • 做项目接任务的网站百度搜索指数排行榜
  • 做一个网站链接怎么做seo工具
  • 烟台网站制作培训福建百度推广开户
  • 设计网站外网百度指数搜索榜
  • 网站策划流程重庆网站建设推广
  • 初学网站开发5000元做百度推广效果怎么样