def search(self): terms = request.args.get('search') q = db.session.query(Content).filter('content.search_vector '\ '@@ plainto_tsquery(:sterms)') q = q.params(sterms=terms) q = q.add_column(func.ts_headline('pg_catalog.english', Content.content, func.plainto_tsquery(terms), 'MaxFragments=5,FragmentDelimiter=|||,'\ 'StartSel="<span class=""shighlight"">", '\ 'StopSel = "</span>", ', )) q = q.add_column(func.ts_headline('pg_catalog.english', Content.title, func.plainto_tsquery(terms), 'HighlightAll=TRUE, '\ 'StartSel="<span class=""shighlight"">", '\ 'StopSel = "</span>"')) q = q.order_by('ts_rank_cd(content.search_vector, '\ 'plainto_tsquery(:sterms)) DESC') results = [(entry, fragments.split('|||'), title) for entry, fragments, title in q] return render_template('content/search.html', term=terms, results=results)
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k, v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline( self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append( _entity_descriptor(model, 'info')[k].astext == v) else: info = json.dumps(info) clauses.append( cast(_entity_descriptor(model, 'info'), Text) == info) return clauses, headlines, order_by_ranks
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k,v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline(self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append(_entity_descriptor(model, 'info')[k].astext == v) else: info = json.dumps(info) clauses.append(cast(_entity_descriptor(model, 'info'), Text) == info) return clauses, headlines, order_by_ranks
def mangle(self, results): return results.add_columns( func.ts_headline( 'pg_catalog.english', func.strip_tags(Content.content), func.plainto_tsquery(self.text), 'MaxFragments=3, FragmentDelimiter=" ... ", StartSel="<b>", StopSel="</b>", MinWords=7, MaxWords=15', type_=Unicode).label("content_short"))
def search(): results = [] form = SearchForm(request.form) if form.validate_on_submit(): search_terms = form.search_terms.data q = db_session.query(Search).\ filter('search.search_vector @@ plainto_tsquery(:terms)') q = q.params(terms=search_terms) q = q.add_column(func.ts_headline('pg_catalog.english', Search.text, func.plainto_tsquery(search_terms), 'MaxFragments=3,FragmentDelimiter=|||,' 'StartSel="<span class=""search-highlight"">", ' 'StopSel = "</span>", ', type_= Unicode)) q = q.add_column(func.ts_headline('pg_catalog.english', Search.title, func.plainto_tsquery(search_terms), 'HighlightAll=TRUE, ' 'StartSel="<span class=""search-title-highlight"">", ' 'StopSel = "</span>"', type_= Unicode)) q = q.order_by( 'ts_rank_cd(search.search_vector, plainto_tsquery(:terms)) DESC') q = q.limit(10) results=[{ 'object': db_session.query(getattr(getattr(getattr( chicagoclimateonline, SEARCH_MODELS[entry.object]), 'models'), entry.object.capitalize())).get(entry.object_id), 'fragments': fragments.split('|||'), 'title': title, 'module': SEARCH_MODELS[entry.object], 'class': entry.object } for entry, fragments, title in q] flash('Your results for <b>{}</b>'.format(search_terms)) else: flash('''Terribly sorry, but it seems that there was a problem with the search terms you entered.''') return render_template( 'etc/search.html', # search_terms=search_terms, results=results, search_form=form, )
def text_search_by_phrase(self, phrase: str) -> list: with session_scope(self.get_session) as session: query_func = func.phraseto_tsquery(phrase, postgresql_regconfig='english') results = session.query(Tournament) \ .filter(Tournament.tsv.op('@@')(query_func)).all() return [Tournament(id=t.id, name=t.name, description=session.query(func.ts_headline('english', t.description, query_func)) .first()) for t in results]
def text_search_by_words(self, words: list) -> list: search_words = ' & '.join(words) with session_scope(self.get_session) as session: results = session.query(Tournament) \ .filter(Tournament.tsv.match(search_words, postgresql_regconfig='english')).all() return [Tournament(id=t.id, name=t.name, description=session.query( func.ts_headline('english', t.description, func.to_tsquery(search_words, postgresql_regconfig='english'))) .first()) for t in results]
def text_search_by_word(self, word) -> list: with session_scope(self.get_session) as session: results = session.query(Team).filter( Team.tsv.match(word, postgresql_regconfig='english')).all() return [ Team(id=t.id, name=t.name, description=session.query( func.ts_headline( 'english', t.description, func.to_tsquery( word, postgresql_regconfig='english'))).first()) for t in results ]
def ts_locs_array( config: ColumnElement, text: ColumnElement, tsquery: ColumnElement, ) -> ColumnElement: options = f"HighlightAll = TRUE, StartSel = {TS_START}, StopSel = {TS_STOP}" delimited = func.ts_headline(config, text, tsquery, options) parts = func.unnest(func.string_to_array(delimited, TS_START)).alias() part = column(parts.name) part_len = func.length(part) - len(TS_STOP) match_pos = func.sum(part_len).over(rows=(None, -1)) + len(TS_STOP) match_len = func.strpos(part, TS_STOP) - 1 return func.array( select([postgresql.array([match_pos, match_len]) ]).select_from(parts).offset(1).as_scalar(), )
def fulltext_search(self, query: str, including: bool): """ SELECT id, ts_headline(description, q) FROM ( SELECT id, description, q FROM goods, to_tsquery('english', 'query & here') q WHERE to_tsvector('english', description) @@ q ) search_t; """ query = self.__prepare_query(query, including) q = func.to_tsquery('english', query) inner_statement = self.__session \ .query(Goods.id, Goods.description, q) \ .select_from(q) \ .filter(func.to_tsvector('english', Goods.description).match(query, postgresql_regconfig='english')) \ .subquery() return self.__session.query( inner_statement.c.id, func.ts_headline(inner_statement.c.description, q)).all()
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if info and '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k, v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline( self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd( func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append( _entity_descriptor(model, 'info')[k].astext == v) else: if type(info) == dict: clauses.append(_entity_descriptor(model, 'info') == info) if type(info) == str or type(info) == str: try: info = json.loads(info) if type(info) == int or type(info) == float: info = '"%s"' % info except ValueError: info = '"%s"' % info clauses.append(_entity_descriptor(model, 'info').contains(info)) return clauses, headlines, order_by_ranks
def search(name): name = name.lower() queries = name.split() # makes an array print(queries) tup = () a = and_(Character.tsvector_col.match(q) for q in queries) o = or_(Character.tsvector_col.match(q) for q in queries) #achar = and_(*tup) #ochar = or_(tup) a_string = '' for index in range(0, len(queries)): a_string += queries[index] if index + 1 is not len(queries): a_string += ' & ' print(a_string) o_string = '' for index in range(0, len(queries)): o_string += queries[index] if index + 1 is not len(queries): o_string += ' | ' print(o_string) acharacters = db.session.query(Character, func.ts_headline('english', Character.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Character.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline('english', Character.appear, func.plainto_tsquery(a_string)).label('happear')).filter(and_(Character.tsvector_col.match(q) for q in queries)).all() ocharacters = db.session.query(Character, func.ts_headline('english', Character.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Character.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline('english', Character.appear, func.plainto_tsquery(o_string)).label('happear')).filter(or_(Character.tsvector_col.match(q) for q in queries)).all() apublishers = db.session.query(Publisher, func.ts_headline('english', Publisher.state, func.to_tsquery(a_string)).label('hstate'), func.ts_headline('english', Publisher.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Publisher.address, func.plainto_tsquery(a_string)).label('haddress'), func.ts_headline('english', Publisher.city, func.plainto_tsquery(name)).label('hcity')).filter(and_(Publisher.tsvector_col.match(q) for q in queries)).all() opublishers = db.session.query(Publisher, func.ts_headline('english', Publisher.state, func.to_tsquery(o_string)).label('hstate'), func.ts_headline('english', Publisher.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Publisher.address, func.plainto_tsquery(o_string)).label('haddress'), func.ts_headline('english', Publisher.city, func.plainto_tsquery(name)).label('hcity')).filter(or_(Publisher.tsvector_col.match(q) for q in queries)).all() ateams = db.session.query(Team, func.ts_headline('english', Team.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Team.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline('english', Team.appear, func.plainto_tsquery(a_string)).label('happear')).filter(and_(Team.tsvector_col.match(q) for q in queries)).all() oteams = db.session.query(Team, func.ts_headline('english', Team.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Team.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline('english', Team.appear, func.plainto_tsquery(o_string)).label('happear')).filter(or_(Team.tsvector_col.match(q) for q in queries)).all() avolumes = db.session.query(Volume, func.ts_headline('english', Volume.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Volume.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline('english', Volume.start_year, func.plainto_tsquery(a_string)).label('hstart')).filter(and_(Volume.tsvector_col.match(q) for q in queries)).all() ovolumes = db.session.query(Volume, func.ts_headline('english', Volume.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Volume.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline('english', Volume.start_year, func.plainto_tsquery(o_string)).label('hstart')).filter(or_(Volume.tsvector_col.match(q) for q in queries)).all() #print(publishers) #print(teams) #print(volumes) return render_template('search_result_template.html', acharacters=acharacters, ocharacters=ocharacters, opublishers=opublishers, apublishers=apublishers, avolumes=avolumes, ovolumes=ovolumes, ateams=ateams, oteams=oteams)
def handle_info_json(self, model, info, fulltextsearch=None): """Handle info JSON query filter.""" clauses = [] headlines = [] order_by_ranks = [] if info and '::' in info: pairs = info.split('|') for pair in pairs: if pair != '': k,v = pair.split("::") if fulltextsearch == '1': vector = _entity_descriptor(model, 'info')[k].astext clause = func.to_tsvector(vector).match(v) clauses.append(clause) if len(headlines) == 0: headline = func.ts_headline(self.language, vector, func.to_tsquery(v)) headlines.append(headline) order = func.ts_rank_cd(func.to_tsvector(vector), func.to_tsquery(v), 4).label('rank') order_by_ranks.append(order) else: clauses.append(_entity_descriptor(model, 'info')[k].astext == v) else: if type(info) == dict: clauses.append(_entity_descriptor(model, 'info') == info) if type(info) == str or type(info) == unicode: try: info = json.loads(info) if type(info) == int or type(info) == float: info = '"%s"' % info except ValueError: info = '"%s"' % info clauses.append(_entity_descriptor(model, 'info').contains(info)) return clauses, headlines, order_by_ranks
def search_text(): terms = request.args['q'] page = int(request.args.get('page', '1')) format = request.args.get('format', 'html') query = CadorsReport.query.filter( 'cadors_report.narrative_agg_idx_col @@ plainto_tsquery(:terms)') query = query.params(terms=terms) query = query.order_by( 'ts_rank_cd(narrative_agg_idx_col, plainto_tsquery(:terms)) DESC', CadorsReport.timestamp.desc()) if format == 'html': query = query.add_column( func.ts_headline('pg_catalog.english', CadorsReport.narrative_agg, func.plainto_tsquery(terms), '''MaxFragments=2, MinWords=15, MaxWords=20, FragmentDelimiter=|||, StartSel="<b>", StopSel = "</b>"''', type_=types.Unicode)) pagination = query.paginate(page) response = make_response( render_template('sr_text.html', reports=pagination.items, pagination=pagination, terms=terms)) return prepare_response(response, 300) else: pagination = query.paginate(page) title = "Results for '%s'" % (terms) return render_list(pagination, title, format)
def search(term): # Parse it term = term.lower().replace("%20", " ") terms = term.split() queryAndArtist = session.query(Artist, func.ts_headline('english', Artist.name, func.plainto_tsquery(term)).label('h_name')) \ .filter(and_(Artist.tsvector_col.match(s) for s in terms)).all() queryOrArtist = session.query(Artist, func.ts_headline('english', Artist.name, func.plainto_tsquery(term)).label('h_name')) \ .filter(or_(Artist.tsvector_col.match(s) for s in terms)).all() queryAndSong = session.query(Song, func.ts_headline('english', Song.song_name, func.plainto_tsquery(term)).label('h_song_name'), \ func.ts_headline('english', Song.artist_name, \ func.plainto_tsquery(term)).label('h_artist_name'), \ func.ts_headline('english', Song.album_name, func.plainto_tsquery(term)).label('h_album_name')) \ .filter(and_(Song.tsvector_col.match(s) for s in terms)).all() queryOrSong = session.query(Song, \ func.ts_headline('english', Song.song_name, func.plainto_tsquery(term)).label('h_song_name'), \ func.ts_headline('english', Song.artist_name, \ func.plainto_tsquery(term)).label('h_artist_name'), \ func.ts_headline('english', Song.album_name, func.plainto_tsquery(term)).label('h_album_name')) \ .filter(or_(Song.tsvector_col.match(s) for s in terms)).all() queryAndYear = session.query(Year, \ func.ts_headline('english', Year.year, func.plainto_tsquery(term)).label('h_year'), \ func.ts_headline('english', Year.top_genre_name, func.plainto_tsquery(term)).label('h_top_genre_name'), \ func.ts_headline('english', Year.top_album_name, func.plainto_tsquery(term)).label('h_top_album_name')) \ .filter(and_(Year.tsvector_col.match(s) for s in terms)).all() queryOrYear = session.query(Year, \ func.ts_headline('english', Year.year, func.plainto_tsquery(term)).label('h_year'), \ func.ts_headline('english', Year.top_genre_name, func.plainto_tsquery(term)).label('h_top_genre_name'), \ func.ts_headline('english', Year.top_album_name, func.plainto_tsquery(term)).label('h_top_album_name')) \ .filter(or_(Year.tsvector_col.match(s) for s in terms)).all() queryAndGenre = session.query(Genre, \ func.ts_headline('english', Genre.name, func.plainto_tsquery(term)).label('h_name'), \ func.ts_headline('english', Genre.description, func.plainto_tsquery(term)).label('h_description')) \ .filter(and_(Genre.tsvector_col.match(s) for s in terms)).all() queryOrGenre = session.query(Genre, \ func.ts_headline('english', Genre.name, func.plainto_tsquery(term)).label('h_name'), \ func.ts_headline('english', Genre.description, func.plainto_tsquery(term)).label('h_description')) \ .filter(or_(Genre.tsvector_col.match(s) for s in terms)).all() return render_template('search.html', andArtist = queryAndArtist, orArtist = queryOrArtist, andSong = queryAndSong, orSong = queryOrSong, andYear = queryAndYear, orYear = queryOrYear, andGenre = queryAndGenre, orGenre = queryOrGenre, term=term)
def search(name): name = name.lower() queries = name.split() # makes an array print(queries) tup = () a = and_(Character.tsvector_col.match(q) for q in queries) o = or_(Character.tsvector_col.match(q) for q in queries) #achar = and_(*tup) #ochar = or_(tup) a_string = '' for index in range(0, len(queries)): a_string += queries[index] if index + 1 is not len(queries): a_string += ' & ' print(a_string) o_string = '' for index in range(0, len(queries)): o_string += queries[index] if index + 1 is not len(queries): o_string += ' | ' print(o_string) acharacters = db.session.query( Character, func.ts_headline('english', Character.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Character.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline( 'english', Character.appear, func.plainto_tsquery(a_string)).label('happear')).filter( and_(Character.tsvector_col.match(q) for q in queries)).all() ocharacters = db.session.query( Character, func.ts_headline('english', Character.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Character.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline( 'english', Character.appear, func.plainto_tsquery(o_string)).label('happear')).filter( or_(Character.tsvector_col.match(q) for q in queries)).all() apublishers = db.session.query( Publisher, func.ts_headline('english', Publisher.state, func.to_tsquery(a_string)).label('hstate'), func.ts_headline('english', Publisher.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Publisher.address, func.plainto_tsquery(a_string)).label('haddress'), func.ts_headline( 'english', Publisher.city, func.plainto_tsquery(name)).label('hcity')).filter( and_(Publisher.tsvector_col.match(q) for q in queries)).all() opublishers = db.session.query( Publisher, func.ts_headline('english', Publisher.state, func.to_tsquery(o_string)).label('hstate'), func.ts_headline('english', Publisher.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Publisher.address, func.plainto_tsquery(o_string)).label('haddress'), func.ts_headline( 'english', Publisher.city, func.plainto_tsquery(name)).label('hcity')).filter( or_(Publisher.tsvector_col.match(q) for q in queries)).all() ateams = db.session.query( Team, func.ts_headline('english', Team.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Team.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline( 'english', Team.appear, func.plainto_tsquery(a_string)).label('happear')).filter( and_(Team.tsvector_col.match(q) for q in queries)).all() oteams = db.session.query( Team, func.ts_headline('english', Team.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Team.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline( 'english', Team.appear, func.plainto_tsquery(o_string)).label('happear')).filter( or_(Team.tsvector_col.match(q) for q in queries)).all() avolumes = db.session.query( Volume, func.ts_headline('english', Volume.name, func.to_tsquery(a_string)).label('hname'), func.ts_headline('english', Volume.publisher_name, func.plainto_tsquery(a_string)).label('hpub'), func.ts_headline( 'english', Volume.start_year, func.plainto_tsquery(a_string)).label('hstart')).filter( and_(Volume.tsvector_col.match(q) for q in queries)).all() ovolumes = db.session.query( Volume, func.ts_headline('english', Volume.name, func.to_tsquery(o_string)).label('hname'), func.ts_headline('english', Volume.publisher_name, func.plainto_tsquery(o_string)).label('hpub'), func.ts_headline( 'english', Volume.start_year, func.plainto_tsquery(o_string)).label('hstart')).filter( or_(Volume.tsvector_col.match(q) for q in queries)).all() #print(publishers) #print(teams) #print(volumes) return render_template('search_result_template.html', acharacters=acharacters, ocharacters=ocharacters, opublishers=opublishers, apublishers=apublishers, avolumes=avolumes, ovolumes=ovolumes, ateams=ateams, oteams=oteams)
def filter_document(self, db: Session, *, filter_str: str, skip: int = 0, limit: int = 100) -> List[FilteredDocument]: # construct the query operand for the full test search. Use # the regconfig of the table column. We need to cast it to # regconfig, which seems to not be possible in sqlalchemy, so # construct the term as text. ts_query = func.websearch_to_tsquery( text("fulltext_regconfig::regconfig"), filter_str) start_sel = "<" stop_sel = ">" fragment_delimiter = "|" res = (db.query( Document, func.ts_headline( text("fulltext_regconfig::regconfig"), Document.fulltext, ts_query, f"MaxFragments=10, " f"minWords=4, " f"maxWords=8, " f"StartSel = {start_sel}, " f"StopSel = {stop_sel}, " f"FragmentDelimiter={fragment_delimiter}", ), ).filter(Document.fulltext_search_vector.op("@@")(ts_query)).offset( skip).limit(limit).all()) retval = [] for result in res: document = result[0] headlines_raw = result[1].split(fragment_delimiter) headlines = [] for hl in headlines_raw: remaining = hl tokens = [] while start_sel in remaining: token, _, remaining = remaining.partition(start_sel) if token: tokens.append(HeadlineToken(text=token, match=False)) token, _, remaining = remaining.partition(stop_sel) assert token tokens.append(HeadlineToken(text=token, match=True)) if remaining: tokens.append(HeadlineToken(text=remaining, match=False)) headlines.append(Headline(tokens=tokens)) retval.append( FilteredDocument(document=document, headlines=headlines)) return retval
def search(term): # Parse it term = term.lower().replace("%20", " ") terms = term.split() queryAndArtist = session.query(Artist, func.ts_headline('english', Artist.name, func.plainto_tsquery(term)).label('h_name')) \ .filter(and_(Artist.tsvector_col.match(s) for s in terms)).all() queryOrArtist = session.query(Artist, func.ts_headline('english', Artist.name, func.plainto_tsquery(term)).label('h_name')) \ .filter(or_(Artist.tsvector_col.match(s) for s in terms)).all() queryAndSong = session.query(Song, func.ts_headline('english', Song.song_name, func.plainto_tsquery(term)).label('h_song_name'), \ func.ts_headline('english', Song.artist_name, \ func.plainto_tsquery(term)).label('h_artist_name'), \ func.ts_headline('english', Song.album_name, func.plainto_tsquery(term)).label('h_album_name')) \ .filter(and_(Song.tsvector_col.match(s) for s in terms)).all() queryOrSong = session.query(Song, \ func.ts_headline('english', Song.song_name, func.plainto_tsquery(term)).label('h_song_name'), \ func.ts_headline('english', Song.artist_name, \ func.plainto_tsquery(term)).label('h_artist_name'), \ func.ts_headline('english', Song.album_name, func.plainto_tsquery(term)).label('h_album_name')) \ .filter(or_(Song.tsvector_col.match(s) for s in terms)).all() queryAndYear = session.query(Year, \ func.ts_headline('english', Year.year, func.plainto_tsquery(term)).label('h_year'), \ func.ts_headline('english', Year.top_genre_name, func.plainto_tsquery(term)).label('h_top_genre_name'), \ func.ts_headline('english', Year.top_album_name, func.plainto_tsquery(term)).label('h_top_album_name')) \ .filter(and_(Year.tsvector_col.match(s) for s in terms)).all() queryOrYear = session.query(Year, \ func.ts_headline('english', Year.year, func.plainto_tsquery(term)).label('h_year'), \ func.ts_headline('english', Year.top_genre_name, func.plainto_tsquery(term)).label('h_top_genre_name'), \ func.ts_headline('english', Year.top_album_name, func.plainto_tsquery(term)).label('h_top_album_name')) \ .filter(or_(Year.tsvector_col.match(s) for s in terms)).all() queryAndGenre = session.query(Genre, \ func.ts_headline('english', Genre.name, func.plainto_tsquery(term)).label('h_name'), \ func.ts_headline('english', Genre.description, func.plainto_tsquery(term)).label('h_description')) \ .filter(and_(Genre.tsvector_col.match(s) for s in terms)).all() queryOrGenre = session.query(Genre, \ func.ts_headline('english', Genre.name, func.plainto_tsquery(term)).label('h_name'), \ func.ts_headline('english', Genre.description, func.plainto_tsquery(term)).label('h_description')) \ .filter(or_(Genre.tsvector_col.match(s) for s in terms)).all() return render_template('search.html', andArtist=queryAndArtist, orArtist=queryOrArtist, andSong=queryAndSong, orSong=queryOrSong, andYear=queryAndYear, orYear=queryOrYear, andGenre=queryAndGenre, orGenre=queryOrGenre, term=term)