mirror of
https://github.com/janeczku/calibre-web
synced 2024-09-27 14:48:22 +00:00
Changed the quick search behavior so that it allows title and author in the same query.
Also word order does not matter anymore. +some more comments
This commit is contained in:
parent
5233f78d03
commit
4b2e7b883b
40
cps/db.py
40
cps/db.py
@ -887,14 +887,12 @@ class CalibreDB:
|
|||||||
self.session.connection().connection.connection.create_function("lower", 1, lcase)
|
self.session.connection().connection.connection.create_function("lower", 1, lcase)
|
||||||
q = list()
|
q = list()
|
||||||
#splits search term into single words
|
#splits search term into single words
|
||||||
author_terms = re.split("[, ]+", term)
|
words = re.split("[, ]+", term)
|
||||||
|
#put the longest words first to make queries more efficient
|
||||||
|
words.sort(key=len,reverse=True)
|
||||||
#search authors for match
|
#search authors for match
|
||||||
for author_term in author_terms:
|
for word in words:
|
||||||
q.append(Books.authors.any(func.lower(Authors.name).ilike("%" + author_term + "%")))
|
q.append(Books.authors.any(func.lower(Authors.name).ilike("%" + word + "%")))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
query = self.generate_linked_query(config.config_read_column, Books)
|
query = self.generate_linked_query(config.config_read_column, Books)
|
||||||
if len(join) == 6:
|
if len(join) == 6:
|
||||||
@ -906,25 +904,29 @@ class CalibreDB:
|
|||||||
elif len(join) == 1:
|
elif len(join) == 1:
|
||||||
query = query.outerjoin(join[0])
|
query = query.outerjoin(join[0])
|
||||||
|
|
||||||
|
filter_expression=[]
|
||||||
cc = self.get_cc_columns(config, filter_config_custom_read=True)
|
cc = self.get_cc_columns(config, filter_config_custom_read=True)
|
||||||
|
|
||||||
#search each category for exact matches with the tag
|
|
||||||
filter_expression = [Books.tags.any(func.lower(Tags.name).ilike("%" + term + "%")),
|
|
||||||
Books.series.any(func.lower(Series.name).ilike("%" + term + "%")),
|
|
||||||
Books.authors.any(and_(*q)),
|
|
||||||
Books.publishers.any(func.lower(Publishers.name).ilike("%" + term + "%")),
|
|
||||||
func.lower(Books.title).ilike("%" + term + "%")]
|
|
||||||
|
|
||||||
|
|
||||||
for c in cc:
|
for c in cc:
|
||||||
if c.datatype not in ["datetime", "rating", "bool", "int", "float"]:
|
if c.datatype not in ["datetime", "rating", "bool", "int", "float"]:
|
||||||
filter_expression.append(
|
filter_expression.append(
|
||||||
getattr(Books,
|
getattr(Books,
|
||||||
'custom_column_' + str(c.id)).any(
|
'custom_column_' + str(c.id)).any(
|
||||||
func.lower(cc_classes[c.id].value).ilike("%" + term + "%")))
|
func.lower(cc_classes[c.id].value).ilike("%" + term + "%"))) #TODO ?
|
||||||
|
# filter out multiple languages and archived books,
|
||||||
|
results=query.filter(self.common_filters(True))
|
||||||
|
|
||||||
#filter out multiple languages and archived books, then return all that match at least one of filter_expression
|
for word in words:
|
||||||
return query.filter(self.common_filters(True)).filter(or_(*filter_expression))
|
filter_expression=[
|
||||||
|
Books.tags.any(func.lower(Tags.name).ilike("%" + word + "%")),
|
||||||
|
Books.series.any(func.lower(Series.name).ilike("%" + word + "%")),
|
||||||
|
#change to or_ to allow mix of title and author in query term
|
||||||
|
Books.authors.any(or_(*q)),
|
||||||
|
Books.publishers.any(func.lower(Publishers.name).ilike("%" + word + "%")),
|
||||||
|
func.lower(Books.title).ilike("%" + word + "%")
|
||||||
|
]
|
||||||
|
results=results.filter(or_(*filter_expression))
|
||||||
|
|
||||||
|
return results
|
||||||
|
|
||||||
def get_cc_columns(self, config, filter_config_custom_read=False):
|
def get_cc_columns(self, config, filter_config_custom_read=False):
|
||||||
tmp_cc = self.session.query(CustomColumns).filter(CustomColumns.datatype.notin_(cc_exceptions)).all()
|
tmp_cc = self.session.query(CustomColumns).filter(CustomColumns.datatype.notin_(cc_exceptions)).all()
|
||||||
|
Loading…
Reference in New Issue
Block a user