cube.js 集成 elasticsearch 的一种变通方法
前边有介绍过cube.js 对于es 的支持(以及关于opendistro elasticsearch的bug)实际上我们可以通过
postgres 的elasticsearch-fdw 迂回式的解决此问题,对于需要的分析都可以通过pg 的fdw 操作解决
环境准备
- docker-compose 文件
version: "3"
services:
elasticsearch:
image: elasticsearch:7.6.0
environment:
- "discovery.type=single-node"
- "http.host=0.0.0.0"
- "cluster.name=odfe-cluster"
- "transport.host=0.0.0.0"
- "network.host=0.0.0.0"
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9200:9200
psotgres-fdw:
image: dalongrong/pg-es-fdw:11
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
pg:
image: dalongrong/pgspider:pg_cron
ports:
- "5433:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
CREATE EXTENSION multicorn;
CREATE SERVER multicorn_es FOREIGN DATA WRAPPER multicorn
OPTIONS (
wrapper 'pg_es_fdw.ElasticsearchFDW'
);
CREATE FOREIGN TABLE articles_es
(
id BIGINT,
title TEXT,
body TEXT,
query TEXT,
score NUMERIC
)
SERVER multicorn_es
OPTIONS
(
host 'elasticsearch',
port '9200',
index 'article-index',
type 'article',
rowid_column 'id',
query_column 'query',
score_column 'score',
timeout '20'
)
;
INSERT INTO articles_es
(
id,
title,
body
)
VALUES
(
1,
'foo',
'spike'
);
create extension postgres_fdw;
CREATE SERVER pg_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'psotgres-fdw', dbname 'postgres');
CREATE USER MAPPING FOR postgres
SERVER pg_server
OPTIONS (user 'postgres', password 'dalong');
CREATE SCHEMA app;
IMPORT FOREIGN SCHEMA public
FROM SERVER pg_server
INTO app;
cube.js 项目
很简单就是postgres 的数据类型,配置好环境变量
- 查询效果
参考资料
https://github.com/rongfengliang/es-fdw-learning
https://github.com/matthewfranglen/postgres-elasticsearch-fdw
https://www.cnblogs.com/rongfengliang/p/12360025.html