【发布时间】:2015-08-20 00:52:08
【问题描述】:
我目前正在使用 PostgreSQL 中的 NOSQL 数据库。它包含 3 个表,分别具有 id、data (jsonb) 和 tsv (tsvector) 字段。
我在从数据字段创建 tsvector 时遇到了一些困难。使用 jsonb 对象我没有任何问题:
对于这样的事情:
{"genres":{"genre": "Electronic"},
"labels": {"label": {"@name": "Turbo", "@catno": "HEMI-06PU"}},
"styles": {"style": ["House", "Techno", "Tech House"]}}
我用过:
UPDATE rel SET tsv = setweight(to_tsvector(coalesce(data->>'genre','')),'B') ||
setweight(to_tsvector(coalesce(data->'labels'->'label'->>'@name','')),'C') ||
setweight(to_tsvector(coalesce(data->'styles'->>'style','')),'D');
但现在我也有像这样的嵌套结构:
"tracklist": {"track": [
{"title": "Untitled",
"artists": {"artist": {"id": "676", "anv": null, "join": ",", "name": "Peter Benisch", "role": null, "tracks": null}},
"duration": "5:42",
"position": "1"},
{"title": "Good Morning",
"artists": {"artist": {"id": "166", "anv": null, "join": ",", "name": "Fred Everything", "role": null, "tracks": null}},
"duration": "4:10",
"position": "2"}]
我正在尝试将“track”-array 中所有曲目的每个标题放入我的 tsvector。整个表格中的曲目数量可能不同。
我尝试过使用
(jsonb_array_elements(data->'tracklist'->'track'))->>'title'
这给了我每行一个标题。
谁能帮助获取这些行作为 to_tsvector() 函数的输入?或者有什么不同的解决方案?
【问题讨论】:
标签: json postgresql full-text-search tsvector nosql