微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

弹性搜索和 nodejs 连接

如何解决弹性搜索和 nodejs 连接

我正在尝试使用 rest api 创建一个 node.js 应用程序来查询弹性搜索应用程序云上存在的数据。我遵循了弹性搜索连接的代码

var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
    host:'localhost:9200'
});

上述连接正确连接,如果我添加任何数据,它也会被添加..但我希望数据不被添加到本地主机..我希望我的实际集群拥有数据.. 我试过下面的代码

var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
    cloud: {
        id: 'cloud_id',},auth: {
        username: "username",password: "pass",});

//data I am trying to add

let data = {
    "id": "park_somethign","title": "Something","description": "Split into the separate Rincon Mountain and Tucson Mountain districts,this park is evidence that the dry Sonoran Desert is still home to a great variety of life spanning six biotic communities. Beyond the namesake giant saguaro cacti,there are barrel cacti,chollas,and prickly pears,as well as lesser long-nosed bats,spotted owls,and javelinas.","nps_link": "https://www.nps.gov/sagu/index.htm","states": [
        "Arizona"
    ],"visitors": 838456,"world_heritage_site": false,"location": "32.20,-109.5","acres": 9215.72,"square_km": 2271.2,"date_established": "1997-10-14T05:00:00Z"
}

//this is what I am trying to do
 client.index({
        index: 'engines',body: data
    }).then(resp => {
        return res.status(200).json({
            message: "Added Data"
        })
    }).catch(e => {
        console.log(e)
        return res.status(500).json({
            message: "Error",e
        })
    })

上面的代码仍然没有添加数据或从我的云集群中检索数据... 在互联网上的任何地方我只找到了 localhost 示例.. 请谁能告诉我如何将 nodejs 直接连接到弹性搜索云集群?

解决方法

确保使用 Elastic Cloud UI 提供的 Cloud ID:

enter image description here

然后使用创建部署时创建的凭据。或者,您也可以创建 API 密钥来进行身份验证:

const { Client } = require('@elastic/elasticsearch')

const client = new Client({
  cloud: {
    id: 'name:bG9jYWxob3N0JGFiY2QkZWZnaA==',},auth: {
    apiKey: {
      id: 'foo',api_key: 'bar'
    }
  }
})

此处的更多信息:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html

,

我有同样的问题。因此,连接到企业搜索的主机 url 将起作用。像这样


var client = new elasticsearch.Client({
// do not forget to add the username and password in the url
  host:"https://<username>:<password>[complete_host_url]" 
}); 

如果这不起作用,请尝试node:"https://<username>:<password>@<ip1><port>"

您还可以通过将 url 作为主机 url 数组提供来一次连接到多个主机。

Refer This

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?