json中具体节点的全文搜索

json中具体节点的全文搜索,json,postgresql,Json,Postgresql,我的表“产品””有两列: Id-Bigint主键 数据-Jsonb 下面是json的示例: { "availability": [ { "qty": 10, "price": 42511, "store": { "name": "my_best_store", "hours":

我的表“产品””有两列:

  • Id-Bigint主键
  • 数据-Jsonb
  • 下面是json的示例:

    {
      "availability": [
        {
          "qty": 10,
          "price": 42511,
          "store": {
            "name": "my_best_store",
            "hours": null,
            "title": {
              "en": null
            },
            "coords": null,
            "address": null,
    
    我在列“数据”中插入json

    这里sql get find“我的最佳商店

    很好。很好

    但我只需要在“可用性”部分找到“我最好的商店”

    我尝试此操作,但结果为空:

    select *
    from product
    where to_tsvector(product.data) @@ to_tsquery('availability & my_best_store')
    

    假设要在“名称”属性中搜索,可以执行以下操作:

    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'))
    
    使用Postgres 12,您可以将其简化为:

    select p.*
    from product p
    where to_tsvector(jsonb_path_query_array(data, '$.availability[*].store.name')) @@ to_tsquery('my_best_store')
    

    to_tsvector(product.data->“availability”)@…
    ?@一匹没有名字的马,但我还需要在“availability.store”部分找到。这没有帮助:to_tsvector(product.data->“availability”->“store”)@@n您只想在
    availability
    中搜索第一个数组元素还是所有数组元素?您使用的是哪个Postgres版本?@a_horse_,在所有数组元素中没有名称。Postgres 9.6版
    至\u tsvector(product.data->“availability”)@@to \u tsquery(“我的最佳商店”)
    在我上一篇评论中
    select p.*
    from product p
    where to_tsvector(jsonb_path_query_array(data, '$.availability[*].store.name')) @@ to_tsquery('my_best_store')