[英]Full text search in concrete node in json
I has table " Product " with two columns:我有表“产品”有两列:
Here example of json:这里以 json 为例:
{
"availability": [
{
"qty": 10,
"price": 42511,
"store": {
"name": "my_best_store",
"hours": null,
"title": {
"en": null
},
"coords": null,
"address": null,
I insert json to column " data ".我将 json 插入“数据”列。
Here sql get find " my_best_store "在这里 sql 找到“ my_best_store ”
select *
from product
where to_tsvector(product.data) @@ to_tsquery('my_best_store')
Nice.好的。 It's work fine.它工作正常。
But I need to find " my_best_store " only in section " availability ".但我只需要在“可用性”部分找到“ my_best_store ”。
I try this but result is empty:我试试这个,但结果是空的:
select *
from product
where to_tsvector(product.data) @@ to_tsquery('availability & my_best_store')
Assuming you want to search in the name attribute, you can do the following:假设您要在 name 属性中进行搜索,您可以执行以下操作:
select p.*
from product p
where exists (select *
from jsonb_array_elements(p.data -> 'availability') as t(item)
where to_tsvector(t.item -> 'store' ->> 'name') @@ to_tsquery('my_best_store'))
With Postgres 12, you can simplify that to:使用 Postgres 12,您可以将其简化为:
select p.*
from product p
where to_tsvector(jsonb_path_query_array(data, '$.availability[*].store.name')) @@ to_tsquery('my_best_store')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.