我正在尝试使用一组文档以及一个表示基于其父级的文档路径的字段来填充 ElasticSearch。
这是我的表格布局:
+----+--------+-------+----------+
| Id | Parent | Alias | Contents |
+----+--------+-------+----------+
| 1 | null | Doc1 | Admin |
| 2 | 1 | Doc2 | Use |
| 3 | 2 | Doc3 | Test |
| 4 | 3 | Doc4 | Ask |
| 5 | null | PDF1 | Intro |
| 6 | 5 | PDF2 | Managers |
+----+--------+-------+----------+
这是所需的输出
+----+--------+-------+----------+---------------------+
| Id | Parent | Alias | Contents | Path |
+----+--------+-------+----------+---------------------+
| 1 | null | Doc1 | Admin | Doc1 |
| 2 | 1 | Doc2 | Use | Doc1\Doc2 |
| 3 | 2 | Doc3 | Test | Doc1\Doc2\Doc3 |
| 4 | 3 | Doc4 | Ask | Doc1\Doc2\Doc3\Doc4 |
| 5 | null | PDF1 | Intro | PDF1 |
| 6 | 5 | PDF2 | Managers | PDF1\PDF2 |
+----+--------+-------+----------+---------------------+
我有这个查询,它获取由参数@child 指定的一个文档的路径;(又名SET @child = 5;
)
SELECT
T2.*
FROM
(SELECT
@r AS _id,
(SELECT
@r:=Parent
FROM
documents
WHERE
id = _id) AS ParentId,
@l:=@l + 1 AS lvl
FROM
(SELECT @r:=@child, @l:=@parent) vars, documents
WHERE
@r <> 0) T1
JOIN
documents T2 ON T1._id = T2.Id
ORDER BY T2.Parent
问题是如果我把它放到子查询中,我该如何设置@child?我已经尝试过 GROUP_CONCAT() 但它总是最终成为每一行的相同路径。我尝试将当前行的 Id 放在子查询中,但它会引发错误:ErrorCode: 1109. Unknown table 'doc' in field list
在以下查询中
SELECT doc.*, (
SELECT GROUP_CONCAT(a.Alias) FROM (SELECT
T2.*
FROM
(SELECT
@r AS _id,
(SELECT
@r:=Parent
FROM
documents
WHERE
id = _id) AS ParentId,
@l:=@l + 1 AS lvl
FROM
(SELECT @r:= doc.Id, @l:=@parent) vars, documents
WHERE
@r <> 0) T1
JOIN
documents T2 ON T1._id = T2.Id
ORDER BY T1.lvl DESC) a
) as Path FROM documents doc
我究竟做错了什么?有没有更好的方法来做到这一点,我没有看到?
虽然它并不完全相关,但我会指出,我正在使用 logstash 脚本按计划将文档从我的数据库加载到 ElasticSearch 中。同样出于多重考虑,我已经删除了大部分列以及内容并替换为虚假内容。