【问题标题】:Performing PostgreSQL match using OR on a JSON field在 JSON 字段上使用 OR 执行 PostgreSQL 匹配
【发布时间】:2017-05-31 16:43:14
【问题描述】:

我有一个看起来像这样的模型

class Message(db.Model):

    content = db.Column(JSON)

我想在包含一些 JSON 字段的字段上执行搜索

通常当我这样做时它会起作用

Message.query.filter(Message.content['summary'].cast(Unicode).match(search_term))

如果字段摘要不存在或为空,我现在想使用或其他字段

使用有或没有强制转换都会失败,类型的编程错误需要是布尔值

Message.query.filter(Message.content['summary'] | Message.content['text+video'].match(search_term)) 

【问题讨论】:

    标签: postgresql sqlalchemy flask-sqlalchemy


    【解决方案1】:

    感谢#sqlalchemy IRC 频道上的Elmer,逻辑不正确,这就是我应该执行搜索的方式

    Message.query.filter(Message.content['summary'].cast(Unicode).match(search_term) | Message.content['text'].cast(Unicode).match(search_term))
    

    有兴趣的人也应该关注这个链接,有相关问题有更好的答案SQLAlchemy Text Matching data inside JSON field with UTF-8

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2021-07-04
      • 1970-01-01
      • 2015-11-25
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多