前边有说明,cube.js 已经es 以下是通过opendistro的一个简单的试用(没跑通,后边有时间可以尝试下找下原因)
opendistro 环境准备
- docker-compose 文件
注意es 版本,目前cube.js 是7.6 的es npm 包,所以最好是兼容的,因为部分版本还是有兼容问题的
version: "3"
services:
elasticsearch:
image: amazon/opendistro-for-elasticsearch:1.4.0
ports:
- "9200:9200"
environment:
- "discovery.type=single-node"
- "http.host=0.0.0.0"
- "opendistro_security.ssl.http.enabled=false"
- "cluster.name=odfe-cluster"
- "transport.host=0.0.0.0"
- "network.host=0.0.0.0"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- 启动
docker-compose up -d
- 测试数据
使用了官方的demo,注意官方的索引命名有问题,不符合cube 的约定,会有bug
初始化es 项目
yarn init -y
yarn add @elastic/elasticsearch@7.6
代码
'use strict'
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://admin:admin@localhost:9200' })
async function run () {
// Let's start by indexing some data
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
})
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Daenerys Targaryen',
quote: 'I am the blood of the dragon.'
}
})
await client.index({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
character: 'Tyrion Lannister',
quote: 'A mind needs books like a sword needs a whetstone.'
}
})
// here we are forcing an index refresh, otherwise we will not
// get any result in the consequent search
await client.indices.refresh({ index: 'game_of_thrones' })
// Let's search!
const { body } = await client.search({
index: 'game_of_thrones',
// type: '_doc', // uncomment this line if you are using Elasticsearch ≤ 6
body: {
query: {
match: { quote: 'winter' }
}
}
})
console.log(body.hits.hits)
}
run().catch(console.log)
package.json
{
"name": "elasticsearch-nodejs",
"version": "1.0.0",
"main": "app.js",
"license": "MIT",
"dependencies": {
"@elastic/elasticsearch": "7.6"
},
"scripts": {
"start":"node app.js"
}
}
运行
yarn start
效果
cube.js 集成es
- 创建项目
注意-d 为odelasticsearch 实际上应该是opendistro 的简称
cubejs create es -d odelasticsearch
- 配置环境变量
.env 文件
CUBEJS_DB_URL=http://admin:admin@localhost:9200
CUBEJS_WEB_SOCKETS=true
CUBEJS_DB_TYPE=odelasticsearch
CUBEJS_API_SECRET=08ad6563349e97296917798f5206cae5f865639114412149d2d9b8f318bb14c2d4eb4c6ce97d3b4a8a7e82abee161daaf2b1ca98003433600ca5178a6fb94b31
- 启动
yarn dev
- 效果
之后需要生成schema,方便测试
bug 说明
当前对于opendistro elasticsearch bug 还是比较多的,比如map undefined
以及生成的schema ,默认带了main 是有问题的
当然在查看官方文档的时候也看到了最近的一个变动,还是需要好好研究下cube.js 的内部机制,已经api gateway 的原理
还是比较期待官方对于opendistro elasticsearch 比较完备的支持的。
参考资料
https://opendistro.github.io/for-elasticsearch-docs/version-history/
https://github.com/elastic/elasticsearch-js
https://www.npmjs.com/package/@elastic/elasticsearch
https://github.com/cube-js/cube.js/blob/master/packages/cubejs-elasticsearch-driver/driver/ElasticSearchDriver.js