This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
alter table posts | ||
add column tsv tsvector; | ||
|
||
create index posts_tsv on posts using gin(tsv); | ||
|
||
update posts set tsv = to_tsvector(coalesce(title)) || to_tsvector(coalesce(body)); | ||
|
||
create or replace function posts_tsv_trigger() returns trigger as $$ | ||
begin | ||
new.tsv := to_tsvector(coalesce(new.title)) || to_tsvector(coalesce(new.body)); | ||
return new; | ||
end | ||
$$ language plpgsql; | ||
|
||
|
||
create trigger tsvectorupdate before insert or update on posts | ||
for each row execute procedure posts_tsv_trigger(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @flow | ||
import Sequelize from 'sequelize'; | ||
import db from 'database/db'; | ||
|
||
type SearchParameter = { | ||
tsquery: string, | ||
fk_user_id?: string, | ||
authorized?: boolean, | ||
page?: number, | ||
}; | ||
|
||
type SearchDataRow = { | ||
id: string, | ||
rank: number, | ||
}; | ||
|
||
export const searchPosts = async ({ | ||
tsquery, | ||
fk_user_id, | ||
authorized, | ||
page = 1, | ||
}: SearchParameter): Promise<SearchDataRow[]> => { | ||
const query = ` | ||
SELECT id, ts_rank(tsv, TO_TSQUERY($tsquery)) * 1000 + total_score AS rank | ||
FROM posts | ||
JOIN (select fk_post_id, SUM(score) as total_score from post_scores group by fk_post_id) as q on q.fk_post_id = posts.id | ||
WHERE tsv @@ TO_TSQUERY($tsquery) | ||
ORDER BY rank DESC | ||
OFFSET ${10 * (page - 1)} | ||
LIMIT 10 | ||
`; | ||
|
||
try { | ||
const rows = await db.query(query, { | ||
bind: { tsquery }, | ||
}); | ||
return rows[0]; | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; | ||
|
||
export const countSearchPosts = async ({ | ||
tsquery, | ||
fk_user_id, | ||
authorized, | ||
page = 1, | ||
}: SearchParameter): Promise<number> => { | ||
const query = ` | ||
SELECT COUNT(*) as count | ||
FROM posts | ||
WHERE tsv @@ TO_TSQUERY($tsquery) | ||
`; | ||
|
||
try { | ||
const rows = await db.query(query, { | ||
bind: { tsquery }, | ||
}); | ||
return rows[0][0].count; | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// @flow | ||
import type { Context } from 'koa'; | ||
import Post, { serializePost } from 'database/models/Post'; | ||
import { searchPosts, countSearchPosts } from 'database/rawQuery/search'; | ||
import { formatShortDescription } from 'lib/common'; | ||
|
||
export const publicSearch = async (ctx: Context) => { | ||
const { q } = ctx.query; | ||
const transformed = `${q.replace(/ /, '|')}:*`; | ||
try { | ||
const [count, searchResult] = await Promise.all([ | ||
countSearchPosts({ | ||
tsquery: transformed, | ||
}), | ||
searchPosts({ | ||
tsquery: transformed, | ||
}), | ||
]); | ||
|
||
const postIds = searchResult.map(r => r.id); | ||
const posts = await Post.readPostsByIds(postIds); | ||
const data = posts | ||
.map(serializePost) | ||
.map(post => ({ ...post, body: formatShortDescription(post.body) })); | ||
ctx.body = { | ||
count, | ||
data, | ||
}; | ||
} catch (e) { | ||
ctx.throw(500, e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @flow | ||
import Router from 'koa-router'; | ||
import { publicSearch } from './search.ctrl'; | ||
|
||
const search = new Router(); | ||
|
||
search.get('/public', publicSearch); | ||
export default search; |