server/search: simplify virtual token mappings
This commit is contained in:
parent
7334f70829
commit
c957befbd0
3 changed files with 33 additions and 60 deletions
|
@ -58,13 +58,9 @@ class Post(Base):
|
||||||
secondaryjoin=post_id == PostRelation.child_id)
|
secondaryjoin=post_id == PostRelation.child_id)
|
||||||
|
|
||||||
tag_count = column_property(
|
tag_count = column_property(
|
||||||
select(
|
select([func.count('1')])
|
||||||
[func.count('1')],
|
.where(PostTag.post_id == post_id) \
|
||||||
PostTag.post_id == post_id
|
.correlate('Post'))
|
||||||
) \
|
|
||||||
.correlate('Post') \
|
|
||||||
.label('tag_count')
|
|
||||||
)
|
|
||||||
|
|
||||||
# TODO: wire these
|
# TODO: wire these
|
||||||
fav_count = Column('auto_fav_count', Integer, nullable=False, default=0)
|
fav_count = Column('auto_fav_count', Integer, nullable=False, default=0)
|
||||||
|
|
|
@ -55,10 +55,25 @@ class Tag(Base):
|
||||||
secondaryjoin=tag_id == TagImplication.child_id)
|
secondaryjoin=tag_id == TagImplication.child_id)
|
||||||
|
|
||||||
post_count = column_property(
|
post_count = column_property(
|
||||||
select(
|
select([func.count('Post.post_id')]) \
|
||||||
[func.count('Post.post_id')],
|
.where(PostTag.tag_id == tag_id) \
|
||||||
PostTag.tag_id == tag_id
|
.correlate('Tag'))
|
||||||
) \
|
|
||||||
.correlate('Tag') \
|
first_name = column_property(
|
||||||
.label('post_count')
|
select([TagName.name]) \
|
||||||
)
|
.where(TagName.tag_id == tag_id) \
|
||||||
|
.limit(1) \
|
||||||
|
.as_scalar(),
|
||||||
|
deferred=True)
|
||||||
|
|
||||||
|
suggestion_count = column_property(
|
||||||
|
select([func.count(TagSuggestion.child_id)]) \
|
||||||
|
.where(TagSuggestion.parent_id == tag_id) \
|
||||||
|
.as_scalar(),
|
||||||
|
deferred=True)
|
||||||
|
|
||||||
|
implication_count = column_property(
|
||||||
|
select([func.count(TagImplication.child_id)]) \
|
||||||
|
.where(TagImplication.parent_id == tag_id) \
|
||||||
|
.as_scalar(),
|
||||||
|
deferred=True)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import sqlalchemy
|
|
||||||
from sqlalchemy.sql.expression import func
|
from sqlalchemy.sql.expression import func
|
||||||
from szurubooru import db
|
from szurubooru import db
|
||||||
from szurubooru.search.base_search_config import BaseSearchConfig
|
from szurubooru.search.base_search_config import BaseSearchConfig
|
||||||
|
@ -8,11 +7,11 @@ class TagSearchConfig(BaseSearchConfig):
|
||||||
return session.query(db.Tag)
|
return session.query(db.Tag)
|
||||||
|
|
||||||
def finalize_query(self, query):
|
def finalize_query(self, query):
|
||||||
return query.order_by(self._first_name_subquery.asc())
|
return query.order_by(db.Tag.first_name.asc())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def anonymous_filter(self):
|
def anonymous_filter(self):
|
||||||
return self._name_filter
|
return self._create_str_filter(db.Tag.first_name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def special_filters(self):
|
def special_filters(self):
|
||||||
|
@ -21,7 +20,7 @@ class TagSearchConfig(BaseSearchConfig):
|
||||||
@property
|
@property
|
||||||
def named_filters(self):
|
def named_filters(self):
|
||||||
return {
|
return {
|
||||||
'name': self._name_filter,
|
'name': self._create_str_filter(db.Tag.first_name),
|
||||||
'category': self._create_str_filter(db.Tag.category),
|
'category': self._create_str_filter(db.Tag.category),
|
||||||
'creation-date': self._create_date_filter(db.Tag.creation_time),
|
'creation-date': self._create_date_filter(db.Tag.creation_time),
|
||||||
'creation-time': self._create_date_filter(db.Tag.creation_time),
|
'creation-time': self._create_date_filter(db.Tag.creation_time),
|
||||||
|
@ -32,15 +31,15 @@ class TagSearchConfig(BaseSearchConfig):
|
||||||
'usages': self._create_num_filter(db.Tag.post_count),
|
'usages': self._create_num_filter(db.Tag.post_count),
|
||||||
'usage-count': self._create_num_filter(db.Tag.post_count),
|
'usage-count': self._create_num_filter(db.Tag.post_count),
|
||||||
'post-count': self._create_num_filter(db.Tag.post_count),
|
'post-count': self._create_num_filter(db.Tag.post_count),
|
||||||
'suggestion-count': self._suggestion_count_filter,
|
'suggestion-count': self._create_num_filter(db.Tag.suggestion_count),
|
||||||
'implication-count': self._implication_count_filter,
|
'implication-count': self._create_num_filter(db.Tag.implication_count),
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def order_columns(self):
|
def order_columns(self):
|
||||||
return {
|
return {
|
||||||
'random': (func.random(), None),
|
'random': (func.random(), None),
|
||||||
'name': (self._first_name_subquery, self.ORDER_ASC),
|
'name': (db.Tag.first_name, self.ORDER_ASC),
|
||||||
'category': (db.Tag.category, self.ORDER_ASC),
|
'category': (db.Tag.category, self.ORDER_ASC),
|
||||||
'creation-date': (db.Tag.creation_time, self.ORDER_DESC),
|
'creation-date': (db.Tag.creation_time, self.ORDER_DESC),
|
||||||
'creation-time': (db.Tag.creation_time, self.ORDER_DESC),
|
'creation-time': (db.Tag.creation_time, self.ORDER_DESC),
|
||||||
|
@ -51,43 +50,6 @@ class TagSearchConfig(BaseSearchConfig):
|
||||||
'usages': (db.Tag.post_count, self.ORDER_DESC),
|
'usages': (db.Tag.post_count, self.ORDER_DESC),
|
||||||
'usage-count': (db.Tag.post_count, self.ORDER_DESC),
|
'usage-count': (db.Tag.post_count, self.ORDER_DESC),
|
||||||
'post-count': (db.Tag.post_count, self.ORDER_DESC),
|
'post-count': (db.Tag.post_count, self.ORDER_DESC),
|
||||||
'suggestion-count':
|
'suggestion-count': (db.Tag.suggestion_count, self.ORDER_DESC),
|
||||||
(self._suggestion_count_subquery, self.ORDER_DESC),
|
'implication-count': (db.Tag.implication_count, self.ORDER_DESC),
|
||||||
'implication-count':
|
|
||||||
(self._implication_count_subquery, self.ORDER_DESC),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def _name_filter(self, query, criterion):
|
|
||||||
str_filter = self._create_str_filter(db.TagName.name)
|
|
||||||
return query.filter(
|
|
||||||
db.Tag.tag_id.in_(
|
|
||||||
str_filter(query.session.query(db.TagName.tag_id), criterion)))
|
|
||||||
|
|
||||||
def _suggestion_count_filter(self, query, criterion):
|
|
||||||
return query.filter(
|
|
||||||
self._apply_num_criterion_to_column(
|
|
||||||
self._suggestion_count_subquery, criterion))
|
|
||||||
|
|
||||||
def _implication_count_filter(self, query, criterion):
|
|
||||||
return query.filter(
|
|
||||||
self._apply_num_criterion_to_column(
|
|
||||||
self._implication_count_subquery, criterion))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def _first_name_subquery(self):
|
|
||||||
return sqlalchemy.select([db.TagName.name]) \
|
|
||||||
.limit(1) \
|
|
||||||
.where(db.TagName.tag_id == db.Tag.tag_id) \
|
|
||||||
.as_scalar()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def _suggestion_count_subquery(self):
|
|
||||||
return sqlalchemy.select([func.count(db.TagSuggestion.child_id)]) \
|
|
||||||
.where(db.TagSuggestion.parent_id == db.Tag.tag_id) \
|
|
||||||
.as_scalar()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def _implication_count_subquery(self):
|
|
||||||
return sqlalchemy.select([func.count(1)]) \
|
|
||||||
.where(db.TagImplication.parent_id == db.Tag.tag_id) \
|
|
||||||
.as_scalar()
|
|
||||||
|
|
Loading…
Reference in a new issue