7

我目前正在编写一个应用程序,允许存储图像,然后标记这些图像。我正在使用 Python 和 Peewee ORM (http://charlesleifer.com/docs/peewee/),它与 ​​Django 的 ORM 非常相似。

我的数据模型如下所示(简化):

class Image(BaseModel):
    key = CharField()

class Tag(BaseModel):
    tag = CharField()

class TagRelationship(BaseModel):
    relImage = ForeignKeyField(Image)
    relTag   = ForeignKeyField(Tag)

现在,我从概念上了解如何查询具有给定标签集的所有图像:

SELECT Image.key
  FROM Image
INNER JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
INNER JOIN Tag
    ON TagRelationship.TagID = Tag.ID
 WHERE Tag.tag
       IN ( 'A' , 'B' )     -- list of multiple tags
GROUP BY Image.key
HAVING COUNT(*) = 2         -- where 2 == the number of tags specified, above

但是,我也希望能够进行更复杂的搜索。具体来说,我希望能够指定“所有标签”列表 - 即图像必须具有要返回的所有指定标签,以及“任何”列表和“无”列表。

编辑:我想澄清一下。具体来说,上述查询是“所有标签”式查询。它返回具有所有给定标签的图像。我希望能够指定如下内容:“给我所有带有标签(绿色、山脉)、任何一个标签(背景、风景)但没有标签(数字、绘图)的图像”。

现在,理想情况下,我希望这是一个 SQL 查询,因为使用 LIMIT 和 OFFSET 后分页变得非常容易。我实际上有一个实现工作,我只需将所有内容加载到 Python 集中,然后使用各种交集运算符。我想知道是否有一种方法可以一次完成这一切?

此外,对于那些感兴趣的人,我已经通过电子邮件向 Peewee 的作者发送了有关如何使用 Peewee 表示上述查询的电子邮件,他回复了以下解决方案:

Image.select(['key']).group_by('key').join(TagRelationship).join(Tag).where(tag__in=['tag1', 'tag2']).having('count(*) = 2')

或者,或者,更短的版本:

Image.filter(tagrelationship_set__relTag__tag__in=['tag1', 'tag2']).group_by(Image).having('count(*) = 2')

在此先感谢您的时间。

4

3 回答 3

5
SELECT Image.key
  FROM Image
  JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
  JOIN Tag
    ON TagRelationship.TagID = Tag.ID
 GROUP BY Image.key
HAVING SUM(Tag.tag IN (mandatory tags )) = N  /*the number of mandatory tags*/
   AND SUM(Tag.tag IN (optional tags  )) > 0
   AND SUM(Tag.tag IN (prohibited tags)) = 0

更新

上述查询的更普遍接受的版本(使用 CASE 表达式将 IN 谓词的布尔结果转换为整数):

SELECT Image.key
  FROM Image
  JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
  JOIN Tag
    ON TagRelationship.TagID = Tag.ID
 GROUP BY Image.key
HAVING SUM(CASE WHEN Tag.tag IN (mandatory tags ) THEN 1 ELSE 0 END) = N  /*the number of mandatory tags*/
   AND SUM(CASE WHEN Tag.tag IN (optional tags  ) THEN 1 ELSE 0 END) > 0
   AND SUM(CASE WHEN Tag.tag IN (prohibited tags) THEN 1 ELSE 0 END) = 0

或使用 COUNT 而不是 SUM:

SELECT Image.key
  FROM Image
  JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
  JOIN Tag
    ON TagRelationship.TagID = Tag.ID
 GROUP BY Image.key
HAVING COUNT(CASE WHEN Tag.tag IN (mandatory tags ) THEN 1 END) = N  /*the number of mandatory tags*/
   AND COUNT(CASE WHEN Tag.tag IN (optional tags  ) THEN 1 END) > 0
   AND COUNT(CASE WHEN Tag.tag IN (prohibited tags) THEN 1 END) = 0
于 2012-01-16T07:58:38.817 回答
2

上半部分得到与强制标签匹配的单词。下半部分是必须存在至少 1 个的标签。底部查询没有 GROUP BY 因为我想知道图像是否出现两次。如果是这样,它既有背景又有风景。ORDER BY count(*) 将使带有背景和横向标签的图片出现在顶部。因此,绿色、山脉、背景景观将是最相关的。然后是绿色、山脉、背景或风景图片。

SELECT Image.key, count(*) AS 'relevance' 
FROM
     (SELECT Image.key
      FROM
        --good image candidates
        (SELECT Image.key
         FROM Image
         WHERE Image.key NOT IN 
            --Bad Images
            (SELECT DISTINCT(Image.key)   --Will reduce size of set, remove duplicates
             FROM Image
             INNER JOIN TagRelationship
                ON Image.ID = TagRelationship.ImageID
             INNER JOIN Tag
                ON TagRelationship.TagID = Tag.ID
              WHERE Tag.tag
                   IN ('digital', 'drawing' )))
    INNER JOIN TagRelationship
        ON Image.ID = TagRelationship.ImageID
    INNER JOIN Tag
        ON TagRelationship.TagID = Tag.ID
    WHERE Tag.tag
           IN ('green', 'mountain')
    GROUP BY Image.key
    HAVING COUNT(*) = count('green', 'mountain')
    --we need green AND mountain

    UNION ALL

    --Get all images with one of the following 2 tags
    SELECT * 
    FROM
        (SELECT Image.key
         FROM Image
         INNER JOIN TagRelationship
             ON Image.ID = TagRelationship.ImageID
         INNER JOIN Tag
             ON TagRelationship.TagID = Tag.ID
          WHERE Tag.tag
             IN ( 'background' , 'landscape' ))
)
GROUP BY Image.key
ORDER BY relevance DESC
于 2012-01-16T07:08:16.497 回答
0

以下查询必须返回所有标记有('A'和'B')和('C' OR 'D')但不是'E'和'F'的图像

SELECT Image.key
FROM Image
INNER JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
INNER JOIN Tag tag1
    ON TagRelationship.TagID = tag1.ID
INNER JOIN Tag tag2
    ON TagRelationship.TagID = tag2.ID
WHERE tag1.tag
    IN ( 'A' , 'B' )
AND tag2.tag NOT IN ('E', 'F')

GROUP BY Image.key
HAVING COUNT(*) = 2 

UNION

SELECT Image.key
FROM Image
INNER JOIN TagRelationship
    ON Image.ID = TagRelationship.ImageID
INNER JOIN Tag tag1
    ON TagRelationship.TagID = tag1.ID
INNER JOIN Tag tag2
    ON TagRelationship.TagID = tag2.ID
WHERE tag1.tag
   IN ( 'C' , 'D' )
AND tag2.tag NOT IN ('E', 'F')
于 2012-01-16T07:05:45.963 回答