gpt4 book ai didi

node.js - 如何使用Node.js将数据推送到ElasticSearch中的指定索引

转载 作者:行者123 更新时间:2023-12-03 01:00:55 25 4
gpt4 key购买 nike

我正在开发聊天应用程序,我需要使用nodejs将聊天信息推送到elasticsearch以获得指定的索引。

我创建了索引,并使用以下代码将数据推送到elasticsearch

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

client.ping({
requestTimeout: 30000,

// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});



var result = client.index({
index: 'chennaiboxchat',
type: 'posts',
id: '1',
body: {
"glossary": {
"title": "example Test",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "mark"
}
}
}
}
},
refresh: true
});

但是,如果下次运行此代码会覆盖data.so,则需要使用Node.js在elasticsearch中更新同一索引中的数据。建议我如何更新单个索引中的数据。

最佳答案

默认情况下,您必须提供完整的文档。因此,使用get获取文档,更改要更改的字段,然后像以前一样放回文档。有一个更新API仅提供部分文档。这实际上是一样的,但是elasticsearch会为您完成get和put部分。如果确实要执行此操作,则通常需要使用version属性进行操作。更多信息可以在这里找到:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

node.js使用的javascript驱动程序也支持更新终结点。在此处查看文档:
https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

关于node.js - 如何使用Node.js将数据推送到ElasticSearch中的指定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37676514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com