Headlines can be displayed for file search results.

This commit is contained in:
Roland 2023-04-12 18:09:10 -06:00
parent f33dc31111
commit 29e562c520
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
7 changed files with 145 additions and 10 deletions

View file

@ -438,6 +438,7 @@ function Archives() {
selectedArchives,
onSelect,
setPage,
!!searchStr,
);
const searchInput = <SearchInput clearable

View file

@ -54,6 +54,7 @@ import {FileBrowser} from "./FileBrowser";
import {refreshDirectoryFiles, refreshFiles} from "../api";
import {useSubscribeEventName} from "../Events";
import {TagsSelector} from "../Tags";
import {Headlines} from "./Headline";
function EbookCard({file}) {
const {s} = useContext(ThemeContext);
@ -264,9 +265,20 @@ export function FileRowTagIcon({file}) {
}
}
export function SearchViewButton() {
export function SearchViewButton({headlines}) {
const {view, setView} = useSearchView();
if (headlines) {
// Cycle: cards -> headline -> list
if (view === 'headline') {
return <Button icon='list' onClick={() => setView('list')}/>;
} else if (view === 'list') {
return <Button icon='th' onClick={() => setView('cards')}/>;
}
return <Button icon='searchengin' onClick={() => setView('headline')}/>;
}
// Cycle: cards -> list
if (view === 'list') {
return <Button icon='th' onClick={() => setView('cards')}/>;
}
@ -295,6 +307,7 @@ export function FilesView(
selectedKeys,
onSelect,
setPage,
headlines = false,
limitOptions = [12, 24, 48, 96],
) {
const {view} = useSearchView();
@ -328,11 +341,13 @@ export function FilesView(
footer={footer}
selectedKeys={selectedKeys || []}
/>;
} else if (view === 'headline') {
body = <Headlines results={files}/>;
} else {
body = <FileCards files={files}/>;
}
const viewButton = <SearchViewButton/>
const viewButton = <SearchViewButton headlines={headlines}/>
const limitDropdown = <SearchLimitDropdown limits={limitOptions}/>;
const tagQuerySelector = <TagsQuerySelector/>;
@ -446,6 +461,10 @@ export function FilesSearchView({
activePage,
totalPages,
setPage,
null,
null,
null,
true,
);
return <>

View file

@ -0,0 +1,92 @@
import {CardIcon, Header, Placeholder, Segment} from "./Theme";
import {PlaceholderLine, Item, Image} from "semantic-ui-react";
import React from "react";
import {ThemeContext} from "../contexts/contexts";
import {encodeMediaPath, FileIcon, findPosterPath, isoDatetimeToString, PreviewLink} from "./Common";
import {Link} from "react-router-dom";
import _ from "lodash";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
const HeadlineParagraph = ({headline}) => {
const {s} = React.useContext(ThemeContext);
if (headline) {
headline = headline.replaceAll('<b>', '<b><u>');
headline = headline.replaceAll('</b>', '</b></u>');
headline = headline.replaceAll('\n', '<br/>');
return <p {...s} dangerouslySetInnerHTML={{__html: headline}}></p>
}
}
const FileHeadline = ({file, to}) => {
const {title_headline, b_headline, c_headline, d_headline} = file;
let poster;
const posterPath = findPosterPath(file);
if (posterPath) {
const posterUrl = posterPath ? `/media/${encodeMediaPath(posterPath)}` : null;
poster = <Image src={posterUrl} size='tiny'/>;
} else {
poster = <FileIcon file={file}/>;
}
let header = <PreviewLink file={file}><HeadlineParagraph headline={title_headline}/></PreviewLink>;
if (to) {
header = <Link to={to}><HeadlineParagraph headline={title_headline}/></Link>
}
let body = <p>No highlights</p>
if (b_headline || c_headline || d_headline) {
body = <>
<HeadlineParagraph headline={b_headline}/>
<HeadlineParagraph headline={c_headline}/>
<HeadlineParagraph headline={d_headline}/>
</>;
}
return <Segment>
<Grid>
<Grid.Row>
<Grid.Column mobile={4} computer={2}>{poster}</Grid.Column>
<Grid.Column mobile={12} computer={12}>
<span style={{fontSize: '1.25em'}}>{header}</span>
</Grid.Column>
</Grid.Row>
</Grid>
<br/>
{body}
</Segment>
}
export const Headlines = ({results}) => {
if (results === null || results === undefined) {
return <Placeholder>
<PlaceholderLine/>
<PlaceholderLine/>
<PlaceholderLine/>
</Placeholder>
} else if (results && results.length === 0) {
return <Segment>No results!</Segment>
}
let items = [];
for (let i = 0; i < results.length; i++) {
const result = results[i];
const {model, data, video} = result;
if (model === 'video' && !_.isEmpty(video)) {
let video_url = `/videos/video/${video.id}`;
const channel = video.channel ? video.channel : null;
if (channel) {
video_url = `/videos/channel/${channel.id}/video/${video.id}`;
}
items = [...items, <FileHeadline key={result['key']} file={result} to={video_url}/>];
} else if (model === 'archive' && !_.isEmpty(data)) {
items = [...items, <FileHeadline key={result['key']} file={result} to={`/archive/${data.id}`}/>];
} else {
items = [...items, <FileHeadline key={result['key']} file={result}/>];
}
}
return <>
{items}
</>
}

View file

@ -176,6 +176,7 @@ function VideosPage() {
selectedVideos,
onSelect,
setPage,
!!searchStr,
);
const searchInput = <SearchInput clearable

View file

@ -416,7 +416,11 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order
stmt = f'''
SELECT
fg.id -- always get `file_group.id` for `handle_file_group_search_results`
{select_columns}
{select_columns},
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"
FROM file_group fg
LEFT JOIN archive a ON a.file_group_id = fg.id
{join}

View file

@ -127,7 +127,11 @@ def search_videos(
join = '\n'.join(joins)
stmt = f'''
SELECT
{select_columns}
{select_columns},
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"
FROM file_group fg
{join}
{where}

View file

@ -592,7 +592,11 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str]
selects = f"{', '.join(selects)}, " if selects else ""
join = '\n'.join(joins)
stmt = f'''
SELECT fg.id, {selects} COUNT(*) OVER() AS total
SELECT fg.id, {selects} COUNT(*) OVER() AS total,
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"
FROM file_group fg
{join}
{f"WHERE {wheres}" if wheres else ""}
@ -644,10 +648,16 @@ def handle_file_group_search_results(statement: str, params: dict) -> Tuple[List
total = results[0]['total'] if results else 0
ordered_ids = [i['id'] for i in results]
try:
ranks = [i['ts_rank'] for i in results]
extras = [dict(
ts_rank=i.get('ts_rank'),
title_headline=i.get('title_headline'),
b_headline=i.get('b_headline'),
c_headline=i.get('c_headline'),
d_headline=i.get('d_headline'),
) for i in results]
except KeyError:
# No `ts_rank`, probably not searching `file_group.textsearch`.
ranks = []
extras = []
with get_db_session() as session:
from modules.videos.models import Video
@ -657,15 +667,19 @@ def handle_file_group_search_results(statement: str, params: dict) -> Tuple[List
# Order FileGroups by their location in ordered_ids.
file_groups: List[Tuple[FileGroup, Video]] = sorted(results, key=lambda i: ordered_ids.index(i[0].id))
results = list()
for rank, (file_group, video) in zip_longest(ranks, file_groups):
for extra, (file_group, video) in zip_longest(extras, file_groups):
video: Video
if video:
results.append(video.__json__())
else:
results.append(file_group.__json__())
# Preserve the ts_ranks, if any.
if rank:
results[-1]['ts_rank'] = rank
if extra:
results[-1]['ts_rank'] = extra['ts_rank']
results[-1]['title_headline'] = extra['title_headline']
results[-1]['b_headline'] = extra['b_headline']
results[-1]['c_headline'] = extra['c_headline']
results[-1]['d_headline'] = extra['d_headline']
return results, total